-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVendingMachine.py
More file actions
82 lines (71 loc) · 2.03 KB
/
VendingMachine.py
File metadata and controls
82 lines (71 loc) · 2.03 KB
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
# Vending machine simulation
from time import sleep
from random import random, choice
class Machine:
priceList = {'A':10, 'B':15, 'C':20}
def __init__(self, items=None, deposit=50):
self.income = 0
self.items = items or {'A':5, 'B':5, 'C':5}
self.deposit = deposit
def insertCoin(self, n):
print('insert coin %d' % n)
self.income += n
return 'loadCoin'
def _refund(self):
if self.income:
print('return coin %d' % self.income)
self.income = 0
# send back coin TODO: API call
def cancel(self):
print('cancel')
self._refund()
return 'ready'
def _buyItem(self, name):
if self.items[name] > 0 and self.income >= self.priceList[name]:
print('send a goods: %s' % name)
self.items[name] -= 1
self.income -= self.priceList[name]
# TODO: Need to confirm if there is enough balance
self._refund()
return 0 # success
elif self.income < self.priceList[name]:
print('Insufficient amount, needs %d coin(s)' % (self.priceList[name]-self.income))
return 1 # Insufficient amount
elif self.items[name] <= 0:
print('item %s is sold out' % name)
return 2 # sold out
else:
print('something wrong, back to state<insertCoin>')
return -1
def buyItem(self, name):
respond = self._buyItem(name)
if respond == 0:
return 'ready'
else:
return 'loadCoin'
def randwalk(state):
cmdTable = {}
cmdTable['ready'] = ['insertCoin', 'cancel']
cmdTable['loadCoin'] = ['insertCoin', 'cancel', 'buyItem']
coinList = [1, 5, 10, 50]
itemList = ['A', 'B', 'C'] # other way: get machine.items at runtime
cmd = choice(cmdTable[state])
if cmd == 'insertCoin':
return cmd, choice(coinList)
elif cmd == 'buyItem':
return cmd, choice(itemList)
else:
return cmd
m = Machine()
s = 'ready'
cmd = ''
while True:
print('state=%s' % s)
cmd = randwalk(s)
if type(cmd) is tuple:
print('cmd=%s' % cmd[0])
s = getattr(m, cmd[0])(cmd[1]) # call m.cmd(arg) & get next state
else:
print('cmd=%s' % cmd)
s = getattr(m, cmd)()
sleep(1)