-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoffeeMachine.java
More file actions
164 lines (130 loc) · 4.47 KB
/
CoffeeMachine.java
File metadata and controls
164 lines (130 loc) · 4.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
package machine;
import java.util.Scanner;
public class CoffeeMachine {
private static final Scanner scanner = new Scanner(System.in);
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 printResources() {
System.out.println("The coffee machine has");
System.out.println(water + " ml of water");
System.out.println(milk + " ml of milk");
System.out.println(coffeeBeans + " g of coffee beans");
System.out.println(cups + " disposable cups");
System.out.println("$" + money + " of money");
System.out.println();
}
public static void buyCappuccino() {
processDrink(200, 100, 12, 6);
}
public static void buyLatte() {
processDrink(350, 75, 20, 7);
}
public static void buyEspresso() {
processDrink(250, 0, 16, 4);
}
public static void processDrink(int waterPerCup, int milkPerCup, int coffeeBeansPerCup, int moneyTaken) {
if (water < waterPerCup) {
System.out.println("Sorry, not enough water!");
} else if (milk < milkPerCup) {
System.out.println("Sorry, not enough milk!");
} else if (coffeeBeans < coffeeBeansPerCup) {
System.out.println("Sorry, not enough coffee beans!");
} else if (cups < 1) {
System.out.println("Sorry, not enough disposable cups!");
} else {
water -= waterPerCup;
milk -= milkPerCup;
coffeeBeans -= coffeeBeansPerCup;
money += moneyTaken;
cups--;
System.out.println("I have enough resources, making you a coffee!");
}
System.out.println();
}
public static void Buy(String coffeeType) {
switch (coffeeType) {
case "1":
buyEspresso();
break;
case "2":
buyLatte();
break;
case "3":
buyCappuccino();
break;
case "back":
return;
default:
break;
}
}
public static void fillResources() {
int waterAdded = getWater();
int milkAdded = getMilk();
int coffeeAdded = getCoffee();
int cupsAdded = getCups();
water += waterAdded;
milk += milkAdded;
coffeeBeans += coffeeAdded;
cups += cupsAdded;
System.out.println();
}
public static void takeMoney() {
System.out.println("I gave you $" + money);
money = 0;
System.out.println();
}
public static String getCoffeeType() {
System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:");
return scanner.nextLine();
}
public static int getWater() {
System.out.println("Write how many ml of water you want to add: ");
return Integer.parseInt(scanner.nextLine());
}
public static int getMilk() {
System.out.println("Write how many ml of milk you want to add: ");
return Integer.parseInt(scanner.nextLine());
}
public static int getCoffee() {
System.out.println("Write how many grams of coffee beans you want to add:");
return Integer.parseInt(scanner.nextLine());
}
public static int getCups() {
System.out.println("Write how many disposable cups you want to add: ");
return Integer.parseInt(scanner.nextLine());
}
public static String mainMenu() {
System.out.println("Write action (buy, fill, take, remaining, exit):");
return scanner.nextLine();
}
public static void main(String[] args) {
while (true) {
String action = mainMenu();
System.out.println();
switch (action) {
case "buy":
String coffeeType = getCoffeeType();
Buy(coffeeType);
break;
case "fill":
fillResources();
break;
case "take":
takeMoney();
break;
case "remaining":
printResources();
break;
case "exit":
System.exit(0);
break;
default:
break;
}
}
}
}