-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachine.java
More file actions
99 lines (82 loc) · 2.08 KB
/
Copy pathMachine.java
File metadata and controls
99 lines (82 loc) · 2.08 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package state;
import state.state.HasMoneyState;
import state.state.NoMoneyState;
import state.state.SoldOutState;
import state.state.SoldState;
import state.state.State;
import state.state.WinnerState;
/**
* 拥有多个状态state的售卖机对象
*/
public class Machine {
private int count = 0;
private State currentState;
//中奖状态
private State winnerState;
//未投币状态
private State noMoneyState;
//已投币状态
private State hasMoneyState;
//售卖状态
private State soldState;
//售空状态
private State soldOutState;
public Machine(int count) {
// 创建多个状态
winnerState = new WinnerState(this);
noMoneyState = new NoMoneyState(this);
hasMoneyState = new HasMoneyState(this);
soldOutState = new SoldOutState(this);
soldState = new SoldState(this);
if (count > 0) {
// 初始化售卖商品数量
this.count = count;
// 初始化售卖机当前状态
this.currentState = noMoneyState;
}
}
// 投币
public void insertMoney() {
currentState.insertMoney();
}
// 退币
public void backMoney() {
currentState.backMoney();
}
// 转动手柄
public void turnCrank() {
currentState.turnCrank();
}
// 出商品
public void dispense() {
System.out.println("售卖机发出商品");
if (count > 0) {
count--;
}
}
// 变更售卖机状态
public void changeState(State state) {
this.currentState = state;
}
public int getCount() {
return count;
}
public State getCurrentState() {
return currentState;
}
public State getWinnerState() {
return winnerState;
}
public State getNoMoneyState() {
return noMoneyState;
}
public State getHasMoneyState() {
return hasMoneyState;
}
public State getSoldState() {
return soldState;
}
public State getSoldOutState() {
return soldOutState;
}
}