#53 fixed bug get sow date from harvest date + javadoc Plant and SelectSowDayController

This commit is contained in:
giavaphi 2022-11-05 16:50:18 +01:00
parent efc95dbcc6
commit 2cf7f55215
3 changed files with 77 additions and 14 deletions

View File

@ -31,31 +31,50 @@ public class SelectSowDayController implements Initializable {
@FXML @FXML
private RadioButton sow_radio; private RadioButton sow_radio;
/**
* close the date selector window
* @param event event
*/
@FXML @FXML
void cancel(ActionEvent event) { void cancel(ActionEvent event) {
closeWindow(); closeWindow();
} }
/**
* get sow date from datePicker or calculate sow date from harvest date
* save selected plant and sow date
* @param event event
*/
@FXML @FXML
void save(ActionEvent event) { void save(ActionEvent event) {
//ToDo save plant and sow date to crop/gardenplan model //ToDo save plant and sow date to crop/gardenplan model
System.out.println(selectedPlant); LocalDate sowDate;
if (sow_radio.isSelected()) { if (sow_radio.isSelected()) {
System.out.println(datepicker.getValue()); sowDate = datepicker.getValue();
} else { } else {
//ToDo method to get current lifecycle group in plant //ToDo method to get current lifecycle group in plant
//ToDo error when sowDate = selectedPlant.sowDateFromHarvestDate(datepicker.getValue(), 0);
//what's up with that group thing in life_cycle we have hardiness zone for that
System.out.println(selectedPlant.sowDateFromHarvestDate(datepicker.getValue(), 0));
} }
System.out.println(sowDate);
System.out.println(selectedPlant);
closeWindow(); closeWindow();
} }
/**
* save the plant which will be planted and update label
* @param plant Plant
*/
public void getSelectedPlant(Plant plant) { public void getSelectedPlant(Plant plant) {
selectedPlant = plant; selectedPlant = plant;
popup_label.setText("Select Harvest/Sow Date for" + selectedPlant.name()); popup_label.setText("Select Harvest/Sow Date for" + selectedPlant.name());
} }
/**
* add listener and set default values
* {@inheritDoc}
* @param location location
* @param resources resources
*/
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
clearDatePickerEntries(); clearDatePickerEntries();
@ -67,6 +86,9 @@ public class SelectSowDayController implements Initializable {
enableDisableSaveButton(); enableDisableSaveButton();
} }
/**
* clear date picker editor when radio button is changed
*/
private void clearDatePickerEntries() { private void clearDatePickerEntries() {
sow_radio.selectedProperty().addListener(new ChangeListener<Boolean>() { sow_radio.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override @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<DatePicker, DateCell> getDayCellFactory() { private Callback<DatePicker, DateCell> getDayCellFactory() {
final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() { final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() {
@ -103,6 +129,10 @@ public class SelectSowDayController implements Initializable {
setStyle("-fx-background-color: #32CD32;"); 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; return dayCellFactory;
} }
/**
* close date picker window
*/
private void closeWindow() { private void closeWindow() {
Stage stage = (Stage) save_button.getScene().getWindow(); Stage stage = (Stage) save_button.getScene().getWindow();
stage.close(); stage.close();
} }
/**
* disable save button, when there is no date selected in date picker
*/
private void enableDisableSaveButton() { private void enableDisableSaveButton() {
save_button.setDisable(true); save_button.setDisable(true);
datepicker.getEditor().textProperty().addListener(new ChangeListener<String>() { datepicker.getEditor().textProperty().addListener(new ChangeListener<String>() {

View File

@ -20,20 +20,40 @@ public record Plant(
List<Pest> pests, List<Pest> pests,
List<GrowthPhase> lifecycle) { List<GrowthPhase> lifecycle) {
/**
* remove all growthPhase which do not belong to the hardiness zone
* @param zone hardiness zone
*/
public void inZone(HardinessZone zone) { public void inZone(HardinessZone zone) {
lifecycle.removeIf(growthPhase -> !growthPhase.zone().equals(zone)); lifecycle.removeIf(growthPhase -> !growthPhase.zone().equals(zone));
} }
/**
* get all growthPhases of lifecycle group
* @param group lifecycle group
* @return list of growthPhases
*/
public List<GrowthPhase> lifecycleForGroup(int group) { public List<GrowthPhase> lifecycleForGroup(int group) {
return lifecycle.stream() return lifecycle.stream()
.filter(growthPhase -> growthPhase.group() != group) .filter(growthPhase -> growthPhase.group() == group)
.collect(Collectors.toList()); .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) { public LocalDate sowDateFromHarvestDate(LocalDate harvestDate, int group) {
return harvestDate.minusDays(timeToHarvest(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) { public int timeToHarvest(int group) {
List<GrowthPhase> activeLifecycle = lifecycleForGroup(group); List<GrowthPhase> activeLifecycle = lifecycleForGroup(group);
GrowthPhase sow = activeLifecycle.stream() GrowthPhase sow = activeLifecycle.stream()
@ -49,6 +69,12 @@ public record Plant(
return (int) DAYS.between(harvest.startDate().atYear(currentYear), sow.startDate().atYear(currentYear)); 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<LocalDate> getDateListOfGrowthPhase(GrowthPhaseType growthPhase) { public List<LocalDate> getDateListOfGrowthPhase(GrowthPhaseType growthPhase) {
List<LocalDate> dates = new LinkedList<>(); List<LocalDate> dates = new LinkedList<>();
for (GrowthPhase growth : lifecycle) { for (GrowthPhase growth : lifecycle) {
@ -56,12 +82,15 @@ public record Plant(
dates = addDatesFromMonthDay(growth); dates = addDatesFromMonthDay(growth);
} }
} }
if (growthPhase.equals(GrowthPhaseType.HARVEST)) {
return removeDatesForPassedSowDates(dates);
}
return 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<LocalDate> addDatesFromMonthDay(GrowthPhase growthPhase) { private List<LocalDate> addDatesFromMonthDay(GrowthPhase growthPhase) {
List<LocalDate> dates = new LinkedList<>(); List<LocalDate> dates = new LinkedList<>();
LocalDate today = LocalDate.now(); LocalDate today = LocalDate.now();
@ -73,9 +102,4 @@ public record Plant(
} }
return dates; return dates;
} }
private List<LocalDate> removeDatesForPassedSowDates(List<LocalDate> dates) {
//ToDo Remove harvest dates where sow dates have already passed current day
return dates;
}
} }

View File

@ -94,6 +94,7 @@
"endDate": "03-10", "endDate": "03-10",
"zone": "ZONE_8A", "zone": "ZONE_8A",
"type": "SOW", "type": "SOW",
"group": 0,
"wateringCycle": { "wateringCycle": {
"litersPerSqM": 15, "litersPerSqM": 15,
"interval": 3, "interval": 3,
@ -115,6 +116,7 @@
"endDate": "05-10", "endDate": "05-10",
"zone": "ZONE_8A", "zone": "ZONE_8A",
"type": "PLANT", "type": "PLANT",
"group": 0,
"wateringCycle": { "wateringCycle": {
"litersPerSqM": 25, "litersPerSqM": 25,
"interval": 3, "interval": 3,
@ -138,6 +140,7 @@
"endDate": "05-20", "endDate": "05-20",
"zone": "ZONE_8A", "zone": "ZONE_8A",
"type": "HARVEST", "type": "HARVEST",
"group": 0,
"wateringCycle": { "wateringCycle": {
"litersPerSqM": 0, "litersPerSqM": 0,
"interval": null, "interval": null,