-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.kt
59 lines (46 loc) · 1.58 KB
/
State.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
package state
interface State {
fun insertCard()
fun removeCard()
fun withdrawCash(amount: Int)
}
interface StateMachine : State {
fun setState(state: State) : StateMachine
}
class NoCardState(private val atm: ATM) : State {
override fun insertCard() {
println("SUCCESS: Card inserted.")
atm.setState(atm.hasCardState)
atm.hasCard = true
}
override fun removeCard() = println("ERROR: No card.")
override fun withdrawCash(amount: Int) = println("ERROR: No card")
}
class HasCardState(private val atm: ATM) : State {
override fun insertCard() = println("ERROR: Card is already inserted.")
override fun removeCard() {
println("SUCCESS: Card removed.")
atm.setState(atm.noCardState)
atm.hasCard = false
}
override fun withdrawCash(amount: Int) {
if (atm.cashAmount >= amount) {
atm.cashAmount -= amount
println("SUCCESS: Cash withdrawn.")
}
else
println("ERROR: Insufficient funds.")
}
}
class ATM(cashAmount: Int) : StateMachine {
val noCardState: NoCardState = NoCardState(this)
val hasCardState: HasCardState = HasCardState(this)
private var state: State = noCardState
var hasCard: Boolean = false
var cashAmount: Int = 0
init { this.cashAmount = cashAmount }
override fun insertCard() = state.insertCard()
override fun removeCard() = state.removeCard()
override fun withdrawCash(amount: Int) = state.withdrawCash(amount)
override fun setState(state: State) : StateMachine = apply { this.state = state }
}