-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoffeeMachine.java
149 lines (124 loc) · 5.13 KB
/
CoffeeMachine.java
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
//package machine;
import java.util.Scanner;
public class CoffeeMachine {
static int money = 550, water = 400, milk = 540, beans = 120, cups = 9;
final static int espressoCost = 4, latteCost = 7, cappuccinoCost = 6;
final static int espressoWater = 250, latteWater = 350, cappuccinoWater = 200;
final static int latteMilk = 75, cappuccinoMilk = 100;
final static int espressoBeans = 16, latteBeans = 20, cappuccinoBeans = 12;
final static Scanner scanner = new Scanner(System.in);
static String coffeeSelect;
public static void main(String[] args) {
while (true) {
System.out.println("Write action (buy, fill, take, remaining, exit):");
String action = scanner.next();
if (action.equals("buy")) {
System.out.print("What do you want to buy? ");
System.out.println("1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:");
coffeeSelect = scanner.next();
if (coffeeSelect.equals("back")) {
continue;
} else {
buy();
}
} else if (action.equals("fill")) {
fill();
} else if (action.equals("take")) {
take();
} else if (action.equals("remaining")) {
status();
} else {
break;
}
}
}
public static void status() {
System.out.println();
System.out.println("The coffee machine has:");
System.out.println(water + " of water");
System.out.println(milk + " of milk");
System.out.println(beans + " of coffee beans");
System.out.println(cups + " of disposable cups");
System.out.println(money + " of money");
}
public static void buy() {
if (coffeeSelect.equals("1")) {
if (totalEspressoCups() > 0) {
System.out.println("I have enough resources, making you a coffee!");
water = water - espressoWater;
beans = beans - espressoBeans;
money = money + espressoCost;
cups = cups - 1;
} else {
if (water / espressoWater == 0) {
System.out.println("Sorry, not enough water!");
} else {
System.out.println("Sorry, not enough coffee beans!");
}
}
} else if (coffeeSelect.equals("2")) {
if (totalLatteCups() > 0) {
System.out.println("I have enough resources, making you a coffee!");
water -= latteWater;
milk -= latteMilk;
beans -= latteBeans;
money += latteCost;
cups -= 1;
} else {
if (water / latteWater == 0) {
System.out.println("Sorry, not enough water!");
} else if (beans / latteBeans == 0) {
System.out.println("Sorry, not enough coffee beans!");
} else {
System.out.println("Sorry, not enough milk!");
}
}
} else if (coffeeSelect.equals("3")) {
if (totalCappuccinoCups() > 0) {
System.out.println("I have enough resources, making you a coffee!");
water -= cappuccinoWater;
milk -= cappuccinoMilk;
beans -= cappuccinoBeans;
money += cappuccinoCost;
cups -= 1;
} else {
if (water / cappuccinoWater == 0) {
System.out.println("Sorry, not enough water!");
} else if (beans / cappuccinoBeans == 0) {
System.out.println("Sorry, not enough coffee beans!");
} else {
System.out.println("Sorry, not enough milk!");
}
}
} else {
System.out.println("Enter valid action");
}
}
public static void fill() {
System.out.println("Write how many ml of water do you want to add:");
int fillWater = scanner.nextInt();
System.out.println("Write how many ml of milk do you want to add:");
int fillMilk = scanner.nextInt();
System.out.println("Write how many grams of coffee beans do you want to add:");
int fillBeans = scanner.nextInt();
System.out.println("Write how many disposable cups of coffee do you want to add:");
int fillCups = scanner.nextInt();
water += fillWater;
milk += fillMilk;
beans += fillBeans;
cups += fillCups;
}
public static void take() {
System.out.println("I gave you $" + money);
money = 0;
}
public static int totalEspressoCups() {
return Math.min(water / espressoWater, beans / espressoBeans);
}
public static int totalLatteCups() {
return Math.min(Math.min(water / latteWater, milk / latteMilk), beans / latteBeans);
}
public static int totalCappuccinoCups() {
return Math.min(Math.min(water / cappuccinoWater, milk / cappuccinoMilk), beans / cappuccinoBeans);
}
}