From 25057d34f0a9d38e54e5f36974bd15f3b477311b Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 31 Oct 2022 08:06:35 +0100 Subject: [PATCH 01/10] created Class TaskListModel --- .../taskList/TaskListModel.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java new file mode 100644 index 0000000..7e158c4 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java @@ -0,0 +1,64 @@ +package ch.zhaw.gartenverwaltung.taskList; + +import ch.zhaw.gartenverwaltung.io.JsonTaskDatabase; +import ch.zhaw.gartenverwaltung.io.TaskDatabase; +import ch.zhaw.gartenverwaltung.types.Task; + +import java.io.IOException; +import java.time.LocalDate; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +public class TaskListModel { + private TaskDatabase taskDatabase; + + static final Comparator sortByStartDate = Comparator.comparing(Task::getStartDate); + + + + public TaskListModel(){ + taskDatabase = new JsonTaskDatabase(); + } + + public TaskListModel(TaskDatabase taskDatabase) { + this.taskDatabase = taskDatabase; + } + + public void addTask(Task task) throws IOException { + taskDatabase.saveTask(task); + } + + public void removeTask(Task task) throws IOException { + taskDatabase.removeTask(task); + } + + public List getTaskList() throws IOException { + return getFilteredTaskList(LocalDate.MIN, LocalDate.MAX); + } + + public List getFutureTasks() throws IOException { + return getFilteredTaskList(LocalDate.now(), LocalDate.MAX); + } + + public List getPastTasks() throws IOException { + return getFilteredTaskList(LocalDate.MIN, LocalDate.now()); + } + + public List[] getTasksUpcomingWeek() throws IOException { + List[] listArray = new List[7]; + for(int i = 0; i < 7; i++) { + LocalDate date = LocalDate.now().plusDays(i); + listArray[i] = taskDatabase.getTaskList(date, date); + } + return listArray; + } + + public List getFilteredTaskList(LocalDate start, LocalDate end) throws IOException { + return getSortedTaskList(taskDatabase.getTaskList(start, end), sortByStartDate); + } + + private List getSortedTaskList(List taskList, Comparator comparator) { + return taskList.stream().sorted(comparator).collect(Collectors.toList()); + } +} From 007fc81b2208ebd49ad911bfb9bb94294189856f Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 31 Oct 2022 13:47:17 +0100 Subject: [PATCH 02/10] added Method planTasksForCrop --- .../taskList/TaskListModel.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java index 7e158c4..7525886 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java @@ -1,7 +1,12 @@ package ch.zhaw.gartenverwaltung.taskList; +import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.io.JsonTaskDatabase; +import ch.zhaw.gartenverwaltung.io.PlantDatabase; import ch.zhaw.gartenverwaltung.io.TaskDatabase; +import ch.zhaw.gartenverwaltung.types.Crop; +import ch.zhaw.gartenverwaltung.types.HardinessZone; +import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Task; import java.io.IOException; @@ -12,23 +17,30 @@ import java.util.stream.Collectors; public class TaskListModel { private TaskDatabase taskDatabase; + private PlantDatabase plantDatabase; static final Comparator sortByStartDate = Comparator.comparing(Task::getStartDate); - - public TaskListModel(){ taskDatabase = new JsonTaskDatabase(); } - public TaskListModel(TaskDatabase taskDatabase) { + public TaskListModel(TaskDatabase taskDatabase, PlantDatabase plantDatabase) { this.taskDatabase = taskDatabase; + this.plantDatabase = plantDatabase; } public void addTask(Task task) throws IOException { taskDatabase.saveTask(task); } + public void planTasksForCrop(Crop crop) throws HardinessZoneNotSetException, IOException { + Plant plant = plantDatabase.getPlantById(HardinessZone.ZONE_8A, crop.getPlantId()).orElseThrow(); + // TODO new exception + // TODO HArdiness Zone + return; + } + public void removeTask(Task task) throws IOException { taskDatabase.removeTask(task); } From d1d9d11b665690ed825d8f8630de54804e74e939 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 31 Oct 2022 14:30:42 +0100 Subject: [PATCH 03/10] added Methods in TaskListModel added Field Crop ID in Task added method removeTasksforCrop --- .../gartenverwaltung/io/JsonTaskDatabase.java | 11 +++++++++++ .../zhaw/gartenverwaltung/io/TaskDatabase.java | 5 +++++ .../taskList/PlantNotFoundException.java | 7 +++++++ .../taskList/TaskListModel.java | 10 ++++++++-- .../ch/zhaw/gartenverwaltung/types/Task.java | 1 + .../ch/zhaw/gartenverwaltung/io/taskdb.json | 18 ++++++++++++------ 6 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/taskList/PlantNotFoundException.java diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java index e16a74d..62c18ba 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java @@ -1,5 +1,6 @@ package ch.zhaw.gartenverwaltung.io; +import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Task; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; @@ -51,6 +52,16 @@ public class JsonTaskDatabase implements TaskDatabase{ return taskMap.values().stream().filter(task -> task.isInTimePeriode(start, end)).toList(); } + @Override + public List getTaskForCrop(Crop crop) { + return null; //TODO implement + } + + @Override + public void removeTasksForCrop(Crop crop) { + // TODO implement + } + /** * If no data is currently loaded, data is loaded from {@link #dataSource}. * If the {@link Task} has an id than the task is added to the {@link #taskMap} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java index 19ad778..02ca54d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java @@ -1,5 +1,6 @@ package ch.zhaw.gartenverwaltung.io; +import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Task; @@ -24,6 +25,10 @@ public interface TaskDatabase { */ List getTaskList(LocalDate start, LocalDate end) throws IOException; + List getTaskForCrop(Crop crop); //TODO Javadoc + + void removeTasksForCrop(Crop crop); // TODO Javadoc + /** * Saves the {@link Task} in the Cache. * diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/PlantNotFoundException.java b/src/main/java/ch/zhaw/gartenverwaltung/taskList/PlantNotFoundException.java new file mode 100644 index 0000000..149e1ef --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/taskList/PlantNotFoundException.java @@ -0,0 +1,7 @@ +package ch.zhaw.gartenverwaltung.taskList; + +public class PlantNotFoundException extends Exception { + public PlantNotFoundException() { + super("The selected Plant was not found in Database!"); + } +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java index 7525886..a42738d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.time.LocalDate; import java.util.Comparator; import java.util.List; +import java.util.function.Supplier; import java.util.stream.Collectors; public class TaskListModel { @@ -34,13 +35,18 @@ public class TaskListModel { taskDatabase.saveTask(task); } - public void planTasksForCrop(Crop crop) throws HardinessZoneNotSetException, IOException { - Plant plant = plantDatabase.getPlantById(HardinessZone.ZONE_8A, crop.getPlantId()).orElseThrow(); + public void planTasksForCrop(Crop crop) throws PlantNotFoundException, HardinessZoneNotSetException, IOException { + Plant plant = plantDatabase.getPlantById(HardinessZone.ZONE_8A, crop.getPlantId()).orElseThrow(PlantNotFoundException::new); // TODO new exception // TODO HArdiness Zone return; } + public void removeTasksForCrop(Crop crop) { + //TODO implement + taskDatabase.removeTasksForCrop(crop); + } + public void removeTask(Task task) throws IOException { taskDatabase.removeTask(task); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index 3b26745..dabaf58 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -15,6 +15,7 @@ public class Task { private final LocalDate startDate; private Integer interval; private LocalDate endDate; + private Crop cropId; /** * default constructor diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json index da201c9..b3514b0 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/taskdb.json @@ -5,7 +5,8 @@ "description": "Plant the seeds, crops in de bed.", "startDate" : "2022-05-01", "endDate" : "2022-05-01", - "interval" : 0 + "interval" : 0, + "cropID" : 0 }, { "id" : 2, @@ -13,7 +14,8 @@ "description": "water the plant, so that the soil is wet around the plant.", "startDate" : "2022-05-01", "endDate" : "2022-09-01", - "interval" : 2 + "interval" : 2, + "cropID" : 0 }, { "id" : 3, @@ -21,7 +23,8 @@ "description": "The fertilizer has to be mixed with water. Then fertilize the plants soil with the mixture", "startDate" : "2022-06-01", "endDate" : "2022-08-01", - "interval" : 28 + "interval" : 28, + "cropID" : 0 }, { "id" : 4, @@ -29,7 +32,8 @@ "description": "Take a big enough coverage for the plants. Cover the whole plant with a bit space between the plant and the coverage", "startDate" : "2022-07-01", "endDate" : "2022-07-01", - "interval" : 0 + "interval" : 0, + "cropID" : 0 }, { "id" : 5, @@ -37,7 +41,8 @@ "description": "Look for pest or illness at the leaves of the plant. Check the soil around the plant, if the roots are enough covered with soil", "startDate" : "2022-05-01", "endDate" : "2022-09-01", - "interval" : 5 + "interval" : 5, + "cropID" : 0 }, { "id" : 6, @@ -45,6 +50,7 @@ "description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ", "startDate" : "2022-09-01", "endDate" : "2022-09-01", - "interval" : 0 + "interval" : 0, + "cropID" : 0 } ] \ No newline at end of file From 0e4e2075812ced796143bac31b3341c16474d4bc Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 31 Oct 2022 15:57:19 +0100 Subject: [PATCH 04/10] completed Methods created Config Class --- .../java/ch/zhaw/gartenverwaltung/Config.java | 19 +++++++++++++++++++ .../taskList/TaskListModel.java | 18 ++++++++---------- .../ch/zhaw/gartenverwaltung/types/Task.java | 13 ++++++------- .../gartenverwaltung/types/TaskTemplate.java | 4 ++-- 4 files changed, 35 insertions(+), 19 deletions(-) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/Config.java diff --git a/src/main/java/ch/zhaw/gartenverwaltung/Config.java b/src/main/java/ch/zhaw/gartenverwaltung/Config.java new file mode 100644 index 0000000..2fa0117 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/Config.java @@ -0,0 +1,19 @@ +package ch.zhaw.gartenverwaltung; + +import ch.zhaw.gartenverwaltung.types.HardinessZone; + +public class Config { + private static HardinessZone currentHardinessZone; + + static { + currentHardinessZone = HardinessZone.ZONE_8A; + } + + public static HardinessZone getCurrentHardinessZone() { + return currentHardinessZone; + } + + public static void setCurrentHardinessZone(HardinessZone currentHardinessZone) { + Config.currentHardinessZone = currentHardinessZone; + } +} diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java index a42738d..a285723 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java @@ -1,19 +1,16 @@ package ch.zhaw.gartenverwaltung.taskList; +import ch.zhaw.gartenverwaltung.Config; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.io.JsonTaskDatabase; import ch.zhaw.gartenverwaltung.io.PlantDatabase; import ch.zhaw.gartenverwaltung.io.TaskDatabase; -import ch.zhaw.gartenverwaltung.types.Crop; -import ch.zhaw.gartenverwaltung.types.HardinessZone; -import ch.zhaw.gartenverwaltung.types.Plant; -import ch.zhaw.gartenverwaltung.types.Task; +import ch.zhaw.gartenverwaltung.types.*; import java.io.IOException; import java.time.LocalDate; import java.util.Comparator; import java.util.List; -import java.util.function.Supplier; import java.util.stream.Collectors; public class TaskListModel { @@ -36,14 +33,15 @@ public class TaskListModel { } public void planTasksForCrop(Crop crop) throws PlantNotFoundException, HardinessZoneNotSetException, IOException { - Plant plant = plantDatabase.getPlantById(HardinessZone.ZONE_8A, crop.getPlantId()).orElseThrow(PlantNotFoundException::new); - // TODO new exception - // TODO HArdiness Zone - return; + Plant plant = plantDatabase.getPlantById(Config.getCurrentHardinessZone(), crop.getPlantId()).orElseThrow(PlantNotFoundException::new); + for (GrowthPhase growthPhase : plant.lifecycle()) { + for (TaskTemplate taskTemplate : growthPhase.taskTemplates()) { + addTask(taskTemplate.generateTask(crop.getStartDate(), crop.getCropId().orElse(0L))); + } + } } public void removeTasksForCrop(Crop crop) { - //TODO implement taskDatabase.removeTasksForCrop(crop); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index dabaf58..8d3db25 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -15,22 +15,24 @@ public class Task { private final LocalDate startDate; private Integer interval; private LocalDate endDate; - private Crop cropId; + private long cropId; /** * default constructor * (used by Json deserializer) */ - public Task(){ + public Task(long cropId){ name= ""; description= ""; startDate = LocalDate.now(); + this.cropId = cropId; } - public Task(String name, String description, LocalDate startDate) { + public Task(String name, String description, LocalDate startDate, long cropId) { this.name = name; this.description = description; this.startDate = startDate; + this.cropId = cropId; } public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval) { @@ -56,10 +58,7 @@ public class Task { } public boolean isInTimePeriode(LocalDate searchStartDate, LocalDate searchEndDate){ - if(startDate.isAfter(searchStartDate) &&startDate.isBefore(searchEndDate)){ - return true; - } - return false; + return startDate.isAfter(searchStartDate) && startDate.isBefore(searchEndDate); } // Getters diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/TaskTemplate.java b/src/main/java/ch/zhaw/gartenverwaltung/types/TaskTemplate.java index 290175e..8ae7862 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/TaskTemplate.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/TaskTemplate.java @@ -44,8 +44,8 @@ public class TaskTemplate { this.relativeStartDate = relativeStartDate; } - public Task generateTask(LocalDate realStartDate) { - Task task = new Task(name, description, realStartDate.plusDays(relativeStartDate)); + public Task generateTask(LocalDate realStartDate, long cropId) { + Task task = new Task(name, description, realStartDate.plusDays(relativeStartDate), cropId); if (relativeEndDate != null) { task.withEndDate(realStartDate.plusDays(relativeEndDate)); } From 5bfebdc92c7ef93d541aa3cd49c3dc70a614101b Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 31 Oct 2022 16:23:49 +0100 Subject: [PATCH 05/10] implemented Methods removeTasksForCrop and getTaskForCrop in JsonTaskDatabase --- .../gartenverwaltung/io/JsonTaskDatabase.java | 23 +++++++++++++--- .../gartenverwaltung/io/TaskDatabase.java | 15 +++++++++-- .../taskList/TaskListModel.java | 26 +++++++++++++++++-- .../ch/zhaw/gartenverwaltung/types/Task.java | 4 ++- 4 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java index 62c18ba..dd885d4 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java @@ -52,14 +52,29 @@ public class JsonTaskDatabase implements TaskDatabase{ return taskMap.values().stream().filter(task -> task.isInTimePeriode(start, end)).toList(); } + /** + * Method get all Tasks for a specific Crop + * @param cropId the cropId + * @return List of Tasks for given Crop + */ @Override - public List getTaskForCrop(Crop crop) { - return null; //TODO implement + public List getTaskForCrop(long cropId) throws IOException { + if(taskMap.isEmpty()) { + loadTaskListFromFile(); + } + return taskMap.values().stream().filter(task -> task.getCropId() == cropId).toList(); } + /** + * Method remove all Tasks for a specific Crop + * @param cropId the crop + */ @Override - public void removeTasksForCrop(Crop crop) { - // TODO implement + public void removeTasksForCrop(long cropId) throws IOException { + if(taskMap.isEmpty()) { + loadTaskListFromFile(); + } + taskMap.values().removeIf(task -> task.getCropId() == cropId); } /** diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java index 02ca54d..02bef7a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskDatabase.java @@ -25,9 +25,20 @@ public interface TaskDatabase { */ List getTaskList(LocalDate start, LocalDate end) throws IOException; - List getTaskForCrop(Crop crop); //TODO Javadoc + /** + * Method get all Tasks for a specific Crop + * @param cropId the cropId + * @return List of Tasks for given Crop + * @throws IOException If the database cannot be accessed + */ + List getTaskForCrop(long cropId) throws IOException; - void removeTasksForCrop(Crop crop); // TODO Javadoc + /** + * Method remove all Tasks for a specific Crop + * @param cropId the cropId + * @throws IOException If the database cannot be accessed + */ + void removeTasksForCrop(long cropId) throws IOException; /** * Saves the {@link Task} in the Cache. diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java index a285723..abd49bd 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java @@ -17,21 +17,39 @@ public class TaskListModel { private TaskDatabase taskDatabase; private PlantDatabase plantDatabase; + /** + * Comparators to create sorted Task List + */ static final Comparator sortByStartDate = Comparator.comparing(Task::getStartDate); public TaskListModel(){ taskDatabase = new JsonTaskDatabase(); } + /** + * Constructor to create Database Objects. + */ public TaskListModel(TaskDatabase taskDatabase, PlantDatabase plantDatabase) { this.taskDatabase = taskDatabase; this.plantDatabase = plantDatabase; } + /** + * Method to save a new Task to Task Database + * @param task the Task to save + * @throws IOException If the database cannot be accessed + */ public void addTask(Task task) throws IOException { taskDatabase.saveTask(task); } + /** + * Method to add all Tasks for a new crop + * @param crop the crop which is added + * @throws PlantNotFoundException if the plantId in the crop doesn't exist in Plant Database + * @throws HardinessZoneNotSetException If there is no Hardiness Zone Set in Plant Database + * @throws IOException If the database cannot be accessed + */ public void planTasksForCrop(Crop crop) throws PlantNotFoundException, HardinessZoneNotSetException, IOException { Plant plant = plantDatabase.getPlantById(Config.getCurrentHardinessZone(), crop.getPlantId()).orElseThrow(PlantNotFoundException::new); for (GrowthPhase growthPhase : plant.lifecycle()) { @@ -41,8 +59,12 @@ public class TaskListModel { } } - public void removeTasksForCrop(Crop crop) { - taskDatabase.removeTasksForCrop(crop); + /** + * Method to remove all Tasks for a specific Crop + * @param cropId The crop which is removed + */ + public void removeTasksForCrop(long cropId) throws IOException { + taskDatabase.removeTasksForCrop(cropId); } public void removeTask(Task task) throws IOException { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index 8d3db25..02d6480 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -35,12 +35,13 @@ public class Task { this.cropId = cropId; } - public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval) { + public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval, long cropId) { this.name = name; this.description = description; this.startDate = startDate; this.endDate = endDate; this.interval = interval; + this.cropId = cropId; } // Builder-pattern-style setters @@ -66,6 +67,7 @@ public class Task { public String getName() { return name; } public String getDescription() { return description; } public LocalDate getStartDate() { return startDate; } + public long getCropId() { return cropId; } public Optional getInterval() { return Optional.ofNullable(interval); From bf5c5c8439bc4b179f2374093183cf006a85e998 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 31 Oct 2022 16:31:59 +0100 Subject: [PATCH 06/10] javadocs in TaskListModel --- .../taskList/TaskListModel.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java index abd49bd..4f9384d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java @@ -62,27 +62,53 @@ public class TaskListModel { /** * Method to remove all Tasks for a specific Crop * @param cropId The crop which is removed + * @throws IOException If the database cannot be accessed */ public void removeTasksForCrop(long cropId) throws IOException { taskDatabase.removeTasksForCrop(cropId); } + /** + * Method to remove a Task from Database + * @param task the Task to remove + * @throws IOException If the database cannot be accessed + */ public void removeTask(Task task) throws IOException { taskDatabase.removeTask(task); } + /** + * Method to get all Tasks + * @return List of all Tasks + * @throws IOException If the database cannot be accessed + */ public List getTaskList() throws IOException { return getFilteredTaskList(LocalDate.MIN, LocalDate.MAX); } + /** + * Method to get all Tasks which are today or in future + * @return List of Tasks + * @throws IOException If the database cannot be accessed + */ public List getFutureTasks() throws IOException { return getFilteredTaskList(LocalDate.now(), LocalDate.MAX); } + /** + * Method to get all Tasks which are today or in past + * @return List of Tasks + * @throws IOException If the database cannot be accessed + */ public List getPastTasks() throws IOException { return getFilteredTaskList(LocalDate.MIN, LocalDate.now()); } + /** + * 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[]) + * @throws IOException If the database cannot be accessed + */ public List[] getTasksUpcomingWeek() throws IOException { List[] listArray = new List[7]; for(int i = 0; i < 7; i++) { @@ -92,10 +118,23 @@ public class TaskListModel { return listArray; } + /** + * Method to get Tasklist filtered by date. + * @param start the start date for the filter + * @param end the end date for the filter + * @return List of Tasks matched by the filter + * @throws IOException If the database cannot be accessed + */ public List getFilteredTaskList(LocalDate start, LocalDate end) throws IOException { return getSortedTaskList(taskDatabase.getTaskList(start, end), sortByStartDate); } + /** + * Method to sort a Tasklist by a given Comparator + * @param taskList The Tasklist to sort + * @param comparator the comparator to sort + * @return a sorted coppy of the given Tasklist + */ private List getSortedTaskList(List taskList, Comparator comparator) { return taskList.stream().sorted(comparator).collect(Collectors.toList()); } From 541217c2810a1ba389305ea16f73c7d7023334cd Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 31 Oct 2022 16:36:24 +0100 Subject: [PATCH 07/10] prepaired Tests for new Methods in JsonTaskDatabase --- .../zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java index e75ea40..d3710ab 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java @@ -34,4 +34,14 @@ public class JsonTaskDatabaseTest { Assertions.assertTrue(taskList.size()>0); */ } + + @Test + void getTaskForCrop() { + // TODO implement Test + } + + @Test + void removeTasksForCrop() { + // TODO implement Test + } } From eea530931eaedb69313b83ed4822c7caa6a4df7a Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 31 Oct 2022 21:01:25 +0100 Subject: [PATCH 08/10] created Some TaskListModelTests --- .../taskList/TaskListModelTest.java | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java diff --git a/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java new file mode 100644 index 0000000..76099a2 --- /dev/null +++ b/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java @@ -0,0 +1,134 @@ +package ch.zhaw.gartenverwaltung.taskList; + +import ch.zhaw.gartenverwaltung.io.*; +import ch.zhaw.gartenverwaltung.types.HardinessZone; +import ch.zhaw.gartenverwaltung.types.Plant; +import ch.zhaw.gartenverwaltung.types.Task; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.time.LocalDate; +import java.util.*; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class TaskListModelTest { + TaskDatabase taskDatabase; + PlantDatabase plantDatabase; + List exampleTaskList; + Map examplePlantMap; + TaskListModel model; + + @BeforeEach + void setUp() throws IOException { + createExampleTaskList(); + taskDatabase = mockTaskDatabase(exampleTaskList); + plantDatabase = mockPlantDatabase(examplePlantMap); + model = new TaskListModel(taskDatabase, plantDatabase); + } + + private TaskDatabase mockTaskDatabase(List exampleTaskList) throws IOException { + TaskDatabase taskDatabase = mock(JsonTaskDatabase.class); + when(taskDatabase.getTaskList(LocalDate.MIN, LocalDate.MAX)).thenReturn(exampleTaskList); + + when(taskDatabase.getTaskList(LocalDate.now(), LocalDate.MAX)).thenReturn((exampleTaskList.subList(1, 4))); + + List pastTasks = new ArrayList<>(); + pastTasks.add(exampleTaskList.get(0)); + pastTasks.add(exampleTaskList.get(2)); + pastTasks.add(exampleTaskList.get(4)); + when(taskDatabase.getTaskList(LocalDate.MIN, LocalDate.now())).thenReturn(pastTasks); + + return taskDatabase; + } + + private PlantDatabase mockPlantDatabase(Map examplePlantMap) { + return new PlantDatabase() { + @Override + public List getPlantList(HardinessZone zone) throws IOException, HardinessZoneNotSetException { + return null; + } + + @Override + public Optional getPlantById(HardinessZone zone, long id) throws IOException, HardinessZoneNotSetException { + return Optional.ofNullable(examplePlantMap.get(id)); + } + }; + } + + void createExampleTaskList() { + exampleTaskList = new ArrayList<>(); + exampleTaskList.add(new Task("name", "description", LocalDate.now().minusDays(1), 1L)); + exampleTaskList.add(new Task("name", "description", LocalDate.now().plusDays(1), 1L)); + exampleTaskList.add(new Task("name", "description", LocalDate.now(), 1L)); + exampleTaskList.add(new Task("name", "description", LocalDate.of(9019, 5, 5), 1L)); + exampleTaskList.add(new Task("name", "description", LocalDate.of(2019, 5, 5), 1L)); + } + + void createExamplePlantMap() { + examplePlantMap = new HashMap<>(); + } + + + + @Test + void addTask() throws IOException { + Task taskToAdd = new Task("name", "description", LocalDate.now(), 1L); + model.addTask(taskToAdd); + verify(taskDatabase, times(1)).saveTask(taskToAdd); + } + + @Test + void removeTask() throws IOException { + Task taskToRemove = new Task("name", "description", LocalDate.now(), 1L); + model.removeTask(taskToRemove); + verify(taskDatabase, times(1)).removeTask(taskToRemove); + } + + @Test + void getTaskList() throws IOException { + List listToCheck = model.getTaskList(); + assertEquals(5, listToCheck.size()); + assertEquals(exampleTaskList.get(4), listToCheck.get(0)); + assertEquals(exampleTaskList.get(0), listToCheck.get(1)); + assertEquals(exampleTaskList.get(2), listToCheck.get(2)); + assertEquals(exampleTaskList.get(1), listToCheck.get(3)); + assertEquals(exampleTaskList.get(3), listToCheck.get(4)); + } + + @Test + void getFutureTasks() throws IOException { + List listToCheck = model.getFutureTasks(); + assertEquals(3, listToCheck.size()); + assertEquals(exampleTaskList.get(2), listToCheck.get(0)); + assertEquals(exampleTaskList.get(1), listToCheck.get(1)); + assertEquals(exampleTaskList.get(3), listToCheck.get(2)); + } + + @Test + void getPastTasks() throws IOException { + List listToCheck = model.getPastTasks(); + assertEquals(3, listToCheck.size()); + assertEquals(exampleTaskList.get(4), listToCheck.get(0)); + assertEquals(exampleTaskList.get(0), listToCheck.get(1)); + assertEquals(exampleTaskList.get(2), listToCheck.get(2)); + } + + @Test + void getTasksUpcomingWeek() { + } + + @Test + void getFilteredTaskList() { + } + + @Test + void planTasksForCrop() { + } + + @Test + void removeTasksForCrop() { + } +} \ No newline at end of file From 38288f8561bbd51f835737c4e78c349428966b25 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 4 Nov 2022 14:20:58 +0100 Subject: [PATCH 09/10] finished Tests for TaskListModel --- .../taskList/TaskListModel.java | 13 ++--- .../taskList/TaskListModelTest.java | 52 ++++++++++++++----- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java index 4f9384d..430d14c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java @@ -9,6 +9,7 @@ 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.stream.Collectors; @@ -105,17 +106,17 @@ 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[]) + * 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>) * @throws IOException If the database cannot be accessed */ - public List[] getTasksUpcomingWeek() throws IOException { - List[] listArray = new List[7]; + public List> getTasksUpcomingWeek() throws IOException { + List> 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; } /** diff --git a/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java index 76099a2..9caf65a 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java @@ -41,18 +41,27 @@ class TaskListModelTest { pastTasks.add(exampleTaskList.get(4)); when(taskDatabase.getTaskList(LocalDate.MIN, LocalDate.now())).thenReturn(pastTasks); + + when(taskDatabase.getTaskList(LocalDate.now(), LocalDate.now())).thenReturn(List.of(exampleTaskList.get(2))); + when(taskDatabase.getTaskList(LocalDate.now().plusDays(1L), LocalDate.now().plusDays(1L))).thenReturn(List.of(exampleTaskList.get(1))); + when(taskDatabase.getTaskList(LocalDate.now().plusDays(2L), LocalDate.now().plusDays(2L))).thenReturn(List.of()); + when(taskDatabase.getTaskList(LocalDate.now().plusDays(3L), LocalDate.now().plusDays(3L))).thenReturn(List.of()); + when(taskDatabase.getTaskList(LocalDate.now().plusDays(4L), LocalDate.now().plusDays(4L))).thenReturn(List.of()); + when(taskDatabase.getTaskList(LocalDate.now().plusDays(5L), LocalDate.now().plusDays(5L))).thenReturn(List.of()); + when(taskDatabase.getTaskList(LocalDate.now().plusDays(6L), LocalDate.now().plusDays(6L))).thenReturn(List.of()); + return taskDatabase; } private PlantDatabase mockPlantDatabase(Map examplePlantMap) { return new PlantDatabase() { @Override - public List getPlantList(HardinessZone zone) throws IOException, HardinessZoneNotSetException { + public List getPlantList(HardinessZone zone) { return null; } @Override - public Optional getPlantById(HardinessZone zone, long id) throws IOException, HardinessZoneNotSetException { + public Optional getPlantById(HardinessZone zone, long id) { return Optional.ofNullable(examplePlantMap.get(id)); } }; @@ -61,10 +70,10 @@ class TaskListModelTest { void createExampleTaskList() { exampleTaskList = new ArrayList<>(); exampleTaskList.add(new Task("name", "description", LocalDate.now().minusDays(1), 1L)); - exampleTaskList.add(new Task("name", "description", LocalDate.now().plusDays(1), 1L)); + exampleTaskList.add(new Task("name", "description", LocalDate.now().plusDays(1), 2L)); exampleTaskList.add(new Task("name", "description", LocalDate.now(), 1L)); exampleTaskList.add(new Task("name", "description", LocalDate.of(9019, 5, 5), 1L)); - exampleTaskList.add(new Task("name", "description", LocalDate.of(2019, 5, 5), 1L)); + exampleTaskList.add(new Task("name", "description", LocalDate.of(2019, 5, 5), 2L)); } void createExamplePlantMap() { @@ -117,18 +126,33 @@ class TaskListModelTest { } @Test - void getTasksUpcomingWeek() { + void getTasksUpcomingWeek() throws IOException { + List> dayList = model.getTasksUpcomingWeek(); + assertEquals(7, dayList.size()); + + //Check day 0 + assertEquals(1, dayList.get(0).size()); + assertEquals(exampleTaskList.get(2), dayList.get(0).get(0)); + + //Check day 1 + assertEquals(1, dayList.get(1).size()); + assertEquals(exampleTaskList.get(1), dayList.get(1).get(0)); + + //Check day 2 + assertEquals(0, dayList.get(2).size()); + //Check day 3 + assertEquals(0, dayList.get(3).size()); + //Check day 4 + assertEquals(0, dayList.get(4).size()); + //Check day 5 + assertEquals(0, dayList.get(5).size()); + //Check day 6 + assertEquals(0, dayList.get(6).size()); } @Test - void getFilteredTaskList() { - } - - @Test - void planTasksForCrop() { - } - - @Test - void removeTasksForCrop() { + void removeTasksForCrop() throws IOException { + model.removeTasksForCrop(1L); + verify(taskDatabase, times(1)).removeTasksForCrop(1L); } } \ No newline at end of file From b7d08944a6d6128391d6d8dba90cd26e8e5ad702 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 6 Nov 2022 17:29:21 +0100 Subject: [PATCH 10/10] added new Methods to filter List by Crop ID --- .../taskList/TaskListModel.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java index 430d14c..cb766be 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java @@ -12,6 +12,7 @@ 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 { @@ -78,6 +79,10 @@ public class TaskListModel { taskDatabase.removeTask(task); } + private List filterListByCrop(List taskList, Long cropId) { + return taskList.stream().filter(task -> task.getCropId() == cropId).collect(Collectors.toList()); + } + /** * Method to get all Tasks * @return List of all Tasks @@ -87,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 getTaskListForCrop(Long cropId) throws IOException { + return filterListByCrop(getTaskList(), cropId); + } + /** * Method to get all Tasks which are today or in future * @return List of Tasks @@ -96,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 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,6 +128,15 @@ public class TaskListModel { return getFilteredTaskList(LocalDate.MIN, LocalDate.now()); } + /** + * 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 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>) @@ -119,6 +151,20 @@ public class TaskListModel { 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>) + * @throws IOException If the database cannot be accessed + */ + public List> getTasksUpcomingWeekForCrop(Long cropId) throws IOException { + List> 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; + } + /** * Method to get Tasklist filtered by date. * @param start the start date for the filter