From: Nils Date: Mon, 10 Nov 2025 12:12:50 +0000 (+0100) Subject: Fuel seting, adding and substacting X-Git-Url: https://git.eternal.ddnss.de/?a=commitdiff_plain;h=22fd5d3a2b8d054b5701d7ab240fcdcffe194160;p=tankstelle.git Fuel seting, adding and substacting --- diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/FuelStation.java b/src/main/java/de/diejungsvondertanke/tankstelle/FuelStation.java index 31be9e6..102f499 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/FuelStation.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/FuelStation.java @@ -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"); } /**