]> Git Server - tankstelle.git/commitdiff
Fehler beim highest und lowest stored amount und beim highest price gefixt
authorSergej Pavlenko <sergepav0@gmail.com>
Wed, 19 Nov 2025 23:16:04 +0000 (00:16 +0100)
committerSergej Pavlenko <sergepav0@gmail.com>
Wed, 19 Nov 2025 23:16:04 +0000 (00:16 +0100)
src/main/java/de/diejungsvondertanke/tankstelle/Main.java

index 312455b1b6b367e0a6373ed8cda64e28e0f30866..4ff3123b1610a051b541afd5a618ee4cdb784b99 100644 (file)
@@ -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;
     }