#23 refactoring of the WeatherGradenTaskPlanner

This commit is contained in:
Gian-Andrea Hutter 2022-12-11 16:16:06 +01:00
parent dbba4e2662
commit a20edae4b8
1 changed files with 55 additions and 69 deletions

View File

@ -10,11 +10,8 @@ import ch.zhaw.gartenverwaltung.types.*;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import static java.util.stream.Collectors.toList;
/** /**
* The WeatherGardenTaskPlanner creates Tasks based on weather events and the rain amount from the last days * The WeatherGardenTaskPlanner creates Tasks based on weather events and the rain amount from the last days
*/ */
@ -45,12 +42,19 @@ public class WeatherGradenTaskPlanner {
private void getSevereWeatherEvents() throws IOException { private void getSevereWeatherEvents() throws IOException {
SevereWeather actualWeather = weatherService.causeSevereWeather(1); SevereWeather actualWeather = weatherService.causeSevereWeather(1);
if (SevereWeather.HAIL.equals(actualWeather)) {
createPreHailTask(); List<Crop> actualCrops = cropList.getCrops();
} else if (SevereWeather.FROST.equals(actualWeather)) { for (Crop crop : actualCrops) {
createPreFrostTask();
} else if (SevereWeather.SNOW.equals(actualWeather)) { List<Task> actualCropTasks = taskList.getTaskForCrop(crop.getCropId().orElse(-1L));
createPreSnowTask();
if (SevereWeather.HAIL.equals(actualWeather)) {
createPreHailTask(crop, actualCropTasks);
} else if (SevereWeather.FROST.equals(actualWeather)) {
createPreFrostTask(crop, actualCropTasks);
} else if (SevereWeather.SNOW.equals(actualWeather)) {
createPreSnowTask(crop, actualCropTasks);
}
} }
} }
@ -60,87 +64,69 @@ public class WeatherGradenTaskPlanner {
} }
/** /**
* Method to create a PreHailTask * Method to create a PreHailTask and saves it in the tasklist
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
private void createPreHailTask() throws IOException { private void createPreHailTask(Crop crop, List<Task> actualTasksForCrop) throws IOException {
Task preHailTask = new Task("Hail",
"During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin",
dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L));
List<Crop> actualCrops = cropList.getCrops(); List<Task> hailTasks = actualTasksForCrop.stream().filter(task -> task.getName().equals("Hail")).toList();
for (Crop crop : actualCrops) { if(isNoSevereWeatherTaskAtDate(preHailTask, hailTasks) && hailTasks.isEmpty()){
Task preHailTask = new Task("Hail", taskList.saveTask(preHailTask);
"During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin",
dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L));
List<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L));
List<Task> hailTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Hail")).toList();
List<Task> hailTaskListAtDate = new ArrayList<>();
for (Task task : hailTasklist) {
if (task.getStartDate() == (preHailTask.getStartDate())) {
hailTaskListAtDate.add(task);
}
}
if(hailTaskListAtDate.isEmpty() && hailTasklist.isEmpty()){
taskList.saveTask(preHailTask);
}
} }
} }
/** /**
* Method to create a PreFrosttask * Method to create a PreFrosttask and saves it in the tasklist
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
private void createPreFrostTask() throws IOException { private void createPreFrostTask(Crop crop, List<Task> actualTasksForCrop) throws IOException {
List<Crop> actualCrops = cropList.getCrops(); Task preFrostTask = new Task("Frost",
for (Crop crop : actualCrops) {
Task preFrostTask = new Task("Frost",
"The temperatur falls below zero degrees, cover especially the root with wool", "The temperatur falls below zero degrees, cover especially the root with wool",
dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L)); dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L));
List<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L)); List<Task> frostTasks = actualTasksForCrop.stream().filter(task -> task.getName().equals("Frost")).toList();
List<Task> frostTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Frost")).toList();
List<Task> frostTaskListAtDate = new ArrayList<>(); if(isNoSevereWeatherTaskAtDate(preFrostTask, frostTasks) && frostTasks.isEmpty()){
for (Task task : frostTasklist) { taskList.saveTask(preFrostTask);
if (task.getStartDate() == preFrostTask.getStartDate()) { }
frostTaskListAtDate.add(task); }
}
} /**
if(frostTaskListAtDate.isEmpty() && frostTasklist.isEmpty()){ * Method to create a PreSnowTask and saves it in the tasklist
taskList.saveTask(preFrostTask); * @throws IOException If the database cannot be accessed
} */
private void createPreSnowTask(Crop crop, List<Task> actualTasksForCrop) throws IOException {
Task preSnowTask = new Task("Snow",
"The weather brings little snowfall. Cover your crops",
dateSevereWeather, dateSevereWeather.plusDays(1L), crop.getCropId().orElse(-1L));
List<Task> snowTasklist = actualTasksForCrop.stream().filter(task -> task.getName().equals("Snow")).toList();
if(isNoSevereWeatherTaskAtDate(preSnowTask, snowTasklist) && snowTasklist.isEmpty()){
taskList.saveTask(preSnowTask);
} }
} }
/** /**
* Method to create a PreSnowTask * Method to create a PreSnowTask and saves it in the tasklist
* @throws IOException If the database cannot be accessed * @param preSevereWeatherTask the Task which would be added if there is not already
* the same Type of severe weather task
* @param severeWeatherTasks List of severe weather tasks from e specific severe weather
* @return true If there is not already a severe weather task of the same type of preSevereWeatherTask
* task at the date of the preSevereWeatherTask
*/ */
private void createPreSnowTask() throws IOException { private boolean isNoSevereWeatherTaskAtDate(Task preSevereWeatherTask, List<Task> severeWeatherTasks) {
List<Crop> actualCrops = cropList.getCrops(); List<Task> severeWeatherTasksAtDate = new ArrayList<>();
for (Crop crop : actualCrops) { for (Task task : severeWeatherTasks) {
if (task.getStartDate() == preSevereWeatherTask.getStartDate()) {
Task preSnowTask = new Task("Snow", severeWeatherTasksAtDate.add(task);
"The weather brings little snowfall. Cover your crops",
dateSevereWeather, dateSevereWeather.plusDays(1L), crop.getCropId().orElse(-1L));
List<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L));
List<Task> snowTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Snow")).toList();
List<Task> snowTaskListAtDate = new ArrayList<>();
for (Task task : snowTasklist) {
if (task.getStartDate() == preSnowTask.getStartDate()) {
snowTaskListAtDate.add(task);
}
} }
if(snowTaskListAtDate .isEmpty() && snowTasklist.isEmpty()){
taskList.saveTask(preSnowTask);
}
} }
return severeWeatherTasksAtDate.isEmpty();
} }
/** /**