-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGumballMachine.kt
57 lines (43 loc) · 1.1 KB
/
GumballMachine.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
package state.gumballmachine.context
import state.gumballmachine.state.*
import kotlin.math.max
/**
* Created by devansh on 26/09/20.
*/
class GumballMachine(numberOfGumballs: Int) {
val soldOutState: State
val noQuarterState: State
val hasQuarterState: State
val soldState: State
val winnerState: State
var state: State
var count: Int = numberOfGumballs
private set
init {
soldOutState = SoldOutState()
noQuarterState = NoQuarterState(this)
hasQuarterState = HasQuarterState(this)
soldState = SoldState(this)
winnerState = WinnerState(this)
count = numberOfGumballs
state = if (numberOfGumballs > 0) {
noQuarterState
} else {
soldOutState
}
}
fun insertQuarter() {
state.insertQuarter()
}
fun ejectQuarter() {
state.ejectQuarter()
}
fun turnCrank() {
state.turnCrank()
state.dispense()
}
fun releaseBall() {
println("A gumball comes rolling out the slot...")
count = max(count - 1, 0)
}
}