-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathvending_machine.go
56 lines (46 loc) · 1.28 KB
/
vending_machine.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
package vending_machine
type VendingMachine struct {
inventory *Inventory
idleState VendingMachineState
readyState VendingMachineState
dispenseState VendingMachineState
returnChangeState VendingMachineState
currentState VendingMachineState
selectedProduct *Product
totalPayment float64
}
func NewVendingMachine() *VendingMachine {
vm := &VendingMachine{
inventory: NewInventory(),
}
vm.idleState = &IdleState{vm}
vm.readyState = &ReadyState{vm}
vm.dispenseState = &DispenseState{vm}
vm.returnChangeState = &ReturnChangeState{vm}
vm.currentState = vm.idleState
return vm
}
func (vm *VendingMachine) SelectProduct(product *Product) {
vm.currentState.SelectProduct(product)
}
func (vm *VendingMachine) InsertCoin(coin Coin) {
vm.currentState.InsertCoin(coin)
}
func (vm *VendingMachine) InsertNote(note Note) {
vm.currentState.InsertNote(note)
}
func (vm *VendingMachine) DispenseProduct() {
vm.currentState.DispenseProduct()
}
func (vm *VendingMachine) ReturnChange() {
vm.currentState.ReturnChange()
}
func (vm *VendingMachine) SetState(state VendingMachineState) {
vm.currentState = state
}
func (vm *VendingMachine) ResetPayment() {
vm.totalPayment = 0
}
func (vm *VendingMachine) ResetSelectedProduct() {
vm.selectedProduct = nil
}