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)); + } + + }