This repository was archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
368 lines (285 loc) · 11.4 KB
/
functions.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
356
357
358
359
360
361
362
363
364
365
366
367
368
from datetime import datetime
NAME_INDEX = 0
COMPANY_INDEX = 1
COLOR_INDEX = 2
QUANTITY_INDEX = 3
PRICE_INDEX = 4
BILLS_PATH = "./bills/"
SHIPPING_PATH = './stocks/'
user_current_details = {}
def welcome():
print("+++++++++++++++++++++++++++++++++++++++++++++++++")
print("\tWelcome to Bike Management System")
print("+++++++++++++++++++++++++++++++++++++++++++++++++")
def list_options():
print("\n")
print("+++++++++++++++++++++++++++++++++++++++++++++++++")
print("1. Order Bikes")
print("2. Add Stock")
print("3. Exit")
print("+++++++++++++++++++++++++++++++++++++++++++++++++")
def add_stock():
display_bikes()
selected_id = 0
while selected_id == 0:
try:
selected_id = int(input("Enter the bike id to add the quantity: "))
except:
print("Please enter valid bike id.")
bike_list = get_bike_list()
selected_bike = bike_list.get(selected_id)
if selected_bike == None:
print("Bike not found! Would you like to add new bike?\n1. Yes \n2. No")
yesno = 0
while yesno < 1 or yesno > 2:
try:
yesno = int(input("Enter the value of option: "))
if yesno > 2:
print("\nPlease enter either 1 or 2!\n")
except:
print("Please enter either 1 or 2!")
if yesno == 1:
add_new_bike()
else:
add_stock()
else:
add_current_stock(selected_id)
def add_current_stock(bike_id):
bike_list = get_bike_list()
bike = bike_list.get(bike_id)
name = bike[NAME_INDEX]
company = bike[COMPANY_INDEX]
color = bike[COLOR_INDEX]
price = bike[PRICE_INDEX]
shipping_name = input("Enter the name of shipping company: ")
shipping_address = input("Enter the address of shipping company: ")
delivery_date = input("Enter the delivery date: ")
bike_quantity = 0
while bike_quantity < 1:
try:
bike_quantity = int(input("Enter the quantity to add: "))
except:
print("\nPlease enter a valid number greater than 0\n")
shipping_cost = 0
while shipping_cost < 0:
try:
shipping_cost = int(input("Enter the shipping cost: "))
except:
print("\nPlease enter valid cost for devliery!")
amount_paid = int(price.replace("$", "")) * bike_quantity
content = f"""Shipping Company: {shipping_name} \nShipping Address: {shipping_address} \nDelivery Date: {delivery_date} \n\nBike Name: {name} \nCompany Name: {company} \nColor: {color} \nQuantity: {bike_quantity} \nPrice: {price} \nAmount paid: ${amount_paid}"""
time_today = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
file_name = SHIPPING_PATH + shipping_name + "_" + time_today + ".txt"
file = open(file_name, "w")
file.write(content)
file.close()
add_bike_stock(bike_id, bike_quantity)
print("\nSuccessfully added the quantity to the stock.")
def add_new_bike():
bike_name = input("Enter the name of bike: ")
company_name = input("Enter the company of bike: ")
bike_color = input("Enter the color of bike: ")
bike_quantity = 0
while bike_quantity < 1:
try:
bike_quantity = int(input("Enter the quantity of the bike: "))
except:
print("\nPlease enter a valid number greater than 0\n")
bike_price = 0
while bike_price < 1:
try:
bike_price = int(input("Enter the price of bike: "))
except:
print("\nPlease enter a valid number greater than 0\n")
print("\nPlease confirm the details of the new bike\n")
print("Bike Name: "+bike_name+"\nCompany Name: "+company_name+"\nColor: "+bike_color+"\nQuantity:"+str(bike_quantity)+"\nPrice: $"+str(bike_price))
print("\nIs is correct?\n1. Yes \n2. No")
yesno = 0
while yesno < 1 or yesno > 2:
try:
yesno = int(input("Enter the value of option: "))
if yesno > 2:
print("\nPlease enter either 1 or 2!\n")
except:
print("\nPlease enter either 1 or 2!\n")
if yesno == 1:
file = open('bike.txt', 'a')
file.write("\n"+bike_name+","+company_name+","+bike_color+","+str(bike_quantity)+",$"+str(bike_price))
file.close()
print("\nSuccessfully added the bike to the stock!")
else:
print("\nPlease enter the details again!\n")
add_new_bike()
def get_user_details():
print("")
name = input("Enter your full name: ")
address = input("Enter your address: ")
email = input("Enter your email: ")
phone = ""
while phone == "":
try:
phone = int(input("Enter your phone number: "))
except:
print("\nPlease enter valid number for phone!\n")
details = {}
details["name"] = name
details["address"] = address
details["phone"] = phone
details["email"] = email
global user_current_details
user_current_details = details
return details
def exit_system():
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print("\tThank you for using Bike Management System")
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
def bike_not_found():
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print("That bike id is not available. Please choose from 1 -", len(get_bike_list()))
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
order_bike()
def purchase_more():
print("\n")
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print("\tThank you for purchasing")
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
yesno = 0
print("Would you like to purchase more?\n1. Yes \n2. No ")
while yesno < 1 or yesno > 2:
try:
yesno = int(input("Enter the value of option: "))
if yesno > 2:
print("\nPlease enter either 1 or 2!\n")
except:
print("\nPlease enter either 1 or 2!\n")
if yesno == 1:
order_bike()
def create_bill(bike_id, quantity):
bike_list = get_bike_list()
bike = bike_list.get(bike_id)
if user_current_details == {}:
user_details = get_user_details()
else:
user_details = user_current_details
name = user_details.get("name")
address = user_details.get("address")
phone = user_details.get("phone")
email = user_details.get("email")
amount_paid = int(bike[PRICE_INDEX].replace("$", "")) * quantity
bike_name = bike[NAME_INDEX]
company_name = bike[COMPANY_INDEX]
bike_color = bike[COLOR_INDEX]
bike_price = bike[PRICE_INDEX]
date_today = datetime.now().strftime("%Y-%m-%d")
time = datetime.now().strftime("%H:%M:%S")
time_today = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
remove_bike_stock(bike_id, quantity)
file_name = BILLS_PATH + str(phone) + "_" + name + "_"+ time_today +".txt"
file = open(file_name, "w")
content = f"""Name: {name} \nAddress: {address} \nPhone: {phone} \nEmail: {email}\n\nBike Name: {bike_name} \nCompany Name: {company_name} \nColor: {bike_color} \nPrice: {bike_price}\nQuantity purchased: {quantity} \nAmount Paid: ${amount_paid} \nDate: {date_today} \nTime: {time}"""
file.write(content)
file.close()
print("\nYour bill\n")
print(content)
purchase_more()
def add_bike_stock(bike_id, quantity_to_add):
bike_list = get_bike_list()
bike = bike_list.get(bike_id)
new_content = []
read = open("bike.txt", "r")
for line in read:
detail = line.replace("\n", "").split(",")
bike_quantity = int(detail[QUANTITY_INDEX])
if detail[NAME_INDEX] == bike[NAME_INDEX]:
detail[QUANTITY_INDEX] = str(bike_quantity + quantity_to_add)
new_content.append(','.join(detail))
read.close()
file = open("bike.txt", "w")
file.write('\n'.join(new_content))
file.close()
def remove_bike_stock(bike_id, quantity_to_remove):
bike_list = get_bike_list()
bike = bike_list.get(bike_id)
new_content = []
read = open("bike.txt", "r")
for line in read:
detail = line.replace("\n", "").split(",")
bike_quantity = int(detail[QUANTITY_INDEX])
if detail[NAME_INDEX] == bike[NAME_INDEX]:
detail[QUANTITY_INDEX] = str(bike_quantity - quantity_to_remove)
new_content.append(','.join(detail))
read.close()
file = open("bike.txt", "w")
file.write('\n'.join(new_content))
file.close()
def out_of_stock(bike_name):
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print("\t", bike_name, "is currently out of stock!")
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
def less_stock(name, quantity):
print("\n")
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print("\tThe quantity you need of", name, "is not available.")
print("\tAvailable Quantity:", quantity)
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
def display_bikes():
print("\n")
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print("Bike ID\t\tBike Name\t\tCompany Name\tColor\t\tQuantity \tPrice")
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
file = open("bike.txt", "r")
id = 1
for line in file:
print(id, "\t\t"+ line.replace(",", "\t\t"))
id = id + 1
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
file.close()
def get_bike_list():
file = open("bike.txt", "r")
id = 1
bikes = {}
for line in file:
bikes[id] = line.replace("\n", "").split(",")
id = id + 1
return bikes
def order_bike():
bike_list = get_bike_list()
loop = True
while loop == True:
display_bikes()
print("\n")
bike_id = 0
while bike_id == 0:
try:
bike_id = int(input("Enter the id of the bike you want to purchase: "))
except:
print("Please enter valid number for bike id!")
if bike_id > len(bike_list) or bike_id < 1:
bike_not_found()
else:
validate_quantity(bike_id)
loop = False
def validate_quantity(bike_id):
bike_list = get_bike_list()
bike = bike_list.get(bike_id)
name = bike[NAME_INDEX]
quantity = int(bike[QUANTITY_INDEX])
if quantity > 1:
loop = True
while loop == True:
purchase_quantity = 0
while purchase_quantity == 0:
try:
purchase_quantity = int(input("Enter the quantity you want to purchase: "))
except:
print("\nPlease enter valid number for quantity!\n")
if purchase_quantity > 0:
if quantity >= purchase_quantity:
loop = False
create_bill(bike_id, purchase_quantity)
else:
less_stock(name, quantity)
else:
print("Please enter a valid amount!")
else:
out_of_stock(name)