From 3e586093ba5f7e347827980ef0a7aa2d590759f2 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 30 Oct 2022 09:32:55 +0100 Subject: [PATCH] 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()); + } }