From 2361afd5374726db451f672bd5f366ab8a92577c Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Fri, 4 Nov 2022 15:32:15 +0100 Subject: [PATCH] #27 gardenplanmodel bugfixed cropid --- .../zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java | 8 ++++---- .../ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java | 3 +++ src/main/java/ch/zhaw/gartenverwaltung/types/Task.java | 4 ++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java b/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java index 5dfe5cd..00ebd44 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java @@ -12,9 +12,9 @@ import ch.zhaw.gartenverwaltung.types.Task; import java.io.IOException; import java.time.LocalDate; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Optional; +import java.util.function.Supplier; /** @@ -24,6 +24,7 @@ public class Gardenplanmodel { private GardenPlan gardenPlan; private List cropList; private TaskListModel taskListModel; + private Object IllegalArgumentException; /** * Constructor of Gardenplan model @@ -48,7 +49,7 @@ public class Gardenplanmodel { * @throws PlantNotFoundException If the plant is not found in the database. */ public void plantAsCrop(Plant plant, LocalDate plantingDate) throws IOException, HardinessZoneNotSetException, PlantNotFoundException { - Crop crop = new Crop(plant.id(),plantingDate); + Crop crop = new Crop(plant.id(), plantingDate); //TODO Add Area to Plant //crop.withArea(0); gardenPlan.saveCrop(crop); @@ -63,9 +64,8 @@ public class Gardenplanmodel { * @throws IOException If the database cannot be accessed */ public void removeCrop(Crop crop) throws IOException { - gardenPlan.removeCrop(crop); - taskListModel.removeTasksForCrop(crop); + taskListModel.removeTasksForCrop(crop.getCropId().orElseThrow(IllegalArgumentException::new)); cropList = gardenPlan.getCrops(); } /** diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java index 7c9f6b0..be0d2ab 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java @@ -159,5 +159,8 @@ public class JsonTaskDatabase implements TaskDatabase{ (res, task) -> res.put(task.getId(), task), (existing, replacement) -> {}); } + + Long maxId = taskMap.isEmpty() ? 0L : Collections.max(taskMap.keySet()); + idProvider = new IdProvider(maxId); } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index 02d6480..545864d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -21,11 +21,11 @@ public class Task { * default constructor * (used by Json deserializer) */ - public Task(long cropId){ + public Task(){ name= ""; description= ""; startDate = LocalDate.now(); - this.cropId = cropId; + // this.cropId = cropId; } public Task(String name, String description, LocalDate startDate, long cropId) {