diff --git a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java index 60ff2ae..e3722fc 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java @@ -7,7 +7,6 @@ import javafx.scene.control.*; import javafx.util.Callback; import java.time.LocalDate; -import java.time.MonthDay; public class SelectSowDayController { private Plant selectedPlant; @@ -17,12 +16,15 @@ public class SelectSowDayController { @FXML private RadioButton harvest_radio; + @FXML + private RadioButton sow_radio; + @FXML + public ToggleGroup phase_group; public LocalDate retrieveResult() { LocalDate sowDate = datepicker.getValue(); if (harvest_radio.isSelected()) { - //ToDo method to get current lifecycle group in plant - sowDate = selectedPlant.sowDateFromHarvestDate(datepicker.getValue(), 0); + sowDate = selectedPlant.sowDateFromHarvestDate(sowDate); } return sowDate; } @@ -46,6 +48,9 @@ public class SelectSowDayController { Callback dayCellFactory = getDayCellFactory(); datepicker.setDayCellFactory(dayCellFactory); datepicker.setEditable(false); + + sow_radio.setUserData(GrowthPhaseType.SOW); + harvest_radio.setUserData(GrowthPhaseType.HARVEST); } public void initSaveButton(Button saveButton) { @@ -75,12 +80,10 @@ public class SelectSowDayController { setDisable(true); setStyle("-fx-background-color: #ffc0cb;"); - 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 (item.compareTo(today) > 0 && (!harvest_radio.isSelected() || selectedPlant.sowDateFromHarvestDate(item).compareTo(today) >= 0)) { + GrowthPhaseType selectedPhase = (GrowthPhaseType) phase_group.getSelectedToggle().getUserData(); - if (dateInRange(item, minDate, maxDate)) { + if (selectedPlant.isDateInPhase(item, selectedPhase)) { setDisable(false); setStyle("-fx-background-color: #32CD32;"); } @@ -88,22 +91,4 @@ public class SelectSowDayController { } }; } - - /** - * 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 16bd061..1cf6466 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java @@ -42,11 +42,10 @@ public record Plant( /** * get sow date from given harvest day from lifecycle group * @param harvestDate date of the harvest - * @param group lifecycle group * @return LocaleDate of sow date */ - public LocalDate sowDateFromHarvestDate(LocalDate harvestDate, int group) { - return harvestDate.minusDays(timeToHarvest(group)); + public LocalDate sowDateFromHarvestDate(LocalDate harvestDate) { + return harvestDate.minusDays(timeToHarvest(lifecycleGroupFromHarvestDate(harvestDate))); } /** @@ -69,19 +68,43 @@ public record Plant( return (int) DAYS.between(harvest.startDate().atYear(currentYear), sow.startDate().atYear(currentYear)); } - - public MonthDay getMaxDateForGrowthPhase(GrowthPhaseType growthPhase) { + public int lifecycleGroupFromHarvestDate(LocalDate harvestDate) { return lifecycle.stream() - .filter(phase -> phase.type().equals(growthPhase)) + .filter(growthPhase -> growthPhase.type().equals(GrowthPhaseType.HARVEST) && + dateInRange(harvestDate, growthPhase.startDate(), growthPhase.endDate())) + .map(GrowthPhase::group) .findFirst() - .map(GrowthPhase::endDate) - .orElse(MonthDay.of(12, 31)); + .orElse(0); } - public MonthDay getMinDateForGrowthPhase(GrowthPhaseType growthPhase) { + + /** + * Checks if the given {@link LocalDate} is within a {@link GrowthPhase} of the given {@link GrowthPhaseType} + * + * @param date The date to check. + * @param phase The {@link GrowthPhaseType} to match against + * @return Whether the date is within the given {@link GrowthPhaseType} + */ + public boolean isDateInPhase(LocalDate date, GrowthPhaseType phase) { return lifecycle.stream() - .filter(phase -> phase.type().equals(growthPhase)) - .findFirst() - .map(GrowthPhase::startDate) - .orElse(MonthDay.of(1, 1)); + .filter(growthPhase -> growthPhase.type().equals(phase)) + .anyMatch(growthPhase -> dateInRange(date, growthPhase.startDate(), growthPhase.endDate())); + } + + /** + * 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/resources/ch/zhaw/gartenverwaltung/SelectSowDay.fxml b/src/main/resources/ch/zhaw/gartenverwaltung/SelectSowDay.fxml index 75f33c3..1ebcf03 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/SelectSowDay.fxml +++ b/src/main/resources/ch/zhaw/gartenverwaltung/SelectSowDay.fxml @@ -18,15 +18,15 @@ - + - + - +