Compare commits

..

No commits in common. "b0369e3174809a31caf34a8443677b167dd37026" and "2312149256926a757a9909f41a63839bf1a30b63" have entirely different histories.

6 changed files with 36 additions and 38 deletions

View File

@ -1,6 +1,7 @@
package ch.zhaw.gartenverwaltung; package ch.zhaw.gartenverwaltung;
import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Crop;
import ch.zhaw.gartenverwaltung.types.GrowthPhaseType;
import ch.zhaw.gartenverwaltung.types.Task; import ch.zhaw.gartenverwaltung.types.Task;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
@ -48,12 +49,8 @@ public class TaskFormularController implements Initializable {
taskName_field.setText(task.getName()); taskName_field.setText(task.getName());
description_area.setText(task.getDescription()); description_area.setText(task.getDescription());
start_datePicker.setValue(task.getStartDate()); start_datePicker.setValue(task.getStartDate());
end_datePicker.setValue(task.getEndDate().orElse(null)); end_datePicker.setValue(task.getEndDate().get());
if(task.getInterval().orElse(null)!=null) { interval_field.setText(task.getInterval().get().toString());
interval_field.setText(task.getInterval().get().toString());
} else {
interval_field.setText(null);
}
} }
private Callback<DatePicker, DateCell> getDayCellFactory() { private Callback<DatePicker, DateCell> getDayCellFactory() {

View File

@ -103,9 +103,10 @@ public class JsonTaskList implements TaskList {
if(taskMap.isEmpty()) { if(taskMap.isEmpty()) {
loadTaskListFromFile(); loadTaskListFromFile();
} }
long id = task.getId().orElse(idProvider.incrementAndGet()); if(task.getId() == 0) {
task.withId(idProvider.incrementAndGet());
taskMap.put(id, task.withId(id)); }
taskMap.put(task.getId(), task);
writeTaskListToFile(); writeTaskListToFile();
} }
@ -121,9 +122,8 @@ public class JsonTaskList implements TaskList {
if(taskMap.isEmpty()) { if(taskMap.isEmpty()) {
loadTaskListFromFile(); loadTaskListFromFile();
} }
Long taskId = task.getId().orElseThrow(IOException::new); if(taskMap.containsKey(task.getId())){
if(taskMap.containsKey(taskId)){ taskMap.remove(task.getId());
taskMap.remove(taskId);
writeTaskListToFile(); writeTaskListToFile();
} }
} }
@ -163,7 +163,7 @@ public class JsonTaskList implements TaskList {
taskMap = result.stream() taskMap = result.stream()
.collect(HashMap::new, .collect(HashMap::new,
(res, task) -> res.put(task.getId().orElse(0L), task), (res, task) -> res.put(task.getId(), task),
(existing, replacement) -> {}); (existing, replacement) -> {});
} }

View File

@ -9,7 +9,7 @@ import java.util.Optional;
* May be created using the builder pattern. * May be created using the builder pattern.
*/ */
public class Task { public class Task {
private Long id; private long id;
private final String name; private final String name;
private final String description; private final String description;
private final LocalDate startDate; private final LocalDate startDate;
@ -62,7 +62,7 @@ public class Task {
} }
// Getters // Getters
public Optional<Long> getId() { return Optional.ofNullable(id); } public long getId() { return id; }
public String getName() { return name; } public String getName() { return name; }
public String getDescription() { return description; } public String getDescription() { return description; }
public LocalDate getStartDate() { return startDate; } public LocalDate getStartDate() { return startDate; }

View File

@ -34,7 +34,6 @@ public class TaskTemplate {
public void setRelativeEndDate(Integer relativeEndDate) { public void setRelativeEndDate(Integer relativeEndDate) {
this.relativeEndDate = relativeEndDate; this.relativeEndDate = relativeEndDate;
} }
public void setInterval(Integer interval) { public void setInterval(Integer interval) {
this.interval = interval; this.interval = interval;
} }
@ -46,10 +45,13 @@ public class TaskTemplate {
} }
public Task generateTask(LocalDate realStartDate, long cropId) { public Task generateTask(LocalDate realStartDate, long cropId) {
LocalDate endDate = relativeEndDate != null ? realStartDate.plusDays(relativeEndDate) : null; Task task = new Task(name, description, realStartDate.plusDays(relativeStartDate), cropId);
if (relativeEndDate != null) {
return new Task(name, description, realStartDate.plusDays(relativeStartDate), cropId) task.withEndDate(realStartDate.plusDays(relativeEndDate));
.withInterval(interval) }
.withEndDate(endDate); if (interval != null) {
task.withInterval(interval);
}
return task;
} }
} }

