-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorAdministrator.py
More file actions
81 lines (73 loc) · 3.22 KB
/
authorAdministrator.py
File metadata and controls
81 lines (73 loc) · 3.22 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
import database
import mysql.connector
def browseAuthorsAndTitles():
# Connect to database
connection = database.getDatabaseConnection()
cursor = connection.cursor()
while True:
print("Please choose an option:")
print("1. Author Search")
print("2. Title Search")
print("3. Go Back to Member Menu")
choice = input("Select an option: ")
if choice == "1":
searchString = input("Enter a substring to search for in author names: ")
query = "SELECT * FROM books WHERE author LIKE %s"
params = ('%' + searchString + '%',)
elif choice == "2":
searchString = input("Enter a substring to search for in book titles: ")
query = "SELECT * FROM books WHERE title LIKE %s"
params = ('%' + searchString + '%',)
elif choice == "3":
break
else:
print("Invalid choice. Please try again.")
continue
cursor.execute(query, params)
books = cursor.fetchall()
# Display the books three at a time
i = 0
while i < len(books):
print("===================================")
print(f"BOOKS SEARCHED FOR: {searchString}")
print("===================================")
for j in range(i, min(i+3, len(books))):
print(f"ISBN: {books[j][0]}")
print(f"Author: {books[j][1]}")
print(f"Title: {books[j][2]}")
print(f"Price: {books[j][3]}")
print("===================================")
action = input('Press "A" to Add to cart, "N" to see next page, or press ENTER to return to menu: ')
if action == 'a':
# Add book to cart
# Get the user input
while True:
try:
userId = int(input("Enter your user ID: "))
break
except ValueError:
print("Invalid input. Please enter a valid integer.")
isbn = input("Enter the ISBN of the book: ")
quantity = input("Enter the quantity of books: ")
# Query the database to check if the book exists
cursor = connection.cursor()
cursor.execute("SELECT * FROM books WHERE isbn = %s", (isbn,))
book = cursor.fetchone()
if book is None:
print("Error: Book not found.")
else:
# Insert the book into the cart table
try:
cursor.execute("INSERT INTO cart (userid, isbn, qty) VALUES (%s, %s, %s)", (userId, isbn, quantity))
connection.commit()
print(f"{quantity} copies of {book[1]} have been added to your cart.")
except mysql.connector.errors.DatabaseError:
print("Error: Could not add book to cart. Please check your input.")
connection.rollback()
elif action == 'n':
i += 3
continue
else:
break
# Close database connection
connection.close()