-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVendingMachine.java
More file actions
160 lines (127 loc) · 4.13 KB
/
VendingMachine.java
File metadata and controls
160 lines (127 loc) · 4.13 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package enumerated;
import java.util.*;
import net.mindview.util.*;
import static enumerated.Input.*;
/**
* RUN:
* javac enumerated/VendingMachine.java && java enumerated.VendingMachine
* OUTPUT:
*
*/
enum Category {
MONEY (NICKEL, DIME, QUARTER, DOLLAR),
ITEM_SELECTION (TOOTHPASTE, CHIPS, SODA, SOAP),
QUIT_TRANSACTION (ABORT_TRANSACTION),
SHUT_DOWN (STOP);
private Input[] values;
Category(Input...types) { values = types; }
private static EnumMap<Input, Category> categories = new EnumMap<Input, Category>(Input.class);
static {
for (Category c : Category.class.getEnumConstants()) {
for (Input type : c.values) {
categories.put(type, c);
}
}
}
public static Category categorize(Input input) {
return categories.get(input);
}
}
public class VendingMachine {
private static State state = State.RESTING;
private static int amount = 0;
private static Input selection = null;
enum StateDuration { TRANSIENT }
enum State {
RESTING {
void next(Input input) {
switch(Category.categorize(input)) {
case MONEY:
amount += input.amount();
state = ADDING_MONEY;
break;
case SHUT_DOWN:
state = TERMINAL;
default:
}
}
},
ADDING_MONEY {
void next(Input input) {
switch (Category.categorize(input)) {
case MONEY:
amount += input.amount();
break;
case ITEM_SELECTION:
selection = input;
if (amount < selection.amount()) {
System.out.println("Insufficient money for " + selection);
}
else {
state = DISPENSING;
}
break;
case QUIT_TRANSACTION:
state = GIVING_CHANGE;
break;
case SHUT_DOWN:
state = TERMINAL;
default:
}
}
},
DISPENSING (StateDuration.TRANSIENT) {
void next() {
System.out.println("here is your " + selection);
amount -= selection.amount();
state = GIVING_CHANGE;
}
},
GIVING_CHANGE (StateDuration.TRANSIENT) {
void next() {
if (amount > 0) {
System.out.println("Your change: " + amount);
amount = 0;
}
state = RESTING;
}
},
TERMINAL {
void output() {
System.out.println("Halted");
}
};
private boolean isTransient = false;
State() {}
State (StateDuration trans) {
isTransient = true;
}
void next(Input input) {
throw new RuntimeException("Only call next(Input input) for non-transient states");
}
void next() {
throw new RuntimeException("Only call next() for StateDuration.TRANSIENT states");
}
void output() {
System.out.println(amount);
}
}
static void run(Generator<Input> gen) {
while (state != State.TERMINAL) {
state.next(gen.next());
while (state.isTransient) {
state.next();
}
state.output();
}
}
public static void main(String[] args) {
Generator<Input> gen = new RandomInputGenerator();
run(gen);
}
}
class RandomInputGenerator implements Generator<Input> {
public Input next() {
return Input.randomSelection();
}
}