Merge branch 'dev' into feature_gardenplan-model_M2

This commit is contained in:
Roman Schenk
2022-11-06 18:22:33 +01:00
committed by GitHub Enterprise
13 changed files with 560 additions and 27 deletions
@@ -20,7 +20,7 @@ import java.util.Map;
import java.util.Optional;
public class JsonGardenPlan implements GardenPlan {
private final URL dataSource = getClass().getResource("user-crops.json");
private final URL dataSource;
private IdProvider idProvider;
private Map<Long, Crop> cropMap = Collections.emptyMap();
@@ -40,7 +40,21 @@ public class JsonGardenPlan implements GardenPlan {
}
/**
* @see GardenPlan#getCrops()
* Default constructor
*/
public JsonGardenPlan() {
this.dataSource = getClass().getResource("user-crops.json");
}
/**
* Constructor to use a specified {@link URL} as a {@link #dataSource}
* @param dataSource A {@link URL} to the file to be used as a data source
*/
public JsonGardenPlan(URL dataSource) {
this.dataSource = dataSource;
}
/**
* {@inheritDoc}
*/
@Override
public List<Crop> getCrops() throws IOException {
@@ -51,7 +65,7 @@ public class JsonGardenPlan implements GardenPlan {
}
/**
* @see GardenPlan#getCropById(long)
* {@inheritDoc}
*/
@Override
public Optional<Crop> getCropById(long id) throws IOException {
@@ -62,7 +76,7 @@ public class JsonGardenPlan implements GardenPlan {
}
/**
* @see GardenPlan#saveCrop(Crop)
* {@inheritDoc}
*
* Saves a crop to the database.
* If no {@link Crop#cropId} is set, one will be generated and
@@ -79,7 +93,7 @@ public class JsonGardenPlan implements GardenPlan {
}
/**
* @see GardenPlan#removeCrop(Crop)
* {@inheritDoc}
*/
@Override
public void removeCrop(Crop crop) throws IOException {
@@ -89,15 +89,16 @@ public class JsonPlantDatabase implements PlantDatabase {
List<Plant> result;
result = mapper.readerForListOf(Plant.class).readValue(dataSource);
for (Plant plant : result) {
plant.inZone(currentZone);
}
// Turn list into a HashMap with structure id => Plant
plantMap = result.stream()
// Remove plants not in the current zone
.filter(plant -> {
plant.inZone(currentZone);
return !plant.lifecycle().isEmpty();
})
// Create Hashmap from results
.collect(HashMap::new,
(res, plant) -> res.put(plant.id(), plant),
(existing, replacement) -> { });
(existing, replacement) -> {});
}
}
}
@@ -9,8 +9,10 @@ import ch.zhaw.gartenverwaltung.types.*;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class TaskListModel {
@@ -77,6 +79,10 @@ public class TaskListModel {
taskDatabase.removeTask(task);
}
private List<Task> filterListByCrop(List<Task> taskList, Long cropId) {
return taskList.stream().filter(task -> task.getCropId() == cropId).collect(Collectors.toList());
}
/**
* Method to get all Tasks
* @return List of all Tasks
@@ -86,6 +92,15 @@ public class TaskListModel {
return getFilteredTaskList(LocalDate.MIN, LocalDate.MAX);
}
/**
* Method to get all Tasks for specific Crop
* @return List of all Tasks for with given CropID
* @throws IOException If the database cannot be accessed
*/
public List<Task> getTaskListForCrop(Long cropId) throws IOException {
return filterListByCrop(getTaskList(), cropId);
}
/**
* Method to get all Tasks which are today or in future
* @return List of Tasks
@@ -95,6 +110,15 @@ public class TaskListModel {
return getFilteredTaskList(LocalDate.now(), LocalDate.MAX);
}
/**
* Method to get all Tasks which are today or in future for specific Crop
* @return List of Tasks with given crop ID
* @throws IOException If the database cannot be accessed
*/
public List<Task> getFutureTasksForCrop(Long cropId) throws IOException {
return filterListByCrop(getFutureTasks(), cropId);
}
/**
* Method to get all Tasks which are today or in past
* @return List of Tasks
@@ -105,17 +129,40 @@ public class TaskListModel {
}
/**
* Method to get an Array of 7 Tasklists for the next 7 days. Index 0 is Tasklist for Today.
* @return Array with length 7 (List<Task>[])
* Method to get all Tasks which are today or in past for specifc crop
* @return List of Tasks with given grop id
* @throws IOException If the database cannot be accessed
*/
public List<Task>[] getTasksUpcomingWeek() throws IOException {
List<Task>[] listArray = new List[7];
public List<Task> getPastTasksForCrop(Long cropId) throws IOException {
return filterListByCrop(getPastTasks(), cropId);
}
/**
* Method to get an List of 7 Tasklists for the next 7 days. Index 0 is Tasklist for Today.
* @return List with length 7 (List<List<Task>>)
* @throws IOException If the database cannot be accessed
*/
public List<List<Task>> getTasksUpcomingWeek() throws IOException {
List<List<Task>> dayTaskList = new ArrayList<>();
for(int i = 0; i < 7; i++) {
LocalDate date = LocalDate.now().plusDays(i);
listArray[i] = taskDatabase.getTaskList(date, date);
dayTaskList.add(taskDatabase.getTaskList(date, date));
}
return listArray;
return dayTaskList;
}
/**
* Method to get an List of 7 Tasklists for the next 7 days. (Filtered Index 0 is Tasklist for Today.
* @return List with length 7 (List<List<Task>>)
* @throws IOException If the database cannot be accessed
*/
public List<List<Task>> getTasksUpcomingWeekForCrop(Long cropId) throws IOException {
List<List<Task>> dayTaskList = new ArrayList<>();
for(int i = 0; i < 7; i++) {
LocalDate date = LocalDate.now().plusDays(i);
dayTaskList.add(filterListByCrop(taskDatabase.getTaskList(date, date), cropId));
}
return dayTaskList;
}
/**
@@ -1,6 +1,7 @@
package ch.zhaw.gartenverwaltung.types;
import java.time.LocalDate;
import java.util.Objects;
import java.util.Optional;
public class Crop {
@@ -41,6 +42,24 @@ public class Crop {
public LocalDate getStartDate() { return startDate; }
public double getArea() { return area; }
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other instanceof Crop otherCrop) {
return Objects.equals(this.cropId, otherCrop.cropId) &&
plantId == otherCrop.plantId &&
startDate != null && startDate.equals(otherCrop.startDate) &&
area == otherCrop.area;
}
return false;
}
@Override
public int hashCode() {
int startCode = startDate != null ? startDate.hashCode() : 0;
return (int) plantId ^ (startCode << 16);
}
@Override
public String toString() {
return String.format("Crop [ cropId: %d, plantId: %d, startDate: %s, area: %f ]",
@@ -21,11 +21,11 @@ public class Task {
* default constructor
* (used by Json deserializer)
*/
public Task(){
public Task(long cropId){
name= "";
description= "";
startDate = LocalDate.now();
// this.cropId = cropId;
this.cropId = cropId;
}
public Task(String name, String description, LocalDate startDate, long cropId) {
@@ -31,7 +31,7 @@
"name": "Germinate",
"relativeStartDate": -14,
"relativeEndDate": null,
"description": "\"Take an egg carton and fill it with soil. Put the seedling deep enaugh so its half covered with soil. Keep it in 10-15 * Celsius with lots of light.\"",
"description": "Take an egg carton and fill it with soil. Put the seedling deep enough so its half covered with soil. Keep it in 10-15 * Celsius with lots of light.",
"interval": null,
"isOptional": false
}
@@ -53,7 +53,7 @@
"name": "hilling",
"relativeStartDate": 0,
"relativeEndDate": null,
"description": "\"When the plants are 20 cm tall, begin hilling the potatoes by gently mounding the soil from the center of your rows around the stems of the plant. Mound up the soil around the plant until just the top few leaves show above the soil. Two weeks later, hill up the soil again when the plants grow another 20 cm.\"",
"description": "When the plants are 20 cm tall, begin hilling the potatoes by gently mounding the soil from the center of your rows around the stems of the plant. Mound up the soil around the plant until just the top few leaves show above the soil. Two weeks later, hill up the soil again when the plants grow another 20 cm.",
"interval": 21,
"isOptional": false
}