From 429ac16d98b52cf95f266d9ae329a03bcdbd342e Mon Sep 17 00:00:00 2001 From: David Guler Date: Sun, 23 Oct 2022 09:49:22 +0200 Subject: [PATCH] filtering lifecycle based on hardiness-zone - Added 2 more plants to test db - Db filters plant lifecycles based on hardiness-zones - getPlantById implemented - Added methods to Plant for calculating sowDates and accounting for multiple lifecycles --- .../io/JsonPlantDatabase.java | 9 +- .../gartenverwaltung/io/PlantDatabase.java | 2 +- .../ch/zhaw/gartenverwaltung/types/Plant.java | 34 +++- .../ch/zhaw/gartenverwaltung/io/plantdb.json | 169 ++++++++++++++++++ 4 files changed, 210 insertions(+), 4 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java index 8882e8e..e589bc8 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java @@ -34,12 +34,17 @@ public class JsonPlantDatabase implements PlantDatabase { result = mapper.readerForListOf(Plant.class).readValue(dataSource); } + for (Plant plant : result) { + plant.inZone(zone); + } return result; } @Override - public Optional getPlantById(long id) { - return Optional.empty(); + public Optional getPlantById(long id, HardinessZone zone) throws IOException { + return getPlantList(zone).stream() + .filter(plant -> plant.id() != id) + .findFirst(); } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java index 2a38e3b..4b8fe85 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java @@ -9,5 +9,5 @@ import java.util.Optional; public interface PlantDatabase { List getPlantList(HardinessZone zone) throws IOException; - Optional getPlantById(long id) throws IOException; + Optional getPlantById(long id, HardinessZone zone) throws IOException; } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java index 27b1944..46ff155 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java @@ -1,14 +1,46 @@ package ch.zhaw.gartenverwaltung.types; +import java.time.LocalDate; import java.util.List; +import static java.time.temporal.ChronoUnit.DAYS; + public record Plant( long id, String name, String description, - int spacing, + String spacing, int light, String soil, List pests, List lifecycle) { + + public void inZone(HardinessZone zone) { + lifecycle.removeIf(growthPhase -> !growthPhase.zone().equals(zone)); + } + + public List lifecycleForGroup(int group) { + return lifecycle.stream() + .filter(growthPhase -> growthPhase.group() != group) + .toList(); + } + + public LocalDate sowDateFromHarvestDate(LocalDate harvestDate, int group) { + return harvestDate.minusDays(timeToHarvest(group)); + } + + public int timeToHarvest(int group) { + List activeLifecycle = lifecycleForGroup(group); + GrowthPhase sow = activeLifecycle.stream() + .filter(growthPhase -> !growthPhase.type().equals(GrowthPhaseType.SOW)) + .findFirst() + .orElseThrow(); + GrowthPhase harvest = activeLifecycle.stream() + .filter(growthPhase -> !growthPhase.type().equals(GrowthPhaseType.HARVEST)) + .findFirst() + .orElseThrow(); + + int currentYear = LocalDate.now().getYear(); + return (int) DAYS.between(harvest.startDate().atYear(currentYear), sow.startDate().atYear(currentYear)); + } } diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json index 50b74f1..8ec3394 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json @@ -81,5 +81,174 @@ ] } ] + }, + { + "id": 1, + "name": "Early Carrot", + "description": "Carrot, (Daucus carota), herbaceous, generally biennial plant of the Apiaceae family that produces an edible taproot. Among common varieties root shapes range from globular to long, with lower ends blunt to pointed. Besides the orange-coloured roots, white-, yellow-, and purple-fleshed varieties are known.", + "lifecycle": [ + { + "startDate": "02-20", + "endDate": "03-10", + "zone": "ZONE_8A", + "type": "SOW", + "wateringCycle": { + "litersPerSqM": 15, + "interval": 3, + "notes": [] + }, + "taskTemplates": [ + { + "name": "hilling", + "relativeStartDate": 0, + "relativeEndDate": 0, + "description": "Mound up the soil around the plant until just the top few leaves show above the soil. ", + "interval": null, + "isOptional": false + } + ] + }, + { + "startDate": "03-10", + "endDate": "05-10", + "zone": "ZONE_8A", + "type": "PLANT", + "wateringCycle": { + "litersPerSqM": 25, + "interval": 3, + "notes": [ + "Be careful not to pour water over the leaves, as this will lead to sunburn." + ] + }, + "taskTemplates": [ + { + "name": "hilling", + "relativeStartDate": 0, + "relativeEndDate": null, + "description": "Mound up the soil around the plant until just the top few leaves show above the soil. ", + "interval": 15, + "isOptional": true + } + ] + }, + { + "startDate": "05-10", + "endDate": "05-20", + "zone": "ZONE_8A", + "type": "HARVEST", + "wateringCycle": { + "litersPerSqM": 0, + "interval": null, + "notes": [] + }, + "taskTemplates": [ + { + "name": "Harvesting", + "relativeStartDate": 0, + "relativeEndDate": 14, + "description": "When the leaves turn to a yellowish brown. Do not harvest earlier. The plant will show when it's ready.", + "interval": null, + "isOptional": false + } + ] + } + ], + "soil": "sandy to loamy, loose soil, free of stones", + "spacing": "5,35,2.5", + "pests": [ + { + "name": "Rot", + "description": "rot, any of several plant diseases, caused by any of hundreds of species of soil-borne bacteria, fungi, and funguslike organisms (Oomycota). Rot diseases are characterized by plant decomposition and putrefaction. The decay may be hard, dry, spongy, watery, mushy, or slimy and may affect any plant part.", + "measurement": "less water" + } + ] + }, + { + "id": 2, + "name": "summertime onion", + "description": "Onion, (Allium cepa), herbaceous biennial plant in the amaryllis family (Amaryllidaceae) grown for its edible bulb. The onion is likely native to southwestern Asia but is now grown throughout the world, chiefly in the temperate zones. Onions are low in nutrients but are valued for their flavour and are used widely in cooking. They add flavour to such dishes as stews, roasts, soups, and salads and are also served as a cooked vegetable.", + "lifecycle": [ + { + "startDate": "03-15", + "endDate": "04-10", + "type": "SOW", + "zone": "ZONE_8A", + "group": 0, + "wateringCycle": { + "litersPerSqM": 15, + "interval": 4, + "notes": [ + + ] + }, + "taskTemplates": [ + { + "name": "hilling", + "relativeStartDate": 0, + "relativeEndDate": 0, + "description": "Mound up the soil around the plant until just the top few leaves show above the soil. ", + "interval": null, + "isOptional": false + } + ] + }, + { + "startDate": "04-10", + "endDate": "07-10", + "type": "PLANT", + "zone": "ZONE_8A", + "group": 0, + "wateringCycle": { + "litersPerSqM": 25, + "interval": 3, + "notes": [ + "" + ] + }, + "taskTemplates": [ + { + "name": "hilling", + "relativeStartDate": 0, + "relativeEndDate": null, + "description": "Mound up the soil around the plant until just the top few leaves show above the soil. ", + "interval": 15, + "isOptional": true + } + ] + }, + { + "startDate": "07-10", + "endDate": "09-20", + "type": "HARVEST", + "zone": "ZONE_8A", + "group": 0, + "wateringCycle": { + "litersPerSqM": 0, + "interval": null, + "notes": [ + + ] + }, + "taskTemplates": [ + { + "name": "Harvesting", + "relativeStartDate": 0, + "relativeEndDate": 14, + "description": "When ready for harvest, the leaves on your onion plants will start to flop over. This happens at the \"neck\" of the onion and it signals that the plant has stopped growing and is ready for storage. Onions should be harvested soon thereafter", + "interval": null, + "isOptional": false + } + ] + } + ], + "soil": "sandy to loamy, loose soil, free of stones", + "spacing": "15,30,2", + "pests": [ + { + "name": "Rot", + "description": "rot, any of several plant diseases, caused by any of hundreds of species of soil-borne bacteria, fungi, and funguslike organisms (Oomycota). Rot diseases are characterized by plant decomposition and putrefaction. The decay may be hard, dry, spongy, watery, mushy, or slimy and may affect any plant part.", + "measurement": "less water" + } + ] } ] \ No newline at end of file