From 9e14920fbb71fa183ea97ae5198d5a242389a9ec Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Tue, 1 Nov 2022 10:33:07 +0100 Subject: [PATCH] #27 gardenplanmodel fully implemented --- .../gardenplan/Gardenplanmodel.java | 144 ++++++++---------- 1 file changed, 64 insertions(+), 80 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java b/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java index c2cabee..18e072b 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java @@ -2,105 +2,89 @@ package ch.zhaw.gartenverwaltung.gardenplan; import ch.zhaw.gartenverwaltung.io.GardenPlan; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; +import ch.zhaw.gartenverwaltung.io.JsonGardenPlan; import ch.zhaw.gartenverwaltung.io.TaskDatabase; -import ch.zhaw.gartenverwaltung.io.PlantDatabase; -import ch.zhaw.gartenverwaltung.types.*; +import ch.zhaw.gartenverwaltung.taskList.PlantNotFoundException; +import ch.zhaw.gartenverwaltung.taskList.TaskListModel; +import ch.zhaw.gartenverwaltung.types.Crop; +import ch.zhaw.gartenverwaltung.types.Plant; +import ch.zhaw.gartenverwaltung.types.Task; import java.io.IOException; -import java.util.Collection; -import java.util.Collections; +import java.time.LocalDate; import java.util.List; import java.util.Optional; +/** + * The Gardenplan model manages the crops in the gardenplan. + */ public class Gardenplanmodel { - private GardenPlan gardenplan; - private TaskDatabase taskDatabase; - private PlantDatabase plantDatabase; -// private TaskListmodel; - private List cropList = Collections.emptyList(); - - public Gardenplanmodel() { - try { - cropList = gardenplan.getCrops(); - } catch (IOException ioException){ - System.err.println("The task can not be created!"); - } + private GardenPlan gardenPlan; + private List cropList; + private TaskListModel taskListModel; + /** + * Constructor of Gardenplan model + * + * @param taskListModel holds a reference to the task list object. + */ + public Gardenplanmodel(TaskListModel taskListModel) throws IOException { + this.taskListModel = taskListModel; + gardenPlan = new JsonGardenPlan(); + cropList = gardenPlan.getCrops(); } - public void plantCrop(Crop crop){ + /** + * Creates a Crop with a {@link Plant} and the planting date of the plant. Then let the Tasklistmodel create the + * gardening {@link Task} for the crop. Store the crop in the gardenplan file and the cache. + * + * @param plant The plant which is wnated to be planted + * @param plantingDate The date, when the plant is planted + * @throws IOException If the database cannot be accessed + * @throws HardinessZoneNotSetException If the hardinesszone could not be added + * @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); + //TODO Add Area to Plant + crop.withArea(0); + cropList.add(crop); - - try { - gardenplan.saveCrop(crop); - createGardeningTasks(crop); - } catch (IOException ioException){ - System.err.println("The task can not be created!"); - } - + gardenPlan.saveCrop(crop); + taskListModel.planTasksForCrop(crop); } - public void removeCrop(Crop crop){ + /** + * Removes a {@link Crop} from the file and the cache + * + * @param crop The plant which is wnated to be planted + * @throws IOException If the database cannot be accessed + */ + public void removeCrop(Crop crop) throws IOException { + cropList.remove(crop); - try { - gardenplan.removeCrop(crop); - //TaskListModel.removeTasksForCrop(crop) - } catch (IOException ioException){ - System.err.println("The task can not be created!"); - } - + gardenPlan.removeCrop(crop); + taskListModel.removeTasksForCrop(crop); } - - public List getCrops(){ + /** + * Returns a list of {@link Crop}s which are currently in the gardenplan. + * + * @throws IOException If the database cannot be accessed + */ + public List getCrops() throws IOException { if(cropList.isEmpty()){ - try { - cropList = gardenplan.getCrops(); - } catch (IOException ioException){ - System.err.println("Crops could not be loaded"); - } - + cropList = gardenPlan.getCrops(); } return cropList; } - public Optional getCrop(Long id){ - try { - return gardenplan.getCropById(id); - - } catch (IOException ioException){ - System.err.println("The task can not be created!"); - } - return null; - } - - private void createGardeningTasks(Crop crop) throws IOException, HardinessZoneNotSetException { - //TaskListModel.generateTasksForCrop(crop) - long plantId = crop.getPlantId(); - Optional plant; - Task task; - - Plant plantL = plant; - - - plant = plantDatabase.getPlantById(HardinessZone.ZONE_8A, plantId); - - if(plant.isPresent()){ - List growthPhases = plantL.lifecycleForGroup(0); - GrowthPhase growthPhase = growthPhases.get(0); - List taskTemplatesForLyfecycle= growthPhase.taskTemplates(); - - task = taskTemplatesForLyfecycle.get(0).generateTask(crop.getStartDate()); - taskDatabase.saveTask(task); - } else { - - } - - - - - //TODO need to get the plants templates, or through the Growphase - // plant.getPlantTemplates - // CreateTaskWithTasktemplate - + /** + * Returns an Optional of {@link Crop} if the crop is the gardenplan + * + * @param cropId The date, when the plant is planted + * @throws IOException If the database cannot be accessed + */ + public Optional getCrop(Long cropId) throws IOException { + return gardenPlan.getCropById(cropId); } }