-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
237 lines (203 loc) · 7.73 KB
/
functions.py
File metadata and controls
237 lines (203 loc) · 7.73 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# This is the functionality that main is using
# The program will terminate if an error comes up while trying to search for an object within the db
# usually, for reasons of a bad connection. If it is a bad connection the db client will time out after 1000 milisecs.
import coffee
import connectDB
import re
from os import path
import json
import pprint
#### choice 1 ####
def addCoffeeItem():
print("Adding a coffee item")
while True:
print("Please enter the coffee ID")
itemId = input(">>>")
# checking if integer
if not re.match("^[0-9]*$",itemId):
print("Sorry coffee id must be a number")
# check if item id already exist
elif searchCoffee(itemId) != "Sorry, but that item number isn't in our records":
print("Sorry, but that coffee ID already exist")
else:
break
print("Please enter the name of the coffee")
name= input(">>>")
# Don't allow ',' in name
if "," in name:
name = re.sub(',',' ',name)
while True:
print("Please enter the price per pound")
pricePerLB = input(">>>")
# checking if integer
if not re.match("^[0-9]*$", pricePerLB):
print("Sorry, the price per pound must be a number")
else:
break
while True:
print("Please enter the roasting type ex: L, M, D")
roasting = input(">>>")
# Only except L, M, D
if roasting != "L" and roasting != "M" and roasting != "D":
print("Sorry, please choose L, M, or D ")
else:
break
while True:
print("Please enter the number of units avalible")
quantity = input(">>>")
# Check if integer
if not re.match("^[0-9]*$", quantity):
print("Sorry, the quantity must be a number")
else:
break
# This is my class object
newCoffee = coffee.Coffee(itemId,name, pricePerLB, roasting, quantity)
print(newCoffee)
# Json object to insert into db
coffeeObject = {
"itemId" : int(itemId),
"name" : str(name),
"pricePerLB" : int(pricePerLB),
"roasting" : str(roasting),
"quantity" : int(quantity)
}
try:
connectDB.collection.insert_one(coffeeObject)
except BaseException as e:
print(e)
#### choice 2 ####
def searchCoffee(id=''):
results = ''
if id =='':
while True:
print("Enter the coffee's item number")
# I made this global so that I can search the itemId before enacting any CRUD operation
global coffeeId
coffeeId = input(">>>")
if not re.match("^[0-9]*$", coffeeId):
print("Sorry, please enter a number")
else:
break
coffeeId = int(coffeeId)
try:
results = connectDB.collection.find_one({"itemId" : coffeeId})
results = str(results)
except BaseException:
print("Sorry, something went wrong")
print("Terminating program...")
quit()
else:
id = int(id)
try:
results = connectDB.collection.find_one({"itemId" : id})
results = str(results)
except BaseException:
print("Sorry, something went wrong")
print("Terminating program...")
quit()
if results == "None":
return "Sorry, but that item number isn't in our records"
else:
return results
#### choice 3 ####
def deleteCoffee():
results = searchCoffee()
if results != "Sorry, but that item number isn't in our records":
pprint.pprint(results)
print("Do you want to delete this coffee item?\n--------------------------------------\ny) yes\nn) no")
choice = input(">>>")
# Verify that they want to delete item.
# This will exit the deleting process if any value other than y/Y is selected
if choice == 'y' or choice == 'Y':
connectDB.collection.delete_one({"itemId" : coffeeId})
print("Item has deleted")
else:
print("Deleting process was canceled...")
else:
pprint.pprint(results)
#### choice 4 ####
def updateCoffee():
results = searchCoffee()
if results != "Sorry, but that item number isn't in our records":
pprint.pprint(results)
print("Do you want to update this coffee item?\n---------------------------------------\ny) yes\nn) no")
choice = input(">>>")
# Verify that they want to update item.
# This will exit the upgrading process if any value other than y/Y is selected
if choice == 'y' or choice =='Y':
print("Select which option you would like to update:\n--------------------------------------------\n1) price per pound\n2) quantity")
field = input(">>>")
if field == "1":
while True:
print("Please enter the new price per pound")
newPrice = input(">>>")
if not re.match("^[0-9]*$", newPrice):
print("Please enter a number")
else:
connectDB.collection.update_one({"itemId": coffeeId},{"$set":{"pricePerLB": int(newPrice)}})
print("Price per pound has been updated")
pprint.pprint(searchCoffee(coffeeId))
break
elif field == "2":
while True:
print("Please enter the new quantity")
newQuantity = input(">>>")
if not re.match("^[0-9]*$", newQuantity):
print("Please enter a number")
else:
connectDB.collection.update_one({"itemId": coffeeId},{"$set":{"quantity": int(newQuantity)}})
print("Quantity has been updated")
pprint.pprint(searchCoffee(coffeeId))
break
else:
print("Invalid option")
else:
print("Updating process was canceled...")
else:
pprint.pprint(results)
#### choice 5 ####
def importJson():
print("Please enter the name of json file. ex: myFile.json")
file = input(">>>")
# Checking if the file exists
if path.exists(file):
file = open(file, 'r')
jsonFile = json.load(file)
for item in jsonFile['coffee']:
# Don't add ones that are already stored in the db
if searchCoffee(item['itemId']) == "Sorry, but that item number isn't in our records":
newCoffee = coffee.Coffee(item['itemId'],item['name'],item['pricePerLB'],item['roasting'], item['quantity'])
print(newCoffee)
print("\n")
try:
connectDB.collection.insert_one(item)
except BaseException as e:
print(e)
print("Importing was successful!")
file.close()
else:
print("Sorry, file not found")
#### choice 6 ####
def exportJson():
print("Exporting file...")
# Create new file for export data
file = open("coffeeExport.json", 'w')
coffee = connectDB.collection.find()
numItems = len(list(coffee))
coffee=connectDB.collection.find()
file.write('{"coffee":[')
for item in coffee:
temp={
"itemId": item['itemId'],
"name": item['name'],
"pricePerLB" : item['pricePerLB'],
"roasting": item['roasting'],
"quantity": item['quantity']
}
json.dump(temp, file, indent=4)
if numItems > 1:
numItems -= 1
file.write(',')
file.write("]}")
file.close()
print("Export complete")