-
Notifications
You must be signed in to change notification settings - Fork 23
/
SoldState.java
44 lines (37 loc) · 1.29 KB
/
SoldState.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package GumballMachineOneInTenGame;
public class SoldState implements State {
GumballMachine gumballMachine;
public SoldState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
@Override
public void insertQuarter() {
// inappropriate action
System.out.println("Please wait, we're already giving you a gumball");
}
@Override
public void ejectQuarter() {
// inappropriate action
System.out.println("Sorry, you already turned the crank");
}
@Override
public void turnCrank() {
// inappropriate action
System.out.println("Turning twice doesn't get you another gumball!");
}
@Override
public void dispense() {
// here's where the real work begins
// we first need to ask the machine to release a gumball
gumballMachine.releaseBall();
// we ask the machine what the gumball count is
if (gumballMachine.getCount() > 0) {
// transition to NoQuarterState
gumballMachine.setState(gumballMachine.getNoQuarterState());
} else {
// transition to SoldOutState
System.out.println("Oops, out of gumballs!");
gumballMachine.setState(gumballMachine.getSoldOutState());
}
}
}