-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction.py
128 lines (102 loc) · 3.99 KB
/
Function.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import csv
import uuid
import datetime
class Food:
foodList = []
optionList = []
foodSelected = []
queueFood = []
queueCount = 0
def __init__(self):
# list of food you want to add
with open("data/food.csv", 'r') as csvFile:
reader = csv.DictReader(csvFile)
for row in reader:
# print(dict(row))
self.foodList.append(dict(row))
# list of option you want to add
with open("data/option.csv", 'r') as csvFile:
reader = csv.DictReader(csvFile)
for row in reader:
# print(dict(row))
self.optionList.append(dict(row))
def showFoodList(self):
return ['{0} {1} Php'.format(i['Name'], i['Price']) for i in self.foodList]
def showOptionList(self):
return ['{0} {1} Php'.format(i['Name'], i['Price']) for i in self.optionList]
def addFood(self, food, option={'Name': '', 'Price': 0}):
foodOptionSelected = {
'foodName': food['Name'],
'foodOption': option['Name'],
'sumPrice': food['Price'] + option['Price']
}
self.foodSelected.append(foodOptionSelected)
def showFoodSelected(self):
return ['{0} {1} {2} Php'.format(
i['foodName'], i['foodOption'], i['sumPrice']) for i in self.foodSelected]
def cancelFoodSelected(self, food):
self.foodSelected.remove(food)
def addQueueFood(self, table, foodSelectedList):
"""
add a queue and table number on the selected food
model data
{
'queue':1,
'table':1,
'foodSelectedList':[
{'foodName': 'Food B', 'foodOption': '', 'total': 35},
{'foodName': 'Food A', 'foodOption': '', 'total': 20}]
}
"""
self.queueCount += 1
value = {'queue': self.queueCount,
'table': table, 'foodSelectedList': foodSelectedList}
self.queueFood.append(value)
self.foodSelected = [] # reset food selected list
def clearQueueFood(self, queue):
# remove queue with selected
self.queueFood.remove(queue)
def getAllQueue(self):
return self.queueFood
def getDetailQueue(self, queueNumber):
for i in self.queueFood:
if (i['queue'] == queueNumber): # find detail
return i
def saveToDb(self, queue):
"""
model queue
{
'queue':1,
'foodSelectedList':[
{'name': 'Food B', 'price': 35},
{'name': 'Food A', 'price': 20}]
}
"""
total = 0 # total price
for food in queue['foodSelectedList']:
total += food['total']
genFoodId = str(uuid.uuid4()) # generate foodId for two files csv
with open('data/db.csv', mode='w') as csvFile:
fieldnames = ['Date', 'FoodId','FoodName', 'Total']
writer = csv.DictWriter(csvFile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'Date': str(datetime.datetime.now()),
'FoodId': genFoodId,
'Total': total
})
with open('data/foodDb.csv', mode='w') as csvFile:
fieldnames = ['FoodId', 'FoodName']
writer = csv.DictWriter(csvFile, fieldnames=fieldnames)
writer.writeheader()
for food in queue['foodSelectedList']:
writer.writerow({'FoodId': genFoodId, # generate unique id
'FoodName': food['foodName']
})
self.queueFood.remove(queue) # remove this queue from queue food
if __name__ == "__main__":
x = Food()
x.addFood({'Name': 'Food A', 'Price': 10}, {
'Name': 'Option B', 'Price': 10})
x.addFood({'Name': 'Food B', 'Price': 20})
x.addQueueFood(5, x.foodSelected)
x.saveToDb(x.queueFood[0])