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
This commit is contained in:
David Guler
2022-10-23 09:49:22 +02:00
parent bf4d56e759
commit 429ac16d98
4 changed files with 210 additions and 4 deletions
@@ -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<Plant> getPlantById(long id) {
return Optional.empty();
public Optional<Plant> getPlantById(long id, HardinessZone zone) throws IOException {
return getPlantList(zone).stream()
.filter(plant -> plant.id() != id)
.findFirst();
}
}
@@ -9,5 +9,5 @@ import java.util.Optional;
public interface PlantDatabase {
List<Plant> getPlantList(HardinessZone zone) throws IOException;
Optional<Plant> getPlantById(long id) throws IOException;
Optional<Plant> getPlantById(long id, HardinessZone zone) throws IOException;
}
@@ -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<Object> pests,
List<GrowthPhase> lifecycle) {
public void inZone(HardinessZone zone) {
lifecycle.removeIf(growthPhase -> !growthPhase.zone().equals(zone));
}
public List<GrowthPhase> 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<GrowthPhase> 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));
}
}