diff --git a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java index e5e80f4..2537e08 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java @@ -31,31 +31,50 @@ public class SelectSowDayController implements Initializable { @FXML private RadioButton sow_radio; + /** + * close the date selector window + * @param event event + */ @FXML void cancel(ActionEvent event) { closeWindow(); } + /** + * get sow date from datePicker or calculate sow date from harvest date + * save selected plant and sow date + * @param event event + */ @FXML void save(ActionEvent event) { //ToDo save plant and sow date to crop/gardenplan model - System.out.println(selectedPlant); + LocalDate sowDate; if (sow_radio.isSelected()) { - System.out.println(datepicker.getValue()); + sowDate = datepicker.getValue(); } else { //ToDo method to get current lifecycle group in plant - //ToDo error when - //what's up with that group thing in life_cycle we have hardiness zone for that - System.out.println(selectedPlant.sowDateFromHarvestDate(datepicker.getValue(), 0)); + sowDate = selectedPlant.sowDateFromHarvestDate(datepicker.getValue(), 0); } + System.out.println(sowDate); + System.out.println(selectedPlant); closeWindow(); } + /** + * save the plant which will be planted and update label + * @param plant Plant + */ public void getSelectedPlant(Plant plant) { selectedPlant = plant; popup_label.setText("Select Harvest/Sow Date for" + selectedPlant.name()); } + /** + * add listener and set default values + * {@inheritDoc} + * @param location location + * @param resources resources + */ @Override public void initialize(URL location, ResourceBundle resources) { clearDatePickerEntries(); @@ -67,6 +86,9 @@ public class SelectSowDayController implements Initializable { enableDisableSaveButton(); } + /** + * clear date picker editor when radio button is changed + */ private void clearDatePickerEntries() { sow_radio.selectedProperty().addListener(new ChangeListener() { @Override @@ -76,6 +98,10 @@ public class SelectSowDayController implements Initializable { }); } + /** + * date picker disable/enable dates according to selected plant: sow or harvest day + * @return cellFactory of datePicker + */ private Callback getDayCellFactory() { final Callback dayCellFactory = new Callback() { @@ -103,6 +129,10 @@ public class SelectSowDayController implements Initializable { setStyle("-fx-background-color: #32CD32;"); } } + if ((!sow_radio.isSelected() && selectedPlant.sowDateFromHarvestDate(item, 0).compareTo(today) < 0)) { + setDisable(true); + setStyle("-fx-background-color: #ffc0cb;"); + } } }; } @@ -110,11 +140,17 @@ public class SelectSowDayController implements Initializable { return dayCellFactory; } + /** + * close date picker window + */ private void closeWindow() { Stage stage = (Stage) save_button.getScene().getWindow(); stage.close(); } + /** + * disable save button, when there is no date selected in date picker + */ private void enableDisableSaveButton() { save_button.setDisable(true); datepicker.getEditor().textProperty().addListener(new ChangeListener() { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java index 2a2bc0b..9cb3d08 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java @@ -20,20 +20,40 @@ public record Plant( List pests, List lifecycle) { + /** + * remove all growthPhase which do not belong to the hardiness zone + * @param zone hardiness zone + */ public void inZone(HardinessZone zone) { lifecycle.removeIf(growthPhase -> !growthPhase.zone().equals(zone)); } + /** + * get all growthPhases of lifecycle group + * @param group lifecycle group + * @return list of growthPhases + */ public List lifecycleForGroup(int group) { return lifecycle.stream() - .filter(growthPhase -> growthPhase.group() != group) + .filter(growthPhase -> growthPhase.group() == group) .collect(Collectors.toList()); } + /** + * 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)); } + /** + * calculate the days between sow and harvest day for lifecycle group + * @param group the lifecycle group + * @return Integer number of dates between sow and harvest day + */ public int timeToHarvest(int group) { List activeLifecycle = lifecycleForGroup(group); GrowthPhase sow = activeLifecycle.stream() @@ -49,6 +69,12 @@ 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) { @@ -56,12 +82,15 @@ public record Plant( dates = addDatesFromMonthDay(growth); } } - if (growthPhase.equals(GrowthPhaseType.HARVEST)) { - return removeDatesForPassedSowDates(dates); - } 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(); @@ -73,9 +102,4 @@ public record Plant( } return dates; } - - private List removeDatesForPassedSowDates(List dates) { - //ToDo Remove harvest dates where sow dates have already passed current day - return dates; - } } diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json index 5a23d09..5ee6331 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json @@ -94,6 +94,7 @@ "endDate": "03-10", "zone": "ZONE_8A", "type": "SOW", + "group": 0, "wateringCycle": { "litersPerSqM": 15, "interval": 3, @@ -115,6 +116,7 @@ "endDate": "05-10", "zone": "ZONE_8A", "type": "PLANT", + "group": 0, "wateringCycle": { "litersPerSqM": 25, "interval": 3, @@ -138,6 +140,7 @@ "endDate": "05-20", "zone": "ZONE_8A", "type": "HARVEST", + "group": 0, "wateringCycle": { "litersPerSqM": 0, "interval": null,