-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
355 lines (278 loc) · 13.5 KB
/
main.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
############################################
# BookStore, author [email protected] #
############################################
from os import _exit
from typing import Match
from datetime import timedelta, date
import mysql.connector
from book_store import BookStore
# Instantiate new BookStore
book_store = BookStore()
user_logged_in = False
user_id = 0
# Get the subjects
subjects = book_store.get_subjects()
# Method for printing the subjects on the screen with options
def print_subjects(subjects):
index = 0
for subject in subjects:
index += 1
print(f"{index}. {subject[0]}")
def print_login_menu() -> None:
print("\n" * 3)
print("******************************************")
print("*** Welcome to the online Book Store ***")
print("*** Please log in ***")
print("******************************************")
print()
print("1. Member login")
print("2. New member Registration")
print("q. Quit")
def print_main_menu() -> None:
print("\n" * 3)
print("******************************************")
print("*** Welcome to the online Book Store ***")
print("******************************************")
print()
print("1. Browse by subject")
print("2. Search by Author/Title")
print("3. Check out")
print("4. Logout")
def print_search_menu():
print("\n")
print("1. Author search")
print("2. Title search")
print("3. Go back to member menu")
def get_input(text = "Enter your choice: ") -> str:
return str(input(text))
def get_member_login():
member_login = []
member_login.append(input("Enter username: "))
member_login.append(input("Enter password: "))
return member_login
def collect_member_data():
member_data = []
member_data.append(input("Enter first name: "))
member_data.append(input("Enter last name: "))
member_data.append(input("Enter streetaddress: "))
member_data.append(input("Enter city: "))
member_data.append(input("Enter zip: "))
member_data.append(input("Enter phone: "))
member_data.append(input("Enter email address: "))
member_data.append(input("Password: "))
try:
# Try and cast zip to int to check if it is an int
x = int(member_data[4])
except ValueError:
print("Invalid zip, try again.")
return None
return member_data
def add_to_cart():
isbn = get_input("Enter ISBN: ")
qty = get_input("Enter qty: ")
try:
data = (user_id, isbn, int(qty))
except ValueError:
print("Invalid qty, should be a number!")
return
if book_store.add_to_cart(data):
print(f"\n{isbn} was successfully added to cart\n")
else:
print("\nSomething went horrible wrong, please check your input.")
def print_receipt(order_id, cart_contents, member_details):
total_sum = 0
print("\n\n")
print(f"\t\t\t Invoice for order: {order_id}")
print()
print("\tShipping address")
print(f"\tName: \t {member_details[0]}")
print(f"\tAddress: {member_details[1]}")
print(f"\t\t{member_details[2]}")
print(f"\t\t{member_details[3]}")
print("-"*80)
print("ISBN\t TITLE\t\t\t\t\t\t\t\t$\tQty\t\tTotal")
print("-------------------------------------------------------------------------------------------------------------------")
for row in cart_contents:
print(f"{row[0]:<12}{row[1]:<68}${row[3]:>5}\t{row[2]:<10}\t${row[3] * row[2]:<10}")
total_sum += (row[3] * row[2])
print("-------------------------------------------------------------------------------------------------------------------")
print(f"Total: ${total_sum}")
print(f"Estimated delivery date: {date.today() + timedelta(days=7)}" )
print("-------------------------------------------------------------------------------------------------------------------")
def print_title(title):
print(f"Title:\t {title[0]}")
print(f"Author:\t {title[1]}")
print(f"ISBN:\t {title[2]}")
print(f"Price:\t {title[3]}")
print(f"Subject\t {title[4]}")
print("\n")
def main():
global user_logged_in, user_id
wanna_quit = False
while wanna_quit is not True:
# User is authenticated
if user_logged_in:
print_main_menu()
choice = get_input()
match (choice):
# Browse by subject
case "1":
index = 0;
subjects = book_store.get_subjects()
allowed = [str(x) for x in range(1,len(subjects) + 1)]
for subject in subjects:
index += 1
print(f"{index}. {subject[0]}")
choice = get_input("Choice: ")
while(choice not in allowed):
print("Invalid option! Valids are:")
for el in allowed:
print(f"{el}", end=" ")
choice = get_input("\nPlease, make your choice: ")
# Convert the choice to a str again
str_subject = subjects[int(choice)-1]
############
titles = book_store.get_titles_by_subject(str_subject[0])
print(f"{len(titles)} available on this subject!")
i = 0
for title in titles:
print_title(title)
# List two at a time
i += 1
if(i % 2 == 0):
choice = get_input("Enter B to add to cart or\nN to browse 2 more or\nQ for exit to menu: ")
# Get input choice for the book(s)
match(choice):
case "B":
add_to_cart()
case "N":
pass# List two more
case "Q":
break
## Search by author or title ##
case "2":
go_back = False
while(go_back is not True):
allowed = ["1", "2", "3"]
print_search_menu()
choice = get_input("Type in your option: ")
while(choice not in allowed):
print_search_menu()
choice = get_input("Invalid option, enter your option[1,2,3]: ")
match(choice):
case "1":
author = get_input("Enter the exact author: ")
result = book_store.get_titles_by_author(author)
print(f"Found {len(result)} books")
i = 0
for title in result:
print_title()
i += 1
if(i % 3 == 0):
choice = get_input("Enter B to add to cart or\nN to browse 3 or\nQ for exit to menu: ")
while(choice not in allowed):
print(f"{choice} is not a valid option!!" )
choice = get_input("Enter B to add to cart or\nN to browse 3 or\nQ for exit to menu: ")
# Get input choice for the book(s)
match(choice):
case "B":
add_to_cart()
case "N":
pass# List two more
case "Q":
break
case "2":
allowed = ["N", "B", "Q"]
title = get_input("Enter part of the title: ")
result = book_store.get_titles_by_title(title)
print(f"Found {len(result)} books")
i = 0
for title in result:
print(title)
print("---------------------")
i += 1
if(i % 3 == 0):
choice = get_input("Enter B to add to cart or\nN to browse 3 or\nQ for exit to menu: ")
while(choice not in allowed):
print(f"{choice} is not a valid option!! Please check brain-eyes relationship" )
choice = get_input("Enter B to add to cart or\nN to browse 3 or\nQ for exit to menu: ")
# Get input choice for the book(s)
match(choice):
case "B":
add_to_cart()
case "N":
pass# List two more
case "Q":
break
case "3":
#Go back
go_back = True
## Checkout ##
case "3":
order_id = 0
# Retrieve contents for current logged in user
cart_contents = book_store.get_cart(user_id)
total_sum = 0
print("ISBN\t TITLE\t\t\t\t\t\t\t\t$\tQty\t\tTotal")
print("-------------------------------------------------------------------------------------------------------------------")
for row in cart_contents:
print(f"{row[0]:<12}{row[1]:<68}${row[3]:>5}\t{row[2]:<10}\t${row[3] * row[2]:<10}")
total_sum += (row[3] * row[2])
print("-------------------------------------------------------------------------------------------------------------------")
print(f"Total: ${total_sum}")
print("-------------------------------------------------------------------------------------------------------------------")
# Do we want to checkout and pay?
if(get_input("Do you want to checkout? (y/N): ").lower()) == "y":
#Do the checkout, we pass cart_contents so we dont have to retreive it again
order_id = book_store.save_order(cart_contents, user_id)
print_receipt(order_id, cart_contents, book_store.get_member_details(user_id))
# Clear cart
book_store.clear_cart(user_id)
allowed = ["4", "5"]
print("\n")
print("4. Logout")
print("5. Quit")
choice = get_input("Your choice: ")
while(choice not in allowed):
choice = get_input("Invalid choice, make choice: ")
match(choice):
case "4":
user_logged_in = False
user_id = 0
pass
case "5":
exit()
case "4":
user_logged_in = False
user_id = 0
else:
# User not authenticated
print_login_menu()
choice = get_input()
match (choice):
case "1":
# Login user
member_login = get_member_login()
user_details = book_store.member_login(member_login)
while(user_details is None):
print("Failed to login, try again")
member_login = get_member_login()
user_details = book_store.member_login(member_login)
# Login the user if the authentication was successful
if user_details[0] == member_login[1]:
print("Logged in!")
user_logged_in = True
user_id = user_details[1]
else:
print("Failed to login")
case "2":
# Collect member data
member_data = collect_member_data()
if(member_data is not None):
# Create the member
book_store.create_member(member_data)
print("Member created successfully!")
case "q":
wanna_quit = True
if __name__ == "__main__":
main()