From 5dd1269dd7f1dfea873491583fd5aa6d10566d34 Mon Sep 17 00:00:00 2001 From: Sergej Pavlenko Date: Thu, 20 Nov 2025 00:16:04 +0100 Subject: [PATCH] Fehler beim highest und lowest stored amount und beim highest price gefixt --- .../diejungsvondertanke/tankstelle/Main.java | 60 ++++++++++++++----- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/Main.java b/src/main/java/de/diejungsvondertanke/tankstelle/Main.java index 312455b..4ff3123 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/Main.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/Main.java @@ -118,14 +118,24 @@ public class Main { * @author Nils Göbbert */ public static FuelStation getHighestPrice(FuelType fuelType) throws NoSuchFuelTypeError { - float highestPrice = fuelStations.get(0).get_price(fuelType); - FuelStation highestStation = fuelStations.get(0); + FuelStation highestStation = null; + float highestPrice = Float.NEGATIVE_INFINITY; + for (FuelStation fuelStation : fuelStations) { - if (fuelStation.get_price(fuelType) > highestPrice) { - highestPrice = fuelStation.get_price(fuelType); - highestStation = fuelStation; + try { + float price = fuelStation.get_price(fuelType); + if (price > highestPrice) { + highestPrice = price; + highestStation = fuelStation; + } + } catch (NoSuchFuelTypeError e) { + } } + + if (highestStation == null) { + throw new NoSuchFuelTypeError("No fuel station with " + fuelType + " found"); + } return highestStation; } @@ -166,14 +176,24 @@ public class Main { * @author Nils Göbbert */ public static FuelStation getHighestStoredAmount(FuelType fuelType) throws NoSuchFuelTypeError { - float highestAmount = fuelStations.get(0).getStored_amount(fuelType); - FuelStation highestStation = fuelStations.get(0); + FuelStation highestStation = null; + float highestAmount = Float.NEGATIVE_INFINITY; + for (FuelStation fuelStation : fuelStations) { - if (fuelStation.getStored_amount(fuelType) > highestAmount) { - highestAmount = fuelStation.getStored_amount(fuelType); - highestStation = fuelStation; + try { + float amount = fuelStation.getStored_amount(fuelType); + if (amount > highestAmount) { + highestAmount = amount; + highestStation = fuelStation; + } + } catch (NoSuchFuelTypeError e) { + } } + + if (highestStation == null) { + throw new NoSuchFuelTypeError("No fuel station with " + fuelType + " found"); + } return highestStation; } @@ -191,14 +211,24 @@ public class Main { * @author Nils Göbbert */ public static FuelStation getLowestStoredAmount(FuelType fuelType) throws NoSuchFuelTypeError { - float lowestAmount = fuelStations.get(0).getStored_amount(fuelType); - FuelStation lowestStation = fuelStations.get(0); + FuelStation lowestStation = null; + float lowestAmount = Float.POSITIVE_INFINITY; + for (FuelStation fuelStation : fuelStations) { - if (fuelStation.getStored_amount(fuelType) > lowestAmount) { - lowestAmount = fuelStation.getStored_amount(fuelType); - lowestStation = fuelStation; + try { + float amount = fuelStation.getStored_amount(fuelType); + if (amount < lowestAmount) { + lowestAmount = amount; + lowestStation = fuelStation; + } + } catch (NoSuchFuelTypeError e) { + } } + + if (lowestStation == null) { + throw new NoSuchFuelTypeError("No fuel station with " + fuelType + " found"); + } return lowestStation; } -- 2.43.0