diff --git a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java index bf5a7bf..7051bfa 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java @@ -93,23 +93,27 @@ public class CropDetailController { } - public void setPlantFromCrop(Crop crop) throws HardinessZoneNotSetException, IOException, PlantNotFoundException { + public void setPlantFromCrop(Crop crop) throws PlantNotFoundException { this.crop = crop; - Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()) - .orElseThrow(PlantNotFoundException::new); + try { + Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()) + .orElseThrow(PlantNotFoundException::new); - cropName_label.setText(plant.name()); - description_label.setText(plant.description()); - light_label.setText(String.valueOf(plant.light())); - soil_label.setText(plant.soil()); - spacing_label.setText(plant.spacing()); - if (plant.image() != null) { - imageView.setImage(plant.image()); + cropName_label.setText(plant.name()); + description_label.setText(plant.description()); + light_label.setText(String.valueOf(plant.light())); + soil_label.setText(plant.soil()); + spacing_label.setText(plant.spacing()); + if (plant.image() != null) { + imageView.setImage(plant.image()); + } + area_label.setText(""); + location_label.setText(""); + createTaskLists(crop); + createPestList(plant); + } catch (HardinessZoneNotSetException | IOException e) { + throw new PlantNotFoundException(); } - area_label.setText(""); - location_label.setText(""); - createTaskLists(crop); - createPestList(plant); } private void createTaskLists(Crop crop) { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java index 3568b50..3901377 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java @@ -108,7 +108,7 @@ public class MyGardenController { stage.initModality(Modality.APPLICATION_MODAL); stage.setResizable(true); stage.showAndWait(); - } catch (IOException | HardinessZoneNotSetException | PlantNotFoundException e) { + } catch (IOException | PlantNotFoundException e) { // TODO: show error alert LOG.log(Level.SEVERE, "Could not load plant details.", e); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java index 67338be..60ff2ae 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java @@ -7,7 +7,7 @@ import javafx.scene.control.*; import javafx.util.Callback; import java.time.LocalDate; -import java.util.List; +import java.time.MonthDay; public class SelectSowDayController { private Plant selectedPlant; @@ -61,36 +61,49 @@ public class SelectSowDayController { /** * date picker disable/enable dates according to selected plant: sow or harvest day + * * @return cellFactory of datePicker */ private Callback getDayCellFactory() { return (datePicker) -> new DateCell() { + private final LocalDate today = LocalDate.now(); + @Override public void updateItem(LocalDate item, boolean empty) { super.updateItem(item, empty); setDisable(true); setStyle("-fx-background-color: #ffc0cb;"); - List dates; - LocalDate today = LocalDate.now(); - if (harvest_radio.isSelected()) { - dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.HARVEST); - } else { - dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.SOW); - } - for (LocalDate date : dates) { - if (item.getMonth() == date.getMonth() - && item.getDayOfMonth() == date.getDayOfMonth() - && item.compareTo(today) > 0) { + + if (item.compareTo(today) > 0 && (!harvest_radio.isSelected() || selectedPlant.sowDateFromHarvestDate(item, 0).compareTo(today) >= 0)) { + GrowthPhaseType selectedPhase = harvest_radio.isSelected() ? GrowthPhaseType.HARVEST : GrowthPhaseType.SOW; + MonthDay minDate = selectedPlant.getMinDateForGrowthPhase(selectedPhase); + MonthDay maxDate = selectedPlant.getMaxDateForGrowthPhase(selectedPhase); + + if (dateInRange(item, minDate, maxDate)) { setDisable(false); setStyle("-fx-background-color: #32CD32;"); } } - if ((harvest_radio.isSelected() && selectedPlant.sowDateFromHarvestDate(item, 0).compareTo(today) < 0)) { - setDisable(true); - setStyle("-fx-background-color: #ffc0cb;"); - } } }; } + + /** + * Checks if the given {@link LocalDate} is within the given {@link MonthDay} range. + * (regardless of year) + * + * @param subject The date to check + * @param min The start of the date-range + * @param max The end of the date-range + * @return Whether the subject is within the range. + */ + private boolean dateInRange(LocalDate subject, MonthDay min, MonthDay max) { + return subject.getMonth().compareTo(min.getMonth()) >= 0 && + subject.getMonth().compareTo(max.getMonth()) <= 0 && + // if the day is less than the minimum day, the minimum month must not be equal + (subject.getDayOfMonth() >= min.getDayOfMonth() || !subject.getMonth().equals(min.getMonth())) && + // if the day is greater than the maximum day, the maximum month must not be equal + (subject.getDayOfMonth() <= max.getDayOfMonth() || !subject.getMonth().equals(max.getMonth())); + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java index 9cb3d08..16bd061 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java @@ -3,7 +3,7 @@ package ch.zhaw.gartenverwaltung.types; import javafx.scene.image.Image; import java.time.LocalDate; -import java.util.LinkedList; +import java.time.MonthDay; import java.util.List; import java.util.stream.Collectors; @@ -69,37 +69,19 @@ public record Plant( return (int) DAYS.between(harvest.startDate().atYear(currentYear), sow.startDate().atYear(currentYear)); } - /** - * filter out the given growthPhase out of the lifecycle - * create list of dates for this growthPhase and return it - * @param growthPhase the wanted growthPhase - * @return a list of dates of the current year - */ - public List getDateListOfGrowthPhase(GrowthPhaseType growthPhase) { - List dates = new LinkedList<>(); - for (GrowthPhase growth : lifecycle) { - if (growth.type().equals(growthPhase)) { - dates = addDatesFromMonthDay(growth); - } - } - return dates; - } - /** - * transform monthDay value of the given growthPhase to localDate - * return a list of dates from start to end of growth phase - * @param growthPhase the current growthPhase - * @return a list of dates of the current year - */ - private List addDatesFromMonthDay(GrowthPhase growthPhase) { - List dates = new LinkedList<>(); - LocalDate today = LocalDate.now(); - LocalDate start = growthPhase.startDate().atYear(today.getYear()); - LocalDate end = growthPhase.endDate().atYear(today.getYear()); - while (!start.isAfter(end)) { - dates.add(start); - start = start.plusDays(1); - } - return dates; + public MonthDay getMaxDateForGrowthPhase(GrowthPhaseType growthPhase) { + return lifecycle.stream() + .filter(phase -> phase.type().equals(growthPhase)) + .findFirst() + .map(GrowthPhase::endDate) + .orElse(MonthDay.of(12, 31)); + } + public MonthDay getMinDateForGrowthPhase(GrowthPhaseType growthPhase) { + return lifecycle.stream() + .filter(phase -> phase.type().equals(growthPhase)) + .findFirst() + .map(GrowthPhase::startDate) + .orElse(MonthDay.of(1, 1)); } }