From: Robin Date: Mon, 10 Nov 2025 10:08:06 +0000 (+0100) Subject: Added set_price method in FuelStation X-Git-Url: https://git.eternal.ddnss.de/?a=commitdiff_plain;h=ff47ea55d3c903f91df95636e13d2ab265ab086e;p=tankstelle.git Added set_price method in FuelStation Added new exception: NoSuchFuelTypeError --- diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/FuelStation.java b/src/main/java/de/diejungsvondertanke/tankstelle/FuelStation.java index 8e09165..00eb456 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/FuelStation.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/FuelStation.java @@ -1,5 +1,7 @@ package de.diejungsvondertanke.tankstelle; +import de.diejungsvondertanke.tankstelle.error.NoSuchFuelTypeError; + /** * Abstract class. * All fuel station subtypes inherit from this class. @@ -45,8 +47,27 @@ abstract class FuelStation { this.fuels = fuels; } - public void set_stored_amount (float stored_amount, FuelType type){ - + public void set_stored_amount(float stored_amount, FuelType type) { + + } + + /** + * Set a new price for a specific type of fuel for this fuel station + * + * @param fuel_type The {@link FuelType} to change + * @param price The new price per litre + * @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_price(FuelType fuel_type, float price) throws NoSuchFuelTypeError { + for (Fuel i : fuels) { + if (i.FUEL_TYPE == fuel_type) { + i.setPrice(price); + return; + } + } + throw new NoSuchFuelTypeError("This fuel station does not have fuel of the given type"); } } diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/error/NoSuchFuelTypeError.java b/src/main/java/de/diejungsvondertanke/tankstelle/error/NoSuchFuelTypeError.java new file mode 100644 index 0000000..1ce5b47 --- /dev/null +++ b/src/main/java/de/diejungsvondertanke/tankstelle/error/NoSuchFuelTypeError.java @@ -0,0 +1,23 @@ +package de.diejungsvondertanke.tankstelle.error; + +/** + * This Error is thrown on the attempt to choose a fuel type from a fuel station + * which the fuel station does not have + */ +public class NoSuchFuelTypeError extends Exception { + /** + * Throws a {@link NoSuchFuelTypeError} + * + * @param message Message to throw with this error + */ + public NoSuchFuelTypeError(String message) { + super(message); + } + + /** + * Throws a {@link NoSuchFuelTypeError} + */ + public NoSuchFuelTypeError() { + super(); + } +}