Files
SoftwareEngineeringCourse-Java/src/lab7_state/ex1/Machine.java
2024-11-01 16:51:39 +01:00

102 lines
2.2 KiB
Java

package lab7_state.ex1;
import lab7_state.ex1.states.*;
public class Machine {
private MachineState currentState;
private final MachineState offState;
private final MachineState idleState;
private final MachineState choiceState;
private final MachineState serviceNeededState;
private final MachineState coffeeReadyState;
private int cups;
private boolean jammed = false;
public Machine() {
offState = new OffState(this);
idleState = new IdleState(this);
choiceState = new ChoiceState(this);
serviceNeededState = new ServiceNeededState(this);
coffeeReadyState = new CoffeeReadyState(this);
currentState = offState;
this.setCups(3);
}
public MachineState getOffState() {
return offState;
}
public MachineState getIdleState() {
return idleState;
}
public MachineState getChoiceState() {
return choiceState;
}
public MachineState getServiceNeededState() {
return serviceNeededState;
}
public MachineState getCoffeeReadyState() {
return coffeeReadyState;
}
public void returnMoney(double value) {
System.out.println("Returning CHF " + value);
}
public void setCurrentState(MachineState currentState) {
System.out.println(this.currentState.toString() + " -> " + currentState);
this.currentState = currentState;
}
public void powerUp() {
currentState.powerUp();
}
public void insertCoin(double value) {
currentState.insertCoin(value);
}
public void returnCoin() {
currentState.returnCoin();
}
public void pushButton() {
currentState.pushButton();
}
public void resetButton() {
currentState.reset();
}
public void removeCup() {
currentState.removeCup();
}
public void setCups(int cups) {
this.cups = cups;
}
public boolean hasCups() {
return cups != 0;
}
public boolean hasCoffee() {
return true;
}
public boolean isJammed() {
return jammed;
}
public void makeCoffee() {
cups--;
if (Math.random() < 0.2) {
jammed = true;
}
}
}