-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoffeeMachine.java
More file actions
151 lines (136 loc) · 4.34 KB
/
Copy pathCoffeeMachine.java
File metadata and controls
151 lines (136 loc) · 4.34 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
package machine;
import java.util.Scanner;
public class CoffeeMachine {
final static 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 main(String[] args) {
boolean switchOn = true;
while (switchOn) {
System.out.println("Write action (buy, fill, take, remaining, exit):");
String action = scanner.next();
System.out.println();
switch (action) {
case "buy":
buy();
break;
case "fill":
fill();
break;
case "take":
take();
break;
case "remaining":
status();
break;
case "exit":
switchOn = false;
break;
default:
break;
}
System.out.println();
}
}
public static void buy() {
System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:");
String coffeeType = scanner.next();
switch (coffeeType) {
case "1":
// espresso
makeCoffee(250, 0, 16, 4);
break;
case "2":
// latte
makeCoffee(350, 75, 20, 7);
break;
case "3":
// cappuccino
makeCoffee(200, 100, 12, 6);
break;
case "back":
return;
default:
break;
}
System.out.println();
}
public static void fill() {
System.out.println("Write how many ml of water do you want to add:");
int water_ = scanner.nextInt();
System.out.println("Write how many ml of milk do you want to add:");
int milk_ = scanner.nextInt();
System.out.println("Write how many grams of coffee beans do you want to add:");
int coffeeBeans_ = scanner.nextInt();
System.out.println("Write how many disposable cups of coffee do you want to add:");
int cups_ = scanner.nextInt();
addWater(water_);
addMilk(milk_);
addCoffeeBeans(coffeeBeans_);
addCups(cups_);
}
public static void take() {
System.out.println("I gave you $" + money);
giveMoney();
}
private static void status() {
System.out.println("The coffee machine has:");
System.out.println(water + " of water");
System.out.println(milk + " of milk");
System.out.println(coffeeBeans + " coffee beans");
System.out.println(cups + " of disposable cups");
System.out.println(money + " of money");
}
private static void makeCoffee(int water_, int milk_, int coffeeBeans_, int money_) {
water -= water_;
milk -= milk_;
coffeeBeans -= coffeeBeans_;
cups -= 1;
if(!checkStatus()) {
addWater(water_);
addMilk(milk_);
addCoffeeBeans(coffeeBeans_);
addCups(1);
} else {
System.out.println("I have enough resources, making you a coffee!");
money += money_;
}
}
private static boolean checkStatus() {
if (water < 0) {
System.out.println("Sorry, not enough water");
return false;
}
if (milk < 0) {
System.out.println("Sorry, not enough milk");
return false;
}
if (coffeeBeans < 0) {
System.out.println("Sorry, not enough coffee beans");
return false;
}
if (cups < 0) {
System.out.println("Sorry, not enough disposable cups");
return false;
}
return true;
}
private static void addWater(int water_) {
water += water_;
}
private static void addMilk(int milk_) {
milk += milk_;
}
private static void addCoffeeBeans(int coffeeBeans_) {
coffeeBeans += coffeeBeans_;
}
private static void addCups(int cups_) {
cups += cups_;
}
private static void giveMoney() {
money -= money;
}
}