* @author Nils Göbbert
*/
public static FuelStation getHighestPrice(FuelType fuelType) throws NoSuchFuelTypeError {
- float highestPrice = fuelStations.get(0).get_price(fuelType);
- FuelStation highestStation = fuelStations.get(0);
+ FuelStation highestStation = null;
+ float highestPrice = Float.NEGATIVE_INFINITY;
+
for (FuelStation fuelStation : fuelStations) {
- if (fuelStation.get_price(fuelType) > highestPrice) {
- highestPrice = fuelStation.get_price(fuelType);
- highestStation = fuelStation;
+ try {
+ float price = fuelStation.get_price(fuelType);
+ if (price > highestPrice) {
+ highestPrice = price;
+ highestStation = fuelStation;
+ }
+ } catch (NoSuchFuelTypeError e) {
+
}
}
+
+ if (highestStation == null) {
+ throw new NoSuchFuelTypeError("No fuel station with " + fuelType + " found");
+ }
return highestStation;
}
* @author Nils Göbbert
*/
public static FuelStation getHighestStoredAmount(FuelType fuelType) throws NoSuchFuelTypeError {
- float highestAmount = fuelStations.get(0).getStored_amount(fuelType);
- FuelStation highestStation = fuelStations.get(0);
+ FuelStation highestStation = null;
+ float highestAmount = Float.NEGATIVE_INFINITY;
+
for (FuelStation fuelStation : fuelStations) {
- if (fuelStation.getStored_amount(fuelType) > highestAmount) {
- highestAmount = fuelStation.getStored_amount(fuelType);
- highestStation = fuelStation;
+ try {
+ float amount = fuelStation.getStored_amount(fuelType);
+ if (amount > highestAmount) {
+ highestAmount = amount;
+ highestStation = fuelStation;
+ }
+ } catch (NoSuchFuelTypeError e) {
+
}
}
+
+ if (highestStation == null) {
+ throw new NoSuchFuelTypeError("No fuel station with " + fuelType + " found");
+ }
return highestStation;
}
* @author Nils Göbbert
*/
public static FuelStation getLowestStoredAmount(FuelType fuelType) throws NoSuchFuelTypeError {
- float lowestAmount = fuelStations.get(0).getStored_amount(fuelType);
- FuelStation lowestStation = fuelStations.get(0);
+ FuelStation lowestStation = null;
+ float lowestAmount = Float.POSITIVE_INFINITY;
+
for (FuelStation fuelStation : fuelStations) {
- if (fuelStation.getStored_amount(fuelType) > lowestAmount) {
- lowestAmount = fuelStation.getStored_amount(fuelType);
- lowestStation = fuelStation;
+ try {
+ float amount = fuelStation.getStored_amount(fuelType);
+ if (amount < lowestAmount) {
+ lowestAmount = amount;
+ lowestStation = fuelStation;
+ }
+ } catch (NoSuchFuelTypeError e) {
+
}
}
+
+ if (lowestStation == null) {
+ throw new NoSuchFuelTypeError("No fuel station with " + fuelType + " found");
+ }
return lowestStation;
}