Add oving3
This commit is contained in:
59
src/main/java/oving3/debugging/CoffeeCup.java
Normal file
59
src/main/java/oving3/debugging/CoffeeCup.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package oving3.debugging;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user