From b7d08944a6d6128391d6d8dba90cd26e8e5ad702 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 6 Nov 2022 17:29:21 +0100 Subject: [PATCH] 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