From 25057d34f0a9d38e54e5f36974bd15f3b477311b Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 31 Oct 2022 08:06:35 +0100 Subject: [PATCH] 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()); + } +}