From: Sergej Pavlenko Date: Wed, 19 Nov 2025 23:16:04 +0000 (+0100) Subject: Fehler beim highest und lowest stored amount und beim highest price gefixt X-Git-Url: https://git.eternal.ddnss.de/?a=commitdiff_plain;h=5dd1269dd7f1dfea873491583fd5aa6d10566d34;p=tankstelle.git Fehler beim highest und lowest stored amount und beim highest price gefixt --- 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; }