-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoffeeMachine.kt
87 lines (76 loc) · 2.52 KB
/
CoffeeMachine.kt
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
package coffeemachine
import utils.Utils.Message.*
import utils.Utils.Coffee
import utils.Utils.CoffeeType
import utils.Utils.Action
import utils.Utils.Ingredient
class CoffeeMachine {
private val availableIngredients = Ingredient(400, 540, 120, 9)
private var moneyLeft = 550 // In USD
private val availableCoffees = listOf(
Coffee(CoffeeType.ESPRESSO, 4, Ingredient(250, 0, 16)),
Coffee(CoffeeType.LATTE, 7, Ingredient(350, 75, 20)),
Coffee(CoffeeType.CAPPUCCINO, 6, Ingredient(200, 100, 12))
)
fun startMachine() {
printState()
getAction()
printState()
}
private fun printState() {
println("The coffee machine has:")
availableIngredients.apply {
println("$water ml of water")
println("$milk ml of milk")
println("$coffeeBeans g of coffee beans")
println("$cups disposable cups")
println("$$moneyLeft of money")
println()
}
}
private fun getAction() {
println(WRITE_ACTION_MESSAGE.message)
val input = readln()
when {
input.equals(Action.BUY.name, true) -> buyCoffee()
input.equals(Action.FILL.name, true) -> fillMachine()
input.equals(Action.TAKE.name, true) -> depositMoney()
}
}
private fun buyCoffee() {
println(MENU.message)
val input = readln().toInt()
val chosenCoffee = availableCoffees[input - 1]
availableIngredients.apply {
coffeeBeans -= chosenCoffee.requiredIngredients.coffeeBeans
milk -= chosenCoffee.requiredIngredients.milk
water -= chosenCoffee.requiredIngredients.water
cups -= chosenCoffee.requiredIngredients.cups
moneyLeft += chosenCoffee.cost
}
}
private fun fillMachine() {
println(FILL_MESSAGE_WATER.message)
val addedWater = readln().toInt()
println(FILL_MESSAGE_MILK.message)
val addedMilk = readln().toInt()
println(FILL_MESSAGE_COFFEE_BEANS.message)
val addedBeans = readln().toInt()
println(FILL_MESSAGE_CUPS.message)
val addedCups = readln().toInt()
availableIngredients.apply {
water += addedWater
milk += addedMilk
coffeeBeans += addedBeans
cups += addedCups
}
println()
}
private fun depositMoney() {
println("I gave you $$moneyLeft\n")
moneyLeft = 0
}
}
fun main() {
CoffeeMachine().startMachine()
}