Repository navigation
Expand file tree
/
Copy pathCoffeeMachineFinal.java
More file actions
199 lines (154 loc) · 5.86 KB
/
Copy pathCoffeeMachineFinal.java
File metadata and controls
199 lines (154 loc) · 5.86 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
197
198
199
import java.util.Scanner;
class Coffee{
private final int water;
private final int milk;
private final int beans;
private final int money;
public Coffee(int water, int milk, int beans, int money){
this.water = water;
this.milk = milk;
this.beans = beans;
this.money = money;
}
public int getWater(){
return water;
}
public int getMilk(){
return milk;
}
public int getBeans(){
return beans;
}
public int getMoney(){
return money;
}
}
class CoffeeMachine{
private int availableMoney = 550;
private int availableWater = 400;
private int availableMilk = 540;
private int availableBeans = 120;
private int availableDisposalCups = 9;
private byte count = 0;
private static final byte CLEANING_LIMIT = 10;
private final Coffee espresso = new Coffee(200, 0, 16, 4);
private final Coffee latte = new Coffee(350, 75, 20, 7);
private final Coffee cappuccino = new Coffee(200, 100, 12, 6);
public void start(){
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("Write action (1.buy, 2.fill, 3.take, 4.remaining, 5.clean, 6.exit) : ");
byte choice = sc.nextByte();
switch(choice){
case 1 :{
if(count >= CLEANING_LIMIT){
System.out.println("I need cleaning!");
}
else{
buy(sc);
}
break;
}
case 2 :{
fill(sc);
break;
}
case 3 : {
take();
break;
}
case 4 : {
display();
break;
}
case 5 : {
count = 0;
System.out.println("I have been cleaned!");
break;
}
case 6 : {
sc.close();
System.out.println("Thank you for visiting!");
System.out.println("---------------------------------------------------------------------------------------------");
return;
}
default : System.out.println("Invalid Input!");
}
}
}
private void buy(Scanner sc){
System.out.println("What do you want to buy? (1.espresso, 2.latte, 3.cappuccino) :");
byte type = sc.nextByte();
switch(type){
case 1 : {
makeCoffee(espresso);
break;
}
case 2 : {
makeCoffee(latte);
break;
}
case 3 : {
makeCoffee(cappuccino);
break;
}
default : System.out.println("Invalid Input!");
}
}
private void makeCoffee(Coffee coffee){
if(availableWater < coffee.getWater()){
System.out.println("Sorry, not enough water!");
return;
}
if(availableMilk < coffee.getMilk()){
System.out.println("Sorry, not enough milk!");
return;
}
if(availableBeans < coffee.getBeans()){
System.out.println("Sorry, not enough coffee beans!");
return;
}
if(availableDisposalCups < 1){
System.out.println("Sorry, not enough cups!");
return;
}
System.out.println("I have enough resources, making you a coffee!");
availableWater -= coffee.getWater();
availableMilk -= coffee.getMilk();
availableBeans -= coffee.getBeans();
availableMoney += coffee.getMoney();
availableDisposalCups--;
count++;
}
private void fill(Scanner sc){
System.out.println("Write how many ml of water you want to add : ");
availableWater += sc.nextInt();
System.out.println("Write how many ml of milk you want to add : ");
availableMilk += sc.nextInt();
System.out.println("Write how many grams of coffee beans you want to add :");
availableBeans += sc.nextInt();
System.out.println("Write how many disposable cups you want to add :");
availableDisposalCups += sc.nextInt();
System.out.println("---------------------------------------------------------------------------------------------");
}
private void take(){
System.out.println("I gave you $" + availableMoney);
availableMoney = 0;
}
private void display(){
System.out.println("---------------------------------------------------------------------------------------------");
System.out.println("The coffee machine has:");
System.out.println(availableWater + " ml of water");
System.out.println(availableMilk + " ml of milk");
System.out.println(availableBeans + " g of coffee beans");
System.out.println(availableDisposalCups + " disposable cups");
System.out.println("$" + availableMoney + " of money");
System.out.println("---------------------------------------------------------------------------------------------");
}
}
public class CoffeeMachineFinal {
public static void main(String[] args) {
CoffeeMachine machine = new CoffeeMachine();
machine.start();
}
}