From d1c5c584686492457ae6fe116415d6d1e5938176 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 24 Oct 2022 18:55:26 +0200 Subject: [PATCH] adapted class PlantListModel to PlantDatabase --- .../io/JsonPlantDatabase.java | 4 +- .../gartenverwaltung/io/PlantDatabase.java | 2 +- .../plantList/PlantListModel.java | 72 ++++++++++++++----- .../ch/zhaw/gartenverwaltung/io/plantdb.json | 2 +- 4 files changed, 57 insertions(+), 23 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java index 5591acd..aa06e65 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java @@ -56,9 +56,9 @@ public class JsonPlantDatabase implements PlantDatabase { * @see PlantDatabase#getPlantById(long) */ @Override - public Optional getPlantById(long id) throws HardinessZoneNotSetException, IOException { + public Optional getPlantById(HardinessZone zone, long id) throws HardinessZoneNotSetException, IOException { if (plantMap.isEmpty()) { - loadPlantList(currentZone); + loadPlantList(zone); } return Optional.ofNullable(plantMap.get(id)); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java index 0b6908d..455791e 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java @@ -30,5 +30,5 @@ public interface PlantDatabase { * @throws IOException If the database cannot be accessed * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified */ - Optional getPlantById(long id) throws IOException, HardinessZoneNotSetException; + Optional getPlantById(HardinessZone zone, long id) throws IOException, HardinessZoneNotSetException; } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java index 22b7de2..86e21f2 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java @@ -1,53 +1,87 @@ 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 java.io.IOException; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.function.Predicate; public class PlantListModel { - private List plantList; + private final PlantDatabase plantDatabase; + private HardinessZone currentZone; /** * Comparators to create sorted Plant List */ public final Comparator sortByName = (Plant o1, Plant o2) -> o1.name().compareTo(o2.name()); public final Comparator getSortById = (Plant o1, Plant o2) -> Long.compare(o1.id(), o2.id()); - public final Comparator sortBySpacing = (Plant o1, Plant o2) -> o1.spacing() - o2.spacing(); - - public PlantListModel(List plantList) { - setPlantList(plantList); + public PlantListModel() { + plantDatabase = new JsonPlantDatabase(); } - public void setPlantList(List plantList) { - this.plantList = plantList; + public void setCurrentZone(HardinessZone currentZone) { + this.currentZone = currentZone; + } + + public HardinessZone getCurrentZone() { + return currentZone; } /** * Method to get actual Plant List in alphabetic Order * @return actual Plant List in alphabetic Order */ - public List getPlantList() { - return getSortedPlantList(sortByName); + public List getPlantList(HardinessZone zone) throws HardinessZoneNotSetException, IOException { + setCurrentZone(zone); + return getSortedPlantList(zone, sortByName); + } + + + /** + * Method to get the actual Plant list in custom Order + * @param zone selected hardiness zone + * @param comparator comparator to sort the list + * @return sorted list with plants in the given hardiness zone + * @throws IOException If the database cannot be accessed + * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified + */ + public List getSortedPlantList(HardinessZone zone, Comparator comparator) throws HardinessZoneNotSetException, IOException { + setCurrentZone(zone); + return plantDatabase.getPlantList(zone).stream().sorted(comparator).toList(); } /** - * Method to get actual Plant List sorted in custom order - * @param comparator comparator Object which is used to sort list - * @return actual Plant List in custom order + * Method to get Filtered plant list + * @param predicate predicate to filter the list + * @param zone selected hardiness zone + * @return filterd list with plants in the hardinness zone + * @throws IOException If the database cannot be accessed + * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified */ - public List getSortedPlantList(Comparator comparator) { - return plantList.stream().sorted(comparator).toList(); + public List getFilteredPlantList(HardinessZone zone, Predicate predicate) throws HardinessZoneNotSetException, IOException { + setCurrentZone(zone); + return getPlantList(zone).stream().filter(predicate).toList(); } /** - * Method to get filtered Plant List with custom filter - * @param predicate predicate for filter - * @return filtered Plant List + * Method to get Filtered plant list by id by exact match + * @param zone selected hardiness zone + * @param id id of plant + * @return if id doesn't exist: empty List, else list with 1 plant entry. + * @throws IOException If the database cannot be accessed + * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified */ - public List getFilteredPlantList(Predicate predicate) { - return plantList.stream().filter(predicate).toList(); + public List getFilteredPlantListById(HardinessZone zone, Long id) throws HardinessZoneNotSetException, IOException { + setCurrentZone(zone); + List plantList = new ArrayList<>(); + plantDatabase.getPlantById(zone, id).ifPresent(plantList::add); + return plantList; } } diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json index f513570..aae89bc 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json @@ -251,4 +251,4 @@ } ] } -] \ No newline at end of file +]