From 3e586093ba5f7e347827980ef0a7aa2d590759f2 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 30 Oct 2022 09:32:55 +0100 Subject: [PATCH 1/3] implemented Method getFilteredPlantListByString in PlantListModel --- .../plantList/PlantListModel.java | 19 ++++++++++++++++-- .../plantList/PlantListModelTest.java | 20 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java index 8217962..d839a3c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java @@ -20,8 +20,8 @@ public class PlantListModel { /** * Comparators to create sorted Plant List */ - static final Comparator sortByName = (Plant o1, Plant o2) -> o1.name().compareTo(o2.name()); - static final Comparator SortById = (Plant o1, Plant o2) -> Long.compare(o1.id(), o2.id()); + static final Comparator sortByName = Comparator.comparing(Plant::name); + static final Comparator SortById = Comparator.comparingLong(Plant::id); /** * Constructor to create Database Object. @@ -98,4 +98,19 @@ public class PlantListModel { plantDatabase.getPlantById(zone, id).ifPresent(plantList::add); return plantList; } + + + public List getFilteredPlantListByString(HardinessZone zone, String searchString) throws HardinessZoneNotSetException, IOException { + if(searchString.charAt(0) == '#') { + try { + return getFilteredPlantListById(zone, Long.parseLong(searchString.substring(1))); + } catch (NumberFormatException e) { + return new ArrayList<>(); + } + } else { + return getFilteredPlantList(zone, plant -> plant.name().contains(searchString) || plant.description().contains(searchString)); + } + } + + } diff --git a/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java index 0aecc35..fe335b4 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java @@ -146,4 +146,24 @@ class PlantListModelTest { assertEquals(1, plantListResult.size()); assertEquals(examplePlantList.get(0), plantListResult.get(0)); } + + @Test + void getFilteredPlantListByString() throws HardinessZoneNotSetException, IOException { + model.setCurrentZone(HardinessZone.ZONE_1A); + List plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "#2"); + assertEquals(0, plantListResult.size()); + plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "#20"); + assertEquals(1, plantListResult.size()); + assertEquals(examplePlantList.get(0), plantListResult.get(0)); + plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "#a2"); + assertEquals(0, plantListResult.size()); + plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "onion"); + assertEquals(1, plantListResult.size()); + assertEquals(examplePlantList.get(0), plantListResult.get(0)); + plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "white roots"); + assertEquals(1, plantListResult.size()); + assertEquals(examplePlantList.get(1), plantListResult.get(0)); + plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "apple"); + assertEquals(0, plantListResult.size()); + } } From da7a31f512327dd110db58074c27446d840c51b4 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 30 Oct 2022 09:37:27 +0100 Subject: [PATCH 2/3] java doc and readme update --- README.md | 3 +++ .../gartenverwaltung/plantList/PlantListModel.java | 11 ++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5b7f091..8e56120 100644 --- a/README.md +++ b/README.md @@ -27,3 +27,6 @@ These branches are for bugfixes. These branches are for javadoc and project documentation (such as the readme, class diagrams etc.). + +## User Manual +- Search Plant List: if first Char is '#': only exact match in ID. diff --git a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java index d839a3c..b4f2a21 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java @@ -99,7 +99,14 @@ public class PlantListModel { return plantList; } - + /** + * + * @param zone selected hardiness zone + * @param searchString the string to search plant List, set '#' as first char the search by id. + * @return List of plants found in Plant List which contain the search String in the name or description + * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified + * @throws IOException If the database cannot be accessed + */ public List getFilteredPlantListByString(HardinessZone zone, String searchString) throws HardinessZoneNotSetException, IOException { if(searchString.charAt(0) == '#') { try { @@ -111,6 +118,4 @@ public class PlantListModel { return getFilteredPlantList(zone, plant -> plant.name().contains(searchString) || plant.description().contains(searchString)); } } - - } From 4e794a8a939af072bfcd9ab50f14bd31544bec43 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 30 Oct 2022 10:31:09 +0100 Subject: [PATCH 3/3] implemented Methods and tests: getFilteredPlantListByHarvestSaison getFilteredPlantListByPlantingSaison #12 --- .../plantList/PlantListModel.java | 42 +++++++++++++++++++ .../plantList/PlantListModelTest.java | 35 +++++++++++++--- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java index b4f2a21..7039476 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java @@ -3,10 +3,12 @@ package ch.zhaw.gartenverwaltung.plantList; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.io.JsonPlantDatabase; import ch.zhaw.gartenverwaltung.io.PlantDatabase; +import ch.zhaw.gartenverwaltung.types.GrowthPhaseType; import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Plant; import java.io.IOException; +import java.time.MonthDay; import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -118,4 +120,44 @@ public class PlantListModel { return getFilteredPlantList(zone, plant -> plant.name().contains(searchString) || plant.description().contains(searchString)); } } + + /** + * + * @param type GrowPhaseType to filter + * @param zone selected hardiness zone + * @param from the earliest date to for the filter + * @param to the lastest date for the filter + * @return List of Plants with selected saison + * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified + * @throws IOException If the database cannot be accessed + */ + private List getFilteredPlantListBySaison(GrowthPhaseType type, HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException { + return getFilteredPlantList(zone, plant -> plant.lifecycle().stream().anyMatch(growthPhase -> growthPhase.startDate().compareTo(from) >= 0 && (growthPhase.startDate().compareTo(to) <= 0) && growthPhase.type() == type)); + } + + /** + * + * @param zone selected hardiness zone + * @param from the earliest date to for the filter + * @param to the lastest date for the filter + * @return List of Plants with selected saison + * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified + * @throws IOException If the database cannot be accessed + */ + public List getFilteredPlantListByPlantingSaison(HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException { + return getFilteredPlantListBySaison(GrowthPhaseType.PLANT, zone, from, to); + } + + /** + * + * @param zone selected hardiness zone + * @param from the earliest date to for the filter + * @param to the lastest date for the filter + * @return List of Plants with selected saison + * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified + * @throws IOException If the database cannot be accessed + */ + public List getFilteredPlantListByHarvestSaison(HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException { + return getFilteredPlantListBySaison(GrowthPhaseType.HARVEST, zone, from, to); + } } diff --git a/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java index fe335b4..f341ba9 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java @@ -3,14 +3,14 @@ package ch.zhaw.gartenverwaltung.plantList; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.io.JsonPlantDatabase; import ch.zhaw.gartenverwaltung.io.PlantDatabase; -import ch.zhaw.gartenverwaltung.types.HardinessZone; -import ch.zhaw.gartenverwaltung.types.Plant; +import ch.zhaw.gartenverwaltung.types.*; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; +import java.time.MonthDay; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -47,7 +47,11 @@ class PlantListModelTest { 0, "sandy to loamy, loose soil, free of stones", new ArrayList<>(), - new ArrayList<>()) + List.of(new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(4, 3), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(8, 5), 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<>()))) ); examplePlantList.add(new Plant( 0, @@ -57,7 +61,8 @@ class PlantListModelTest { 6, "sandy", new ArrayList<>(), - new ArrayList<>()) + List.of(new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))) ); examplePlantList.add(new Plant( 1, @@ -67,7 +72,7 @@ class PlantListModelTest { 0, "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<>()))) ); } @@ -166,4 +171,24 @@ class PlantListModelTest { plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "apple"); assertEquals(0, plantListResult.size()); } + + @Test + void getFilteredPlantListByPlantingSaison() throws HardinessZoneNotSetException, IOException { + model.setCurrentZone(HardinessZone.ZONE_1A); + List plantListResult = model.getFilteredPlantListByPlantingSaison(HardinessZone.ZONE_8A, MonthDay.of(4, 4), MonthDay.of(8, 4)); + assertEquals(2, plantListResult.size()); + assertEquals(examplePlantList.get(2), plantListResult.get(0)); + assertEquals(examplePlantList.get(1), plantListResult.get(1)); + } + + @Test + void getFilteredPlantListByHarvestSaison() throws HardinessZoneNotSetException, IOException { + model.setCurrentZone(HardinessZone.ZONE_1A); + List plantListResult = model.getFilteredPlantListByHarvestSaison(HardinessZone.ZONE_8A, MonthDay.of(4, 4), MonthDay.of(8, 4)); + assertEquals(2, plantListResult.size()); + assertEquals(examplePlantList.get(1), plantListResult.get(0)); + assertEquals(examplePlantList.get(0), plantListResult.get(1)); + } + + }