-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
304 lines (245 loc) · 6.98 KB
/
main.go
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"fmt"
"os"
)
// Regular Coffee
const (
WaterNeededPerCup = 200
MilkNeededPerCup = 50
CoffeeNeededPerCup = 15
)
// Espresso
const (
WaterPerEspresso = 250
MilkPerEspresso = 0
CoffeePerEspresso = 16
CostPerEspresso = 4
)
// Latte
const (
WaterPerLatte = 350
MilkPerLatte = 75
CoffeePerLatte = 20
CostPerLatte = 7
)
// Cappuccino
const (
WaterPerCappuccino = 200
MilkPerCappuccino = 100
CoffeePerCappuccino = 12
CostPerCappuccino = 6
)
func MinIntSlice(array []int) (minNum int) {
if len(array) > 0 {
minNum = array[0]
}
for i, e := range array {
if i == 0 || e < minNum {
minNum = e
}
}
return
}
func processCalculation(getWater, getMilk, getCoffee, getCups int) (int, int) {
calcWaterRatio := getWater / WaterNeededPerCup
calcMilkRatio := getMilk / MilkNeededPerCup
calcCoffeeRatio := getCoffee / CoffeeNeededPerCup
ratioSlice := []int{calcWaterRatio, calcMilkRatio, calcCoffeeRatio}
minCups := MinIntSlice(ratioSlice)
maxCups := 0
if minCups > getCups {
maxCups = minCups - getCups
}
return minCups, maxCups
}
func processBuyFillTake(
storedMoney,
storedWater,
storedMilk,
storedCoffee,
storedCups int,
) {
var remainingMoney, remainingWater, remainingMilk, remainingCoffee, remainingCups int
remainingMoney += storedMoney
remainingWater += storedWater
remainingMilk += storedMilk
remainingCoffee += storedCoffee
remainingCups += storedCups
for {
userAction := getUserAction() // buy | fill | take
switch userAction {
case "buy":
remainingMoney, remainingWater, remainingMilk, remainingCoffee, remainingCups = processBuy(
remainingMoney, remainingWater, remainingMilk, remainingCoffee,
remainingCups,
)
continue
case "fill":
remainingWater, remainingMilk, remainingCoffee, remainingCups = processFill(
remainingWater,
remainingMilk,
remainingCoffee,
remainingCups,
)
continue
case "take":
remainingMoney = processTake(
storedMoney,
)
continue
case "remaining":
processRemaining(
remainingMoney, remainingWater, remainingMilk, remainingCoffee,
remainingCups,
)
continue
case "exit":
os.Exit(0)
default:
}
}
}
func processBuy(storedMoney, storedWater, storedMilk, storedCoffee, storedCups int) (
int, int, int, int, int,
) {
var wantToBuy int
var remainingMoney, remainingWater, remainingMilk, remainingCoffee, remainingCups int
fmt.Println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino: ")
_, _ = fmt.Scan(&wantToBuy)
switch wantToBuy {
case 1: // Espresso
if storedWater >= WaterPerEspresso && storedCoffee >= CoffeePerEspresso {
remainingMoney = storedMoney + CostPerEspresso
remainingWater = storedWater - WaterPerEspresso
remainingMilk = storedMilk - MilkPerEspresso
remainingCoffee = storedCoffee - CoffeePerEspresso
remainingCups = storedCups - 1
fmt.Println("I have enough resources, making you a coffee!")
} else {
remainingMoney = storedMoney
remainingWater = storedWater
remainingMilk = storedMilk
remainingCoffee = storedCoffee
remainingCups = storedCups
if storedWater < WaterPerEspresso {
fmt.Println("Sorry, not enough water!")
} else if storedCoffee < CoffeePerEspresso {
fmt.Println("Sorry, not enough coffee!")
}
}
case 2: // Latte
if storedWater >= WaterPerLatte && storedMilk >= MilkPerLatte && storedCoffee >= CoffeePerLatte {
remainingMoney = storedMoney + CostPerLatte
remainingWater = storedWater - WaterPerLatte
remainingMilk = storedMilk - MilkPerLatte
remainingCoffee = storedCoffee - CoffeePerLatte
remainingCups = storedCups - 1
fmt.Println("I have enough resources, making you a coffee!")
} else {
remainingMoney = storedMoney
remainingWater = storedWater
remainingMilk = storedMilk
remainingCoffee = storedCoffee
remainingCups = storedCups
if storedWater < WaterPerLatte {
fmt.Println("Sorry, not enough water!")
} else if storedMilk < MilkPerLatte {
fmt.Println("Sorry, not enough milk!")
} else if storedCoffee < CoffeePerLatte {
fmt.Println("Sorry, not enough coffee!")
}
}
case 3: // Cappuccino
if storedWater >= WaterPerCappuccino && storedMilk >= MilkPerCappuccino && storedCoffee >= CoffeePerCappuccino {
remainingMoney = storedMoney + CostPerCappuccino
remainingWater = storedWater - WaterPerCappuccino
remainingMilk = storedMilk - MilkPerCappuccino
remainingCoffee = storedCoffee - CoffeePerCappuccino
remainingCups = storedCups - 1
fmt.Println("I have enough resources, making you a coffee!")
} else {
remainingMoney = storedMoney
remainingWater = storedWater
remainingMilk = storedMilk
remainingCoffee = storedCoffee
remainingCups = storedCups
if storedWater < WaterPerCappuccino {
fmt.Println("Sorry, not enough water!")
} else if storedMilk < MilkPerCappuccino {
fmt.Println("Sorry, not enough milk!")
} else if storedCoffee < CoffeePerCappuccino {
fmt.Println("Sorry, not enough coffee!")
}
}
default:
remainingMoney = storedMoney
remainingWater = storedWater
remainingMilk = storedMilk
remainingCoffee = storedCoffee
remainingCups = storedCups
}
return remainingMoney, remainingWater, remainingMilk, remainingCoffee, remainingCups
}
func processFill(
remainingWater,
remainingMilk,
remainingCoffee,
remainingCups int,
) (
int, int, int, int,
) {
var addedWater, addedMilk, addedCoffee, addedCups int
fmt.Println("Write how many ml of water you want to add:")
_, _ = fmt.Scan(&addedWater)
fmt.Println("Write how many ml of milk you want to add:")
_, _ = fmt.Scan(&addedMilk)
fmt.Println("Write how many grams of coffee beans you want to add:")
_, _ = fmt.Scan(&addedCoffee)
fmt.Println("Write how many disposable cups you want to add:")
_, _ = fmt.Scan(&addedCups)
remainingWater = remainingWater + addedWater
remainingMilk = remainingMilk + addedMilk
remainingCoffee = remainingCoffee + addedCoffee
remainingCups = remainingCups + addedCups
return remainingWater, remainingMilk, remainingCoffee, remainingCups
}
func processTake(storedMoney int) int {
remainingMoney := storedMoney - storedMoney
fmt.Printf("I gave you $%d\n", remainingMoney)
return remainingMoney
}
func processRemaining(
remainingMoney, remainingWater, remainingMilk, remainingCoffee,
remainingCups int,
) {
fmt.Println("")
fmt.Println("")
fmt.Println("The coffee machine has:")
fmt.Printf("%d ml of water\n", remainingWater)
fmt.Printf("%d ml of milk\n", remainingMilk)
fmt.Printf("%d g of coffee beans\n", remainingCoffee)
fmt.Printf("%d disposable cups\n", remainingCups)
fmt.Printf("$%d of money\n", remainingMoney)
}
func getUserAction() string {
var userAction string
fmt.Println("Write action (buy, fill, take, remaining, exit):")
_, _ = fmt.Scan(&userAction)
return userAction
}
func main() {
// Declaring Variables
storedMoney := 550
storedWater := 400
storedMilk := 540
storedCoffee := 120
storedCups := 9
processBuyFillTake(
storedMoney,
storedWater,
storedMilk,
storedCoffee,
storedCups,
)
}