-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcoffeeMachine.java
More file actions
196 lines (170 loc) · 6.47 KB
/
coffeeMachine.java
File metadata and controls
196 lines (170 loc) · 6.47 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package done;
import java.util.Scanner;
class coffeeMachine {
private static final Scanner sc = new Scanner(System.in);
private static boolean isEnabled = true;
private static Status state = Status.MAINMENU;
private static int water = 400;
private static int milk = 540;
private static int coffeebeans = 120;
private static int cups = 9;
private static int money = 550;
public static void main(String[] args) {
while (isEnabled) {
switch(state) {
case MAINMENU:
chooseAction();
break;
case BUYING:
buyCoffee();
break;
case TAKING:
takeMoney();
break;
case FILLING:
fillMachine();
break;
case GETTING:
getInfo();
break;
case TURNINGOFF:
isEnabled = false;
break;
}
}
}
private enum Status {
MAINMENU,
BUYING,
FILLING,
TAKING,
GETTING,
TURNINGOFF
}
private static String checkInput() {
return sc.next();
}
private static void chooseAction() {
System.out.print("Write action (buy, fill, take, remaining, exit): ");
String action = checkInput();
System.out.println();
switch(action) {
case "buy":
state = Status.BUYING;
break;
case "fill":
state = Status.FILLING;
break;
case "take":
state = Status.TAKING;
break;
case "remaining":
state = Status.GETTING;
break;
case "exit":
state = Status.TURNINGOFF;
break;
default:
System.out.println("I cannot understand you. Please try again");
break;
}
}
private static void buyCoffee() {
System.out.print("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu: ");
String coffeeType = checkInput();
switch(coffeeType) {
case "1":
if (water >= 250 && coffeebeans >= 16 && cups >= 1) {
System.out.println("I have enough resources, making you a coffee!");
water -= 250;
coffeebeans -= 16;
money += 4;
cups -= 1;
} else {
if (water < 250) {
System.out.println("Sorry, not enough water!");
} else if (coffeebeans < 16) {
System.out.println("Sorry, not enough coffee beans!");
} else {
System.out.println("Sorry, not enough disposable cups!");
}
}
break;
case "2":
if (water >= 350 && milk >= 75 && coffeebeans >= 20 && cups >= 1) {
System.out.println("I have enough resources, making you a coffee!");
water -= 350;
milk -= 75;
coffeebeans -= 20;
money += 7;
cups -= 1;
} else {
if (water < 350) {
System.out.println("Sorry, not enough water!");
} else if (milk < 75) {
System.out.println("Sorry, not enough milk!");
} else if (coffeebeans < 20) {
System.out.println("Sorry, not enough coffee beans!");
} else {
System.out.println("Sorry, not enough disposable cups!");
}
}
break;
case "3":
if (water >= 200 && milk >= 100 && coffeebeans >= 12 && cups >= 1) {
System.out.println("I have enough resources, making you a coffee!");
water -= 200;
milk -= 100;
coffeebeans -= 12;
money += 6;
cups -= 1;
} else {
if (water < 200) {
System.out.println("Sorry, not enough water!");
} else if (milk < 100) {
System.out.println("Sorry, not enough milk!");
} else if (coffeebeans < 12) {
System.out.println("Sorry, not enough coffee beans!");
} else {
System.out.println("Sorry, not enough disposable cups!");
}
}
break;
case "back":
break;
default:
System.out.println("Sorry, I cannot understand you.");
break;
}
System.out.println();
state = Status.MAINMENU;
}
private static void fillMachine() {
System.out.print("Write how many ml of water do you want to add: ");
water += Integer.parseInt(checkInput());
System.out.print("Write how many ml of milk do you want to add: ");
milk += Integer.parseInt(checkInput());
System.out.print("Write how many grams of coffee beans do you want to add: ");
coffeebeans += Integer.parseInt(checkInput());
System.out.print("Write how many disposable cups of coffee do you want to add: ");
cups += Integer.parseInt(checkInput());
System.out.println();
state = Status.MAINMENU;
}
private static void getInfo() {
System.out.println("The coffee machine has:");
System.out.println(water + " of water");
System.out.println(milk + " of milk");
System.out.println(coffeebeans + " of coffee beans");
System.out.println(cups + " of disposable cups");
System.out.println(money + " of money");
System.out.println();
state = Status.MAINMENU;
}
private static void takeMoney() {
System.out.println("I gave you $" + money);
money = 0;
System.out.println();
state = Status.MAINMENU;
}
}