Files
oops/src/main/java/oving4/testing/CoffeeCup.java
T
Andreas Omholt Olsen 7dd68c1ed8 Add oving 4
2026-02-02 10:57:55 +01:00

74 lines
1.7 KiB
Java

package oving4.testing;
public class CoffeeCup {
private double capacity;
private double currentVolume;
public CoffeeCup() {
this.capacity = 0.0;
this.currentVolume = 0.0;
}
public CoffeeCup(double capacity, double currentVolume) {
if (!CoffeeCup.isValidCapacity(capacity)) {
throw new IllegalArgumentException("Illegal capacity given.");
}
this.capacity = capacity;
if (!this.isValidVolume(currentVolume)) {
throw new IllegalArgumentException("Illegal volume given.");
}
this.currentVolume = currentVolume;
}
public double getCapacity() {
return this.capacity;
}
public double getCurrentVolume() {
return this.currentVolume;
}
private static boolean isValidCapacity(double capacity) {
return capacity >= 0.0;
}
public void increaseCupSize(double biggerCapacity) {
if (CoffeeCup.isValidCapacity(biggerCapacity)) {
this.capacity += biggerCapacity;
}
}
private boolean isValidVolume(double volume) {
return volume <= this.capacity && volume >= 0.0;
}
private boolean canDrink(double volume) {
return volume <= this.currentVolume;
}
public void drinkCoffee(double volume) {
if (!this.isValidVolume(volume) || !this.canDrink(volume)) {
throw new IllegalArgumentException("You cannot drink that much coffee!");
}
this.currentVolume -= volume;
}
public void fillCoffee(double volume) {
if (!this.isValidVolume(this.currentVolume + volume)) {
throw new IllegalArgumentException(
"You just poured coffee all over the table. Good job.");
}
this.currentVolume += volume;
}
@Override
public String toString() {
return String.format("[CoffeeCup capacity=%.2f, currentVolume=%.2f]", this.capacity,
this.currentVolume);
}
}