created Class TaskListModel

This commit is contained in:
schrom01 2022-10-31 08:06:35 +01:00
parent f452b73233
commit 25057d34f0
1 changed files with 64 additions and 0 deletions

View File

@ -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<Task> 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<Task> getTaskList() throws IOException {
return getFilteredTaskList(LocalDate.MIN, LocalDate.MAX);
}
public List<Task> getFutureTasks() throws IOException {
return getFilteredTaskList(LocalDate.now(), LocalDate.MAX);
}
public List<Task> getPastTasks() throws IOException {
return getFilteredTaskList(LocalDate.MIN, LocalDate.now());
}
public List<Task>[] getTasksUpcomingWeek() throws IOException {
List<Task>[] 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<Task> getFilteredTaskList(LocalDate start, LocalDate end) throws IOException {
return getSortedTaskList(taskDatabase.getTaskList(start, end), sortByStartDate);
}
private List<Task> getSortedTaskList(List<Task> taskList, Comparator<Task> comparator) {
return taskList.stream().sorted(comparator).collect(Collectors.toList());
}
}