From: Leander Schnurrer Date: Mon, 17 Nov 2025 16:11:36 +0000 (+0100) Subject: Methods to add new fuel stations and to calculate the total stock level of a fuel... X-Git-Url: https://git.eternal.ddnss.de/?a=commitdiff_plain;h=5be1ba7a03daef37fd877711725e08e734ccb68d;p=tankstelle.git Methods to add new fuel stations and to calculate the total stock level of a fuel type --- diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/Main.java b/src/main/java/de/diejungsvondertanke/tankstelle/Main.java index 729ba11..9a25493 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/Main.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/Main.java @@ -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 fuelStations = new ArrayList(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 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