added lab7 ex1
This commit is contained in:
parent
8cefde97d3
commit
8f38c89d7c
101
src/lab7_state/ex1/Machine.java
Normal file
101
src/lab7_state/ex1/Machine.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
21
src/lab7_state/ex1/MachineState.java
Normal file
21
src/lab7_state/ex1/MachineState.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
29
src/lab7_state/ex1/Main.java
Normal file
29
src/lab7_state/ex1/Main.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
32
src/lab7_state/ex1/states/ChoiceState.java
Normal file
32
src/lab7_state/ex1/states/ChoiceState.java
Normal file
@ -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());
|
||||||
|
}
|
||||||
|
}
|
15
src/lab7_state/ex1/states/CoffeeReadyState.java
Normal file
15
src/lab7_state/ex1/states/CoffeeReadyState.java
Normal file
@ -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());
|
||||||
|
}
|
||||||
|
}
|
20
src/lab7_state/ex1/states/IdleState.java
Normal file
20
src/lab7_state/ex1/states/IdleState.java
Normal file
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
src/lab7_state/ex1/states/OffState.java
Normal file
15
src/lab7_state/ex1/states/OffState.java
Normal file
@ -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());
|
||||||
|
}
|
||||||
|
}
|
15
src/lab7_state/ex1/states/ServiceNeededState.java
Normal file
15
src/lab7_state/ex1/states/ServiceNeededState.java
Normal file
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user