View File

@ -72,8 +72,7 @@ public class JsonTaskListTest {
@Test @Test
@DisplayName("Remove task.") @DisplayName("Remove task.")
void removeTask() { void removeTask() {
Task task = new Task("Dummy", "Dummy", LocalDate.parse("2022-05-31", formatter), 1) Task task = new Task("Dummy", "Dummy", LocalDate.parse("2022-05-31", formatter), 1).withId(2);
.withId(2);
try { try {
testDatabase.removeTask(task); testDatabase.removeTask(task);
List<Task> taskList; List<Task> taskList;

View File

@ -55,8 +55,8 @@ public class GardenPlanModelTest {
new GrowthPhase(MonthDay.of(2, 8), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), new GrowthPhase(MonthDay.of(2, 8), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()),
new GrowthPhase(MonthDay.of(10, 2), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))); new GrowthPhase(MonthDay.of(10, 2), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>())));
exampleCropOnion = new Crop(examplePlantOnion.id(), LocalDate.of(2023, 3, 1)) exampleCropOnion = new Crop(examplePlantOnion.id(), LocalDate.of(2023,3,1));
.withId(3); exampleCropOnion.withId(3);
examplePlantCarrot = new Plant( examplePlantCarrot = new Plant(
1, 1,
"Early Carrot", "Early Carrot",
@ -67,18 +67,18 @@ public class GardenPlanModelTest {
"sandy to loamy, loose soil, free of stones", "sandy to loamy, loose soil, free of stones",
new ArrayList<>(), new ArrayList<>(),
List.of(new GrowthPhase(MonthDay.of(4, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))); List.of(new GrowthPhase(MonthDay.of(4, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>())));
exampleCropCarrot = new Crop(examplePlantCarrot.id(), LocalDate.now()) exampleCropCarrot = new Crop(examplePlantCarrot.id(), LocalDate.now());
.withId(5); exampleCropCarrot.withId(5);
exampleCrop1 = new Crop(1, LocalDate.of(2023, 2, 25)) exampleCrop1 = new Crop(1, LocalDate.of(2023,2,25));
.withId(0) exampleCrop1.withId(0);
.withArea(0.5); exampleCrop1.withArea(0.5);
exampleCrop2 = new Crop(1, LocalDate.of(2023, 3, 1)) exampleCrop2 = new Crop(1,LocalDate.of(2023,3,1));
.withId(1) exampleCrop2.withId(1);
.withArea(0.5); exampleCrop2.withArea(0.5);
exampleCrop3 = new Crop(0, LocalDate.of(2023, 3, 1)) exampleCrop3 = new Crop(0,LocalDate.of(2023,3,1));
.withId(2) exampleCrop3.withId(2);
.withArea(1.0); exampleCrop3.withArea(1.0);
exampleCrops = new ArrayList<>(); exampleCrops = new ArrayList<>();
exampleCrops.add(exampleCrop1); exampleCrops.add(exampleCrop1);
@ -107,7 +107,7 @@ public class GardenPlanModelTest {
@Test @Test
void plantAsCrop() throws HardinessZoneNotSetException, IOException, PlantNotFoundException { void plantAsCrop() throws HardinessZoneNotSetException, IOException, PlantNotFoundException {
model.plantAsCrop(examplePlantOnion, LocalDate.of(2023, 3, 1)); model.plantAsCrop(examplePlantOnion, LocalDate.of(2023,3,1));
Optional<Crop> exampleCrop = model.getCrop(2L); Optional<Crop> exampleCrop = model.getCrop(2L);
assertTrue(exampleCrop.isPresent()); assertTrue(exampleCrop.isPresent());
assertEquals(model.getCrops().get(2), exampleCrop.get()); assertEquals(model.getCrops().get(2), exampleCrop.get());
@ -119,6 +119,6 @@ public class GardenPlanModelTest {
exampleCrop1.withId(2); exampleCrop1.withId(2);
exampleCrop1.withArea(1.5); exampleCrop1.withArea(1.5);
model.removeCrop(exampleCrop1); model.removeCrop(exampleCrop1);
assertEquals(2, model.getCrops().size()); assertEquals(2,model.getCrops().size());
} }
} }