-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
168 lines (147 loc) · 4.72 KB
/
main.js
File metadata and controls
168 lines (147 loc) · 4.72 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
const readline = require('readline');
// Create a readline interface for user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Promisified input function
function input(promptText) {
return new Promise((resolve) => {
rl.question(promptText, (answer) => {
resolve(answer);
});
});
}
let coffeeMachine = [400, 540, 120, 9, 550];
async function main() {
let action;
do {
action = await input("Write action (buy, fill, take, remaining, exit): ");
await go(action, coffeeMachine);
} while (action !== "exit");
rl.close(); // Close the readline interface when done
}
async function go(action, coffeeMachine) {
switch (action) {
case "buy":
coffeeMachine = await buy(coffeeMachine);
break;
case "fill":
coffeeMachine = await fill(coffeeMachine);
break;
case "take":
coffeeMachine = take(coffeeMachine);
break;
case "remaining":
remaining(coffeeMachine);
break;
case "exit":
return;
default:
console.log("This wasn't an option");
break;
}
}
async function fill(coffeeMachine) {
let addAmount;
addAmount = await input("Write how many ml of water you want to add: ");
coffeeMachine[0] += parseInt(addAmount);
addAmount = await input("Write how many ml of milk you want to add: ");
coffeeMachine[1] += parseInt(addAmount);
addAmount = await input("Write how many grams of coffee beans you want to add: ");
coffeeMachine[2] += parseInt(addAmount);
addAmount = await input("Write how many disposable coffee cups you want to add: ");
coffeeMachine[3] += parseInt(addAmount);
return coffeeMachine;
}
function take(coffeeMachine) {
console.log(`I gave you $${coffeeMachine[4]}`);
coffeeMachine[4] = 0;
return coffeeMachine;
}
function remaining(coffeeMachine) {
console.log(`The coffee machine has:
${coffeeMachine[0]} ml of water
${coffeeMachine[1]} ml of milk
${coffeeMachine[2]} g of coffee beans
${coffeeMachine[3]} disposable cups
$${coffeeMachine[4]} of money`);
}
async function buy(coffeeMachine) {
let coffeeType = await input(
"What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu: "
);
switch (coffeeType) {
case "1":
coffeeMachine = espressoAmount(coffeeMachine);
break;
case "2":
coffeeMachine = latteAmount(coffeeMachine);
break;
case "3":
coffeeMachine = cappuccinoAmount(coffeeMachine);
break;
case "back":
return coffeeMachine;
default:
console.log("This wasn't an option");
break;
}
return coffeeMachine;
}
function espressoAmount(coffeeMachine) {
if (coffeeMachine[0] < 250) {
console.log("Sorry, not enough water!");
} else if (coffeeMachine[2] < 16) {
console.log("Sorry, not enough coffee beans!");
} else if (coffeeMachine[3] < 1) {
console.log("Sorry, not enough cups!");
} else {
console.log("I have enough resources, making you a coffee!");
coffeeMachine[0] -= 250;
coffeeMachine[2] -= 16;
coffeeMachine[4] += 4;
coffeeMachine[3] -= 1;
}
return coffeeMachine;
}
function latteAmount(coffeeMachine) {
if (coffeeMachine[0] < 350) {
console.log("Sorry, not enough water!");
} else if (coffeeMachine[1] < 75) {
console.log("Sorry, not enough milk!");
} else if (coffeeMachine[2] < 20) {
console.log("Sorry, not enough coffee beans!");
} else if (coffeeMachine[3] < 1) {
console.log("Sorry, not enough cups!");
} else {
console.log("I have enough resources, making you a coffee!");
coffeeMachine[0] -= 350;
coffeeMachine[1] -= 75;
coffeeMachine[2] -= 20;
coffeeMachine[4] += 7;
coffeeMachine[3] -= 1;
}
return coffeeMachine;
}
function cappuccinoAmount(coffeeMachine) {
if (coffeeMachine[0] < 200) {
console.log("Sorry, not enough water!");
} else if (coffeeMachine[1] < 100) {
console.log("Sorry, not enough milk!");
} else if (coffeeMachine[2] < 12) {
console.log("Sorry, not enough coffee beans!");
} else if (coffeeMachine[3] < 1) {
console.log("Sorry, not enough cups!");
} else {
console.log("I have enough resources, making you a coffee!");
coffeeMachine[0] -= 200;
coffeeMachine[1] -= 100;
coffeeMachine[2] -= 12;
coffeeMachine[4] += 6;
coffeeMachine[3] -= 1;
}
return coffeeMachine;
}
// Start the main function
main();