implemented Methods

removeTasksForCrop and getTaskForCrop
in JsonTaskDatabase
This commit is contained in:
schrom01 2022-10-31 16:23:49 +01:00
parent 0e4e207581
commit 5bfebdc92c
4 changed files with 59 additions and 9 deletions

View File

@ -52,14 +52,29 @@ public class JsonTaskDatabase implements TaskDatabase{
return taskMap.values().stream().filter(task -> task.isInTimePeriode(start, end)).toList(); 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 @Override
public List<Task> getTaskForCrop(Crop crop) { public List<Task> getTaskForCrop(long cropId) throws IOException {
return null; //TODO implement 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 @Override
public void removeTasksForCrop(Crop crop) { public void removeTasksForCrop(long cropId) throws IOException {
// TODO implement if(taskMap.isEmpty()) {
loadTaskListFromFile();
}
taskMap.values().removeIf(task -> task.getCropId() == cropId);
} }
/** /**

View File

@ -25,9 +25,20 @@ public interface TaskDatabase {
*/ */
List<Task> getTaskList(LocalDate start, LocalDate end) throws IOException; List<Task> getTaskList(LocalDate start, LocalDate end) throws IOException;
List<Task> 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<Task> 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. * Saves the {@link Task} in the Cache.

View File

@ -17,21 +17,39 @@ public class TaskListModel {
private TaskDatabase taskDatabase; private TaskDatabase taskDatabase;
private PlantDatabase plantDatabase; private PlantDatabase plantDatabase;
/**
* Comparators to create sorted Task List
*/
static final Comparator<Task> sortByStartDate = Comparator.comparing(Task::getStartDate); static final Comparator<Task> sortByStartDate = Comparator.comparing(Task::getStartDate);
public TaskListModel(){ public TaskListModel(){
taskDatabase = new JsonTaskDatabase(); taskDatabase = new JsonTaskDatabase();
} }
/**
* Constructor to create Database Objects.
*/
public TaskListModel(TaskDatabase taskDatabase, PlantDatabase plantDatabase) { public TaskListModel(TaskDatabase taskDatabase, PlantDatabase plantDatabase) {
this.taskDatabase = taskDatabase; this.taskDatabase = taskDatabase;
this.plantDatabase = plantDatabase; 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 { public void addTask(Task task) throws IOException {
taskDatabase.saveTask(task); 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 { public void planTasksForCrop(Crop crop) throws PlantNotFoundException, HardinessZoneNotSetException, IOException {
Plant plant = plantDatabase.getPlantById(Config.getCurrentHardinessZone(), crop.getPlantId()).orElseThrow(PlantNotFoundException::new); Plant plant = plantDatabase.getPlantById(Config.getCurrentHardinessZone(), crop.getPlantId()).orElseThrow(PlantNotFoundException::new);
for (GrowthPhase growthPhase : plant.lifecycle()) { 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 { public void removeTask(Task task) throws IOException {

View File

@ -35,12 +35,13 @@ public class Task {
this.cropId = cropId; 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.name = name;
this.description = description; this.description = description;
this.startDate = startDate; this.startDate = startDate;
this.endDate = endDate; this.endDate = endDate;
this.interval = interval; this.interval = interval;
this.cropId = cropId;
} }
// Builder-pattern-style setters // Builder-pattern-style setters
@ -66,6 +67,7 @@ public class Task {
public String getName() { return name; } public String getName() { return name; }
public String getDescription() { return description; } public String getDescription() { return description; }
public LocalDate getStartDate() { return startDate; } public LocalDate getStartDate() { return startDate; }
public long getCropId() { return cropId; }
public Optional<Integer> getInterval() { public Optional<Integer> getInterval() {
return Optional.ofNullable(interval); return Optional.ofNullable(interval);