From 5bfebdc92c7ef93d541aa3cd49c3dc70a614101b Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 31 Oct 2022 16:23:49 +0100 Subject: [PATCH] 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);