]> Git Server - tankstelle.git/commitdiff
Methods to add new fuel stations and to calculate the total stock level of a fuel...
authorLeander Schnurrer <lschnurrer@gmx.de>
Mon, 17 Nov 2025 16:11:36 +0000 (17:11 +0100)
committerLeander Schnurrer <lschnurrer@gmx.de>
Mon, 17 Nov 2025 16:11:36 +0000 (17:11 +0100)
src/main/java/de/diejungsvondertanke/tankstelle/Main.java

index 729ba11993c071e38fa9d5476156afacdcd99595..9a25493cdf9ed40b0ba7d7f21e2c81777ab324f6 100644 (file)
@@ -4,6 +4,7 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
+import java.util.Arrays;
 
 import de.diejungsvondertanke.tankstelle.error.NoSuchFuelTypeError;
 
@@ -17,17 +18,25 @@ public class Main {
     /**
      * (Initial) array of all {@link FuelStation}s
      */
-    static FuelStation[] fuelStations = { new SmallFuelStation((short) 0), new SmallFuelStation((short) 0),
+    static FuelStation[] initialFuelStations = { new SmallFuelStation((short) 0), new SmallFuelStation((short) 0),
             new MediumFuelStation(0), new MediumFuelStation(0), new MediumFuelStation(0),
             new LargeFuelStation("PlatzhalterFirma1"), new LargeFuelStation("PlatzhalterFirma2"),
             new LargeFuelStation("PlatzhalterFirma3") };
 
+    /**
+     * create an array list to store the fuel stations while the program is running
+     * add the initial array of fuel stations to the List
+     */
+    static ArrayList<FuelStation> fuelStations = new ArrayList<FuelStation>(Arrays.asList(initialFuelStations));
+
+
     /**
      * Main method
      * 
      * @param args Program arguments (not in use)
      */
     public static void main(String[] args) {
+
     }
 
     /**
@@ -104,8 +113,8 @@ public class Main {
      * @author Nils Göbbert
      */
     public static FuelStation getHighestPrice(FuelType fuelType) throws NoSuchFuelTypeError {
-        float highestPrice = fuelStations[0].get_price(fuelType);
-        FuelStation highestStation = fuelStations[0];
+        float highestPrice = fuelStations.toArray(new FuelStation[0])[0].get_price(fuelType);
+        FuelStation highestStation = fuelStations.toArray(new FuelStation[0])[0];
         for (FuelStation fuelStation : fuelStations) {
             if (fuelStation.get_price(fuelType) > highestPrice) {
                 highestPrice = fuelStation.get_price(fuelType);
@@ -127,8 +136,8 @@ public class Main {
      * @author Nils Göbbert
      */
     public static FuelStation getHighestAccumulatedValue() throws NoSuchFuelTypeError {
-        float highestValue = fuelStations[0].get_cumulative_retail_price();
-        FuelStation highestStation = fuelStations[0];
+        float highestValue = fuelStations.toArray(new FuelStation[0])[0].get_cumulative_retail_price();
+        FuelStation highestStation = fuelStations.toArray(new FuelStation[0])[0];
         for (FuelStation fuelStation : fuelStations) {
             if (fuelStation.get_cumulative_retail_price() > highestValue) {
                 highestValue = fuelStation.get_cumulative_retail_price();
@@ -152,8 +161,8 @@ public class Main {
      * @author Nils Göbbert
      */
     public static FuelStation getHighestStoredAmount(FuelType fuelType) throws NoSuchFuelTypeError {
-        float highestAmount = fuelStations[0].getStored_amount(fuelType);
-        FuelStation highestStation = fuelStations[0];
+        float highestAmount = fuelStations.toArray(new FuelStation[0])[0].getStored_amount(fuelType);
+        FuelStation highestStation = fuelStations.toArray(new FuelStation[0])[0];
         for (FuelStation fuelStation : fuelStations) {
             if (fuelStation.getStored_amount(fuelType) > highestAmount) {
                 highestAmount = fuelStation.getStored_amount(fuelType);
@@ -177,8 +186,8 @@ public class Main {
      * @author Nils Göbbert
      */
     public static FuelStation getLowestStoredAmount(FuelType fuelType) throws NoSuchFuelTypeError {
-        float lowestAmount = fuelStations[0].getStored_amount(fuelType);
-        FuelStation lowestStation = fuelStations[0];
+        float lowestAmount = fuelStations.toArray(new FuelStation[0])[0].getStored_amount(fuelType);
+        FuelStation lowestStation = fuelStations.toArray(new FuelStation[0])[0];
         for (FuelStation fuelStation : fuelStations) {
             if (fuelStation.getStored_amount(fuelType) > lowestAmount) {
                 lowestAmount = fuelStation.getStored_amount(fuelType);
@@ -219,7 +228,7 @@ public class Main {
      */
     public static FuelStation[] getFuelStationListWhenUnderstaffed(short available_employees) {
         ArrayList<FuelStation> result = new ArrayList<>();
-        FuelStation[] fuelStationsLocalCopy = FuelStation.sortDescending(fuelStations);
+        FuelStation[] fuelStationsLocalCopy = FuelStation.sortDescending(fuelStations.toArray(new FuelStation[0]));
         for (FuelStation station : fuelStationsLocalCopy) {
             if (Math.floorDiv(available_employees, station.getNumber_of_employees()) >= 1) {
                 available_employees -= station.getNumber_of_employees();
@@ -244,4 +253,71 @@ public class Main {
         }
         return sum;
     }
+
+
+    /**
+     * Add new large fuel stations
+     * @param supermarket_company add the special attribut for large fuel stations
+     * 
+     * @author Leander Schnurrer
+     */
+    public static void addNewFuelstation(String supermarket_company){
+        while(supermarket_company.isEmpty()){
+            // Show error in UI
+        }
+        fuelStations.add(new LargeFuelStation(supermarket_company));
+    }
+
+    /**
+     * Add new medium fuel stations
+     * @param retail_space add the special attribut for medium fuel stations
+     * 
+     * @author Leander Schnurrer
+     */
+    public static void addNewFuelstation(float retail_space){
+        while(retail_space <= 0) {
+            // Show error in UI
+        }
+        fuelStations.add(new MediumFuelStation(retail_space));
+    }
+
+    /**
+     * Add new small fuel stations
+     * @param number_of_vending_machines add the special attribut for small fuel stations
+     * 
+     * @author Leander Schnurrer
+     */
+    public static void addNewFuelstation(short number_of_vending_machines){
+        while(number_of_vending_machines <= 0) {
+            // Show error in UI
+        }
+        fuelStations.add(new SmallFuelStation(number_of_vending_machines));
+    }
+
+
+    /**
+     * Determine the total stock levels of selected petrol stations for a given type of fuel
+     * 
+     * @param fuelType select the fuel type
+     * @param FuelStations select the fuel stations 
+     * 
+     * @return the sum of stock levels of the specific fuel type from the selected fuel stations
+     * 
+     * @author Leander Schnurrer
+     */
+    public static float getTotalStockLevelOfFuel(FuelType fuelType, FuelStation[] FuelStations) {
+        float sum = 0f;
+        for(FuelStation fuelStation : FuelStations){
+            for(Fuel fuel : fuelStation.fuels){
+                if(fuel.getFuelType() == fuelType){
+                    sum += fuel.getStored_amount();
+                    break;
+                }
+            }
+        }
+
+        return sum;
+    }
+
+
 }
\ No newline at end of file