-
Notifications
You must be signed in to change notification settings - Fork 14
/
State.kt
74 lines (66 loc) · 2.14 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package org.vld.sdp.behavioral
/**
* Vending Machine State interface
*/
interface State {
fun handleRequest(): List<String>
}
/**
* Vending Machine implementation
*/
class VendingMachine {
// set the initial Vending Machine state to the Show Products step
var state: State = ShowProducts(this, listOf())
/**
* Handles the request as per the current state and set the next Vending Machine state
*/
fun proceed(): List<String> = state.handleRequest()
}
/**
* Show Products step implementation
*/
class ShowProducts(private val vendingMachine: VendingMachine, private val request: List<String>) : State {
override fun handleRequest(): List<String> {
// handle the request
val result = request + "Show Products"
// set the next Vending Machine step
vendingMachine.state = SelectProduct(vendingMachine, result)
return result
}
}
/**
* Select Product step implementation
*/
class SelectProduct(private val vendingMachine: VendingMachine, private val request: List<String>) : State {
override fun handleRequest(): List<String> {
// handle the request
val result = request + "Select Product"
// set the next Vending Machine step
vendingMachine.state = DepositMoney(vendingMachine, result)
return result
}
}
/**
* Deposit Money step implementation
*/
class DepositMoney(private val vendingMachine: VendingMachine, private val request: List<String>) : State {
override fun handleRequest(): List<String> {
// handle the request
val result = request + "Deposit Money"
// set the next Vending Machine step
vendingMachine.state = DeliverProduct(vendingMachine, result)
return result
}
}
/**
* Deliver Product step implementation
*/
class DeliverProduct(private val vendingMachine: VendingMachine, private val request: List<String>) : State {
override fun handleRequest(): List<String> {
// handle the request
val result = request + "Deliver Product"
// set the next Vending Machine step
vendingMachine.state = ShowProducts(vendingMachine, result)
return result
}
}