-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathAutomata.java
More file actions
100 lines (83 loc) · 2.17 KB
/
Copy pathAutomata.java
File metadata and controls
100 lines (83 loc) · 2.17 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
import java.util.HashMap;
public class Automata {
enum STATES {
OFF, WAIT, ACCEPT, CHECK, COOK
}
private int cash;
private STATES state;
static HashMap <String, String> prices = new HashMap<String, String>();
static {
prices.put("1", "Coffee with milk,30");
prices.put("2", "Green Tea,40");
prices.put("3", "Cappuchino,50");
prices.put("4", "Latthe,30");
prices.put("5", "Orange Juice,40");
}
public Automata() {
state = STATES.OFF;
this.cash = 0;
}
public STATES getState() {
return state;
}
public void setState(STATES state) {
this.state = state;
}
public int getCash() {
return cash;
}
public void setCash(int cash) {
this.cash = cash;
}
public void on() {
if (state == STATES.OFF) {
state = STATES.WAIT;
}
}
public void off() {
state = STATES.OFF;
}
public void coin(int amount) {
if (state == STATES.WAIT || state == STATES.ACCEPT) {
state = STATES.ACCEPT;
cash +=amount;
}
}
public void choice(String menuNum) {
if (state != STATES.OFF) {
if (check(menuNum)) {
cook(menuNum);
} else {
cancel();
}
}
}
private boolean check(String menuNum) {
state = STATES.CHECK;
return Integer.parseInt(prices.get(menuNum).split(",")[1]) <= cash;
}
public void printState() {
System.out.println("Current state is " + state);
}
public void printMenu(){
prices.forEach((k,v) -> System.out.println(" "+k+" "+v));
}
private void cook (String menuNum){
state = STATES.COOK;
finish(menuNum);
}
public int cancel() {
int sum = cash;
if (state == STATES.ACCEPT || state == STATES.CHECK ) {
cash = 0;
state = STATES.WAIT;
} else {
sum = -1;
}
return sum;
}
private void finish(String menuNum){
state = STATES.WAIT;
cash -= Integer.parseInt(prices.get(menuNum).split(",")[1]);
}
}