-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanageBooks.py
251 lines (224 loc) · 11.8 KB
/
manageBooks.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
from menu_system import *
from book import Book
from author import Author
from genre import Genre
from manageGenres import *
from manageAuthors import *
import os
def manageBooks():
while True:
show_manage_books_menu()
choice = input("Please select the number associated with the action you want to perform: ").strip()
match choice:
case "1":
while True:
clear_console_screen()
print("Adding a new book...")
try:
book_title = input("Enter Book Title: ").title().strip()
if not book_title:
raise ValueError("Book title cannot be empty.")
Genre.listGenres()
while True:
try:
genre_name = input("Enter Genre name: ").title().strip()
genre = Genre(genre_name=genre_name)
if not genre.checkGenreExistsByName():
print("Genre not found! Please add it to the database to proceed.")
time.sleep(1.5)
clear_console_screen()
add_genre_prompt()
genre_id = genre.fetchGenreId()
break
else:
genre_id = genre.fetchGenreId()
break
except Exception:
print("Invalid input. Please enter a valid genre name.")
Author.listAuthors()
while True:
try:
author_name = input("Now select an author: ").title().strip()
author = Author(author_full_name=author_name)
if not author.checkAuthorExists():
print("Author not found! Please add it to the database and then pick the appropriate author")
time.sleep(1.5)
clear_console_screen()
add_author_prompt()
author_id = author.fetchAuthorId()
break
else:
author_id = genre.fetchGenreId()
break
except ValueError:
print("Invalid input. Please enter a valid integer.")
while True:
try:
price = float(input("Enter Book Price: ").strip())
if price < 0:
print("Price cannot be negative. Please try again.")
else:
break
except ValueError:
print("Invalid input. Please enter a valid number for the price.")
while True:
try:
stock_quantity = int(input("Enter Stock Quantity: ").strip())
if stock_quantity < 0:
print("Stock quantity cannot be negative. Please try again.")
else:
break
except ValueError:
print("Invalid input. Please enter a valid integer for the stock quantity.")
# Create and add the book
book = Book(book_title=book_title, genre_id=genre_id, author_id=author_id, price=price, stock_quantity=stock_quantity)
book.addBook()
print("Book added successfully!")
add_another = input("Do you want to add another book? (yes/no): ").strip().lower()
if add_another != 'yes':
break # Exit the loop and return to the main menu
except ValueError as ve:
print(f"Input Error: {ve}")
except Exception as e:
print(f"Unexpected Error: {e}")
input("Press Enter to try again...")
case "2":
clear_console_screen()
print("Updating an existing book...")
Book.listBooksByIdentifier()
try:
book_id = int(input("Enter Book ID to update: ").strip())
if book_id <= 0:
raise ValueError("Book ID must be a positive integer.")
else:
clear_console_screen()
print(f"Currently modifying the following book:")
book = Book(book_id=book_id)
book.fetchSingleBook() # Fetch and display book details
show_update_books_menu()
update_choice = input("Please select the number associated with the action you want to perform: ").strip()
match update_choice:
case "1": # Update book title
clear_console_screen()
print(f"Currently modifying book title of book:")
book.fetchSingleBook()
new_title = input("Enter new Book Title: ").strip().title()
if not new_title:
raise ValueError("Book title cannot be empty.")
else:
book = Book(book_id=book_id, book_title=new_title)
book.updateBookTitle()
print("Book title updated successfully!")
case "2": # Update genre
clear_console_screen()
Genre.listGenres()
new_genre_id = int(input("Enter new Genre ID for this book: ").strip())
if new_genre_id <= 0:
raise ValueError("Genre ID must be a positive integer.")
else:
book = Book(book_id=book_id, genre_id=new_genre_id)
book.updateBookGenre()
print("Genre updated successfully!")
case "3": # Update author
clear_console_screen()
Author.listAuthors()
new_author_id = int(input("Enter new Author ID: ").strip())
if new_author_id <= 0:
raise ValueError("Author ID must be a positive integer.")
else:
book = Book(book_id=book_id, author_id=new_author_id)
book.updateBookAuthor()
print("Author updated successfully!")
case "4": # Update price
clear_console_screen()
Book.fetchSingleBook(book_id=book_id)
new_price = float(input("Enter new Book Price: ").strip())
if new_price < 0:
raise ValueError("Price cannot be negative.")
else:
book = Book(book_id=book_id, price=new_price)
book.updateBookPrice()
print("Price updated successfully!")
case "5": # Update stock quantity
clear_console_screen()
Book.fetchSingleBook(book_id=book_id)
new_stock_quantity = int(input("Enter new Stock Quantity: ").strip())
if new_stock_quantity < 0:
raise ValueError("Stock quantity cannot be negative.")
else:
book = Book(book_id=book_id, stock_quantity=new_stock_quantity)
book.updateBookStock()
print("Stock quantity updated successfully!")
case "0":
clear_console_screen()
break
case _: # Handle invalid choices
print("Invalid choice. Please try again.")
except ValueError as ve:
print(f"Input Error: {ve}")
except Exception as e:
print(f"Unexpected Error: {e}")
case "3":
clear_console_screen()
print("Deleting a book...")
Book.listBooksByIdentifier()
try:
book_id = int(input("Enter Book ID to delete: ").strip())
if book_id <= 0:
raise ValueError("Book ID must be a positive integer.")
book = Book(book_id=book_id)
book.deleteBook()
except ValueError as ve:
print(f"Input Error: {ve}")
except Exception as e:
print(f"Unexpected Error: {e}")
case "4": #Option 4
clear_console_screen()
print("Listing by Identifier")
try:
Book.listBooksByIdentifier()
except Exception as e:
print(f"Error listing books: {e}")
case "5": # Option 5
clear_console_screen()
print("Listing by Title")
try:
Book.listBooksByTitle()
except Exception as e:
print(f"Error listing books: {e}")
case "6": # Option 6
clear_console_screen()
print("Listing by Author")
try:
Book.listBooksByAuthor()
except Exception as e:
print(f"Error listing books: {e}")
case "7":
clear_console_screen()
print("Listing by Stock")
try:
Book.listBooksByStock()
except Exception as e:
print(f"Error listing books: {e}")
case "8":
clear_console_screen()
print("Listing by Price")
try:
Book.listBooksByPrice()
except Exception as e:
print(f"Error listing books: {e}")
case "9":
clear_console_screen()
print("Listing low stock books:")
try:
Book.listLowStockBooks()
except Exception as e:
print(f"Error listing books: {e}")
case "0":
print("Returning to main menu...")
clear_console_screen()
break
case _:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
manageBooks()