diff --git a/src/lab7_state/ex1/Machine.java b/src/lab7_state/ex1/Machine.java new file mode 100644 index 0000000..8ad4e6a --- /dev/null +++ b/src/lab7_state/ex1/Machine.java @@ -0,0 +1,101 @@ +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; + } + } +} diff --git a/src/lab7_state/ex1/MachineState.java b/src/lab7_state/ex1/MachineState.java new file mode 100644 index 0000000..8ad207b --- /dev/null +++ b/src/lab7_state/ex1/MachineState.java @@ -0,0 +1,21 @@ +package lab7_state.ex1; + +public abstract class MachineState { + protected Machine machine; + + public MachineState(Machine machine) { + this.machine = machine; + } + + protected void powerUp() {} + protected void insertCoin(double value) {} + protected void returnCoin() {} + protected void reset() {} + protected void pushButton() {} + protected void removeCup() {} + + @Override + public String toString() { + return getClass().getSimpleName(); + } +} diff --git a/src/lab7_state/ex1/Main.java b/src/lab7_state/ex1/Main.java new file mode 100644 index 0000000..a69bf46 --- /dev/null +++ b/src/lab7_state/ex1/Main.java @@ -0,0 +1,29 @@ +package lab7_state.ex1; + +public class Main { + public static void main(String[] args) { + Machine machine = new Machine(); + System.out.println("A"); + machine.powerUp(); + machine.insertCoin(1); + + System.out.println("B"); + machine.insertCoin(0.25); + machine.returnCoin(); + + System.out.println("C"); + machine.insertCoin(0.25); + machine.pushButton(); + machine.removeCup(); + + for (int i = 0; i < 4; i++) { + System.out.println("D" + i); + machine.insertCoin(0.25); + machine.pushButton(); + machine.removeCup(); + } + + System.out.println("E"); + machine.resetButton(); + } +} diff --git a/src/lab7_state/ex1/states/ChoiceState.java b/src/lab7_state/ex1/states/ChoiceState.java new file mode 100644 index 0000000..aa6975e --- /dev/null +++ b/src/lab7_state/ex1/states/ChoiceState.java @@ -0,0 +1,32 @@ +package lab7_state.ex1.states; + +import lab7_state.ex1.Machine; +import lab7_state.ex1.MachineState; + +public class ChoiceState extends MachineState { + public ChoiceState(Machine machine) { + super(machine); + } + + @Override + protected void returnCoin() { + machine.returnMoney(0.25); + machine.setCurrentState(machine.getIdleState()); + } + + @Override + protected void pushButton() { + if (!machine.hasCups()) { + System.out.println("The machine is out of cups"); + } else if (!machine.hasCoffee()) { + System.out.println("The machine is out of coffee beans"); + } else if (machine.isJammed()) { + System.out.println("The machine is jammed"); + } else { + machine.makeCoffee(); + machine.setCurrentState(machine.getCoffeeReadyState()); + return; + } + machine.setCurrentState(machine.getServiceNeededState()); + } +} diff --git a/src/lab7_state/ex1/states/CoffeeReadyState.java b/src/lab7_state/ex1/states/CoffeeReadyState.java new file mode 100644 index 0000000..fb5eeba --- /dev/null +++ b/src/lab7_state/ex1/states/CoffeeReadyState.java @@ -0,0 +1,15 @@ +package lab7_state.ex1.states; + +import lab7_state.ex1.Machine; +import lab7_state.ex1.MachineState; + +public class CoffeeReadyState extends MachineState { + public CoffeeReadyState(Machine machine) { + super(machine); + } + + @Override + protected void removeCup() { + machine.setCurrentState(machine.getIdleState()); + } +} diff --git a/src/lab7_state/ex1/states/IdleState.java b/src/lab7_state/ex1/states/IdleState.java new file mode 100644 index 0000000..68b79ac --- /dev/null +++ b/src/lab7_state/ex1/states/IdleState.java @@ -0,0 +1,20 @@ +package lab7_state.ex1.states; + +import lab7_state.ex1.Machine; +import lab7_state.ex1.MachineState; + +public class IdleState extends MachineState { + public IdleState(Machine machine) { + super(machine); + } + + @Override + protected void insertCoin(double value) { + if (value != 0.25) { + machine.returnMoney(value); + } else { + System.out.println("Please choose your coffee"); + machine.setCurrentState(machine.getChoiceState()); + } + } +} diff --git a/src/lab7_state/ex1/states/OffState.java b/src/lab7_state/ex1/states/OffState.java new file mode 100644 index 0000000..d4aad99 --- /dev/null +++ b/src/lab7_state/ex1/states/OffState.java @@ -0,0 +1,15 @@ +package lab7_state.ex1.states; + +import lab7_state.ex1.Machine; +import lab7_state.ex1.MachineState; + +public class OffState extends MachineState { + public OffState(Machine machine) { + super(machine); + } + + @Override + protected void powerUp() { + machine.setCurrentState(machine.getIdleState()); + } +} diff --git a/src/lab7_state/ex1/states/ServiceNeededState.java b/src/lab7_state/ex1/states/ServiceNeededState.java new file mode 100644 index 0000000..f0a49a9 --- /dev/null +++ b/src/lab7_state/ex1/states/ServiceNeededState.java @@ -0,0 +1,15 @@ +package lab7_state.ex1.states; + +import lab7_state.ex1.Machine; +import lab7_state.ex1.MachineState; + +public class ServiceNeededState extends MachineState { + public ServiceNeededState(Machine machine) { + super(machine); + } + + @Override + protected void reset() { + machine.setCurrentState(machine.getIdleState()); + } +}