-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpickle_ver.py
355 lines (329 loc) · 11.2 KB
/
pickle_ver.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# MADE BY: Tanmay Deshpande
# GRADE: XII
# SECTION: E
# DATE OF COMPLETION: 13-05-2020
# SALES EMULATION PROJECT
import pickle
import sys
from datetime import date
import subprocess as sp
import pkg_resources
installed = {pkg.key for pkg in pkg_resources.working_set}
required = {'questionary', 'tabulate'}
missing = required - installed
if missing:
python = sys.executable
sp.check_call([python, '-m', 'pip', 'install', '--upgrade',
*missing], stdout=sp.DEVNULL)
from tabulate import tabulate
import questionary as qr
else:
from tabulate import tabulate
import questionary as qr
print(sys.executable)
print(sys.version)
today = date.today()
header_standard = ['ITEM_CODE', 'DESC',
'PRICE', 'DISCOUNT', 'QTY', 'REORDER_QTY']
header_purchase = ['ITEM_CODE', 'DESC', 'PRICE',
'DISCOUNT', 'ACTUAL PRICE', 'QTY', 'TOTAL_PRICE']
def createFiles():
f = open('Items.dat', 'wb')
f.close()
f = open('Sales.dat', 'wb')
f.close()
def newItem():
cont = True
while cont:
print('-'*45)
codes = {} # Dict to hold item code and index of same
data = []
with open('Items.dat', 'rb') as fileObject:
while True:
try:
data = pickle.load(fileObject)
if len(data) == 0:
pass
else:
for row in data:
index = data.index(row)
ncode = row[0]
c = {ncode: index}
codes.update(c)
except:
break
while True:
code = str(input('Enter the item code: ')) # 1
if code in codes.keys():
print('Code already taken!')
print('Try Again!')
elif code.isalnum() is False or len(code) != 4:
print('Try Again!')
else:
break
descInput = str(
input("Enter a short description of the item: ")) # 2
desc = descInput[:20]
while True:
priceInput = float(input("Enter the price of the item: ")) # 3
if priceInput < 0:
print('Try again!')
else:
break
while True:
discInput = float(input("Enter the discount(in %): ")) # 4
if discInput < 0:
print('Try Again!')
else:
break
while True:
qtyInput = int(input("Enter the current quantity: ")) # 5
if qtyInput < 0:
print('Try Again!')
else:
break
while True:
reQtyInput = int(input('Enter the reorder quantity: ')) # 6
if reQtyInput < 0:
print('Try Again!')
else:
break
ins = [code, descInput, priceInput,
discInput, qtyInput, reQtyInput]
data.append(ins)
with open('Items.dat', 'wb') as fileObject:
pickle.dump(data, fileObject)
print('Entry successfully made!')
cont = qr.confirm('Do you wish to continue?').ask()
def modifyItem():
cont = True
while cont:
print("-"*45)
data = []
codes = {}
with open('Items.dat', 'rb') as fileObject:
while True:
try:
data = pickle.load(fileObject)
if len(data) == 0:
pass
else:
for row in data:
index = data.index(row)
ncode = row[0]
c = {ncode: index}
codes.update(c)
except:
break
if len(data) == 0:
print('Data Set is currently empty')
print('Add some data first!')
return
else:
print('Current data set:\n')
print(tabulate(data, header_standard, 'fancy_grid'), '\n')
while True:
modCode = str(
input("Enter the code for the item to be modified: "))
if modCode not in codes.keys():
print('Invalid Code!\n')
else:
modIndex = codes[modCode] # Gets index of row to be modified
rec = data[modIndex] # Gets row to be modified
print('Entry that is going to be modified:\n')
print(rec)
newData = rec
data.pop(modIndex)
break
newDesc = str(input("Enter the NEW Desc(Enter for skip): "))
if newDesc == "":
pass
else:
newData[1] = newDesc
while True:
newPrice = str(input('Enter the NEW Price(Enter for skip): '))
if newPrice == '':
break
elif float(newPrice) < 0:
print('Price cannot be negative!')
else:
newPrice = float(newPrice)
newData[2] = newPrice
break
while True:
newDisc = str(input('Enter the NEW Discount(Enter for skip): '))
if newDisc == '':
break
elif float(newDisc) < 0:
print('Discount cannot be negative!')
else:
newDisc = float(newDisc)
newData[3] = newDisc
break
while True:
newQty = str(input('Enter the NEW Quantity(Enter for skip): '))
if newQty == '':
break
elif int(newQty) < 0:
print('Quantity cannot be negative!')
else:
newQty = int(newQty)
newData[4] = newQty
break
while True:
newReQty = str(
input('Enter the NEW Reorder Quantity(Enter for skip): '))
if newReQty == '':
break
elif int(newReQty) < 0:
print('Reorder Quantity cannot be negative!')
else:
newReQty = float(newReQty)
newData[5] = newReQty
break
data.insert(modIndex, newData)
with open('Items.dat', 'wb') as fileObject:
pickle.dump(data, fileObject)
print('Entry modified succesfully!')
cont = qr.confirm('Do you wish to continue?').ask()
def removeItem():
cont = True
while cont:
data = []
codes = {}
with open('Items.dat', 'rb') as fileObject:
while True:
try:
data = pickle.load(fileObject)
if len(data) == 0:
pass
else:
for row in data:
index = data.index(row)
ncode = row[0]
c = {ncode: index}
codes.update(c)
except:
break
if len(data) == 0:
print('Data Set is currently empty')
print('Add some data first!')
return
else:
print('-'*45)
print('Current records:\n')
print(tabulate(data, header_standard, 'fancy_grid'), '\n')
while True:
remCode = str(
input('Enter the code for which you want to delete the record: '))
if remCode not in codes.keys():
print('Invalid Code!')
else:
break
remIndex = codes[remCode]
data.pop(remIndex)
newData = data
with open('Items.dat', 'wb') as fileObject:
pickle.dump(newData, fileObject)
print('Modified records:')
print(tabulate(newData, header_standard, 'fancy_grid'), '\n')
cont = qr.confirm('Do you wish to continue?').ask()
def showAll():
data = []
with open('Items.dat', 'rb') as fileObject:
while True:
try:
data = pickle.load(fileObject)
except:
break
if len(data) == 0:
print('Data Set is currently empty')
print('Add some data first!')
return
print('-'*45)
print(tabulate(data, header_standard, 'fancy_grid'), '\n')
def purchaseItem():
data = []
codes = {}
with open("Items.dat", "rb") as fileObject:
while True:
try:
data = pickle.load(fileObject)
if len(data) == 0:
pass
else:
for row in data:
index = data.index(row)
ncode = row[0]
codes.update({ncode: index})
except:
break
if len(data) == 0:
print('Data Set is currently empty')
print('Add some data first!')
return
cont = True
while cont:
print(f"Current data:\n")
print(tabulate(data, header_standard, 'fancy_grid'), '\n')
while True:
purchaseCode = str(
input("Enter the item code that you want to purchase: "))
if purchaseCode not in codes.keys():
print('Invalid code!')
else:
index = codes[purchaseCode]
rec = data[index]
break
discount = float(rec[3])
acDisc = (discount/100) # Converting discount from float to percent
# Price - Price*Discount
actualPrice = (float(rec[2]) - (float(rec[2])*acDisc))
while True:
qty = int(input('Enter the quantity(INT): '))
if qty < 0:
print('Value cannot be negative!')
elif qty >= 0:
break
totalPrice = actualPrice*qty
newList = [purchaseCode, rec[1], rec[2],
rec[3], actualPrice, qty, totalPrice]
with open('Sales.dat', 'ab') as fileObject:
pickle.dump(newList, fileObject)
contentList = []
contentList.append(newList)
print(today)
print('\n')
print(tabulate(contentList, header_purchase, 'fancy_grid'), '\n')
print("- Thank you for shopping with us! ")
print("- No returns,no refunds ")
print("- If cashier doesn't provide the bill, then this purchase is on the house\n")
cont = qr.confirm('Do you wish to continue?').ask()
def main():
createFiles()
options = ['1.Add a new item',
'2.Modify an existing item',
'3.Remove an exisiting item',
'4.Show all items',
'5.Purchase an item and generate a bill',
'6.EXIT']
while True:
print('-'*50)
question = qr.select(
'Select from one of the options: ',
choices=options)
response = question.ask()
if response == options[0]:
newItem()
elif response == options[1]:
modifyItem()
elif response == options[2]:
removeItem()
elif response == options[3]:
showAll()
elif response == options[4]:
purchaseItem()
elif response == options[5]:
exit()
if __name__ == "__main__":
main()