]> Git Server - tankstelle.git/commitdiff
Fuel seting, adding and substacting
authorNils <adamantschild@gmail.com>
Mon, 10 Nov 2025 12:12:50 +0000 (13:12 +0100)
committerNils <adamantschild@gmail.com>
Mon, 10 Nov 2025 12:12:50 +0000 (13:12 +0100)
src/main/java/de/diejungsvondertanke/tankstelle/FuelStation.java

index 31be9e68c64d469d7d79d88a5c38fad70d92673c..102f49939d7a9512a3b1938cebed5f588f6700da 100644 (file)
@@ -46,9 +46,44 @@ abstract class FuelStation {
         this.size = size;
         this.fuels = fuels;
     }
-
-    public void set_stored_amount(float stored_amount, FuelType type) {
-
+    /**
+     * Set a new fuel amount for a specific type of fuel for this fuel station
+     * 
+     * @param type The {@link FuelType} to change
+     * @param new_amound     The new fuel amound
+     * @throws NoSuchFuelTypeError This Error is thrown on the attempt to choose a
+     *                             fuel type from a fuel station
+     *                             which the fuel station does not have
+     */
+    public void set_stored_amount(float new_amount, FuelType type)throws NoSuchFuelTypeError {
+         for (Fuel i : fuels) {
+            if (i.FUEL_TYPE == type) {
+                i.setStored_amount(new_amount);
+                return;
+            }
+            throw new NoSuchFuelTypeError("This fuel station does not have fuel of the given type");
+        }
+    }
+    /**
+     * Add or subtract from a fuel amount for a specific type of fuel for this fuel station
+     * 
+     * @param type The {@link FuelType} to change
+     * @param new_amound     The aound of fuel that is added or subtracted
+     * @throws NoSuchFuelTypeError This Error is thrown on the attempt to choose a
+     *                             fuel type from a fuel station
+     *                             which the fuel station does not have
+     */
+        public void add_stored_amount(float new_amount, FuelType type)throws NoSuchFuelTypeError , ArithmeticException {
+         for (Fuel i : fuels) {
+            if (i.FUEL_TYPE == type) {
+                if (new_amount + i.getStored_amount() < 0){
+                    throw new ArithmeticException("Fuel amound can't be negativ");
+                }
+                i.setStored_amount(new_amount + i.getStored_amount());
+                return;
+            }
+        }
+        throw new NoSuchFuelTypeError("This fuel station does not have fuel of the given type");
     }
 
     /**