From 4072308ae64259e67b0f2d0966f50467c2079e9d Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Sun, 27 Nov 2022 10:13:11 +0100 Subject: [PATCH] #23 Implementation of Gardenschedule for wateringtask adjustment --- .../gartenverwaltung/io/SevereWeather.java | 2 +- .../io/WeatherGradenTaskPlanner.java | 43 ++++++++++++++++--- .../gartenverwaltung/io/WeatherService.java | 15 +++++++ .../models/GardenSchedule.java | 2 + 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java b/src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java index d76036f..239e34a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/SevereWeather.java @@ -1,5 +1,5 @@ package ch.zhaw.gartenverwaltung.io; public enum SevereWeather { - FROST,SNOW,HAIL,NO_SEVERE_WEATHER + FROST,SNOW,HAIL,NO_SEVERE_WEATHER,RAIN } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java index 1d19cd9..1e13b74 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherGradenTaskPlanner.java @@ -1,7 +1,7 @@ package ch.zhaw.gartenverwaltung.io; -import ch.zhaw.gartenverwaltung.types.Crop; -import ch.zhaw.gartenverwaltung.types.Task; +import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; +import ch.zhaw.gartenverwaltung.types.*; import java.io.IOException; import java.time.LocalDate; @@ -9,15 +9,24 @@ import java.util.List; public class WeatherGradenTaskPlanner { private final TaskList taskList; + private final PlantList plantList; + private final CropList cropList; WeatherService weatherService; private final LocalDate dateSevereWeather = LocalDate.of(22,12,15); - public WeatherGradenTaskPlanner(TaskList taskList) { + public WeatherGradenTaskPlanner(TaskList taskList, PlantList plantList, CropList cropList) { this.taskList = taskList; + this.plantList = plantList; + this.cropList = cropList; weatherService = new WeatherService(); } - public void getWeatherEvents() throws IOException { + public void refreshTasks() throws IOException, HardinessZoneNotSetException, PlantNotFoundException { + getSevereWeatherEvents(); + getRainAmount(); + } + + private void getSevereWeatherEvents() throws IOException { SevereWeather actualWeather = weatherService.causeSevereWeather(0); if (SevereWeather.HAIL.equals(actualWeather)) { createPreHail(); @@ -28,6 +37,11 @@ public class WeatherGradenTaskPlanner { } } + private void getRainAmount() throws IOException, HardinessZoneNotSetException, PlantNotFoundException { + int rainAmount = weatherService.causeRainAmount(3); + adjustWateringTask(rainAmount); + } + public void createPreHail() throws IOException { Task preHailTask = new Task("Hail", @@ -54,11 +68,26 @@ public class WeatherGradenTaskPlanner { taskList.saveTask(preSnowTask); } - public void calculateRainAmount(){ + public void adjustWateringTask(int rainAmount) throws HardinessZoneNotSetException, IOException, PlantNotFoundException { + + for (Crop crop : cropList.getCrops()) { + Plant plant = plantList.getPlantById(HardinessZone.ZONE_8A,crop.getPlantId()).orElseThrow(PlantNotFoundException::new); + + for (GrowthPhase growthphase : plant.lifecycle()) { + + if(growthphase.wateringCycle().litersPerSqM() < rainAmount){ + adjustNextExecutionOfWateringTask(taskList.getTaskList(LocalDate.now(), LocalDate.now().plusDays(7))); + } + } + } } - public void adjustWateringTask(List crops){ - + private void adjustNextExecutionOfWateringTask(List cropTaskList){ + for(Task task : cropTaskList){ + if(task.getName().equals("watering task")){ + task.setNextExecution(task.getNextExecution().plusDays(task.getInterval().orElse(1))); + } + } } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java index a82177c..510b292 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/WeatherService.java @@ -1,6 +1,10 @@ package ch.zhaw.gartenverwaltung.io; public class WeatherService { + private static final int NO_RAIN = 0; + private static final int LITTLE_RAIN = 15; + private static final int RAIN = 25; + private static final int HEAVY_RAIN = 50; public SevereWeather causeSevereWeather(int randomWeather) { return switch (randomWeather) { @@ -11,4 +15,15 @@ public class WeatherService { }; } + + public int causeRainAmount(int randomRainAmount) { + return switch (randomRainAmount) { + case 1 -> NO_RAIN; + case 2 -> LITTLE_RAIN; + case 3 -> RAIN; + case 4 -> HEAVY_RAIN; + default -> -1; + }; + + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java index c3292f6..dde9dec 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java @@ -52,7 +52,9 @@ public class GardenSchedule { for (GrowthPhase growthPhase : plant.lifecycleForGroup(growPhaseGroup)) { for (TaskTemplate taskTemplate : growthPhase.taskTemplates()) { addTask(taskTemplate.generateTask(crop.getStartDate(), crop.getCropId().orElse(0L))); + //TODO add wateringtask } + //growthPhase.wateringCycle() } }