Answered step by step
Verified Expert Solution
Question
1 Approved Answer
# Library Project import datetime import uuid class LibrarySystem: def __init__(self, list_of_books, library_name): self.list_of_books = list_of_books
# Library Project import datetime import uuid class LibrarySystem: def __init__(self, list_of_books, library_name): self.list_of_books = list_of_books self.list_of_books = "list_of_books.txt" self.library_name = library_name self.books_dict = {} with open(self.list_of_books) as bk: content = bk.readlines() for line in content: books_title, author, category = line.strip().split(",") book_id = str(uuid.uuid4()) # generate a unique ID self.books_dict.update({book_id: {"book_title": books_title, "author": author, "category": category, "quantity": 5, "availability": "yes"}}) def display_books(self): print("-----------------------------------List Of Books----------------------------") print("Book ID", "\t", "Title", "\t\t\t", "Author", "\t\t", "Category", "\t\t", "Quantity", "\t", "Availability") print("----------------------------------------------------------------------------") for key, value in self.books_dict.items(): print(key, "\t\t", value.get("book_title"), "\t\t", value.get("author"), "\t", value.get("category"), "\t\t", value.get("quantity"), "\t\t", value.get("availability")) def rent_books(self): rented_books = [] while True: search_criteria = input("Enter book ID, title, author, or category to search for a book (q to quit): ") if search_criteria == 'q': break found_books = [] for book_id, book_info in self.books_dict.items(): if search_criteria.lower() in book_info['book_title'].lower() or search_criteria.lower() in book_info[ 'author'].lower() or search_criteria.lower() in \ book_info['category'].lower() or search_criteria == book_id: found_books.append((book_id, book_info)) if not found_books: print(f"No books found for '{search_criteria}'") else: print("Found books:") print("Book ID", "\t", "Title", "\t\t\t", "Author", "\t\t", "Category", "\t\t", "Quantity", "\t", "Availability") print("----------------------------------------------------------------------------") for book_id, book_info in found_books: print(book_id, "\t\t", book_info['book_title'], "\t\t", book_info['author'], "\t", book_info['category'], "\t\t", book_info['quantity'], "\t\t", book_info['availability']) book_id = input("\nEnter the book ID to rent (q to quit): ") if book_id == 'q': break elif book_id not in self.books_dict.keys(): print("Invalid book ID. Please enter a valid book ID.") elif self.books_dict[book_id]['availability'] == 'no': print("This book is currently not available. Please choose another book or try again later.") elif int(self.books_dict[book_id]['quantity']) == 0: print( "Sorry, all copies of this book have been rented out." " Please choose another book or try again later.") else: lender_name = input("Enter your name: ") issue_date = datetime.date.today() self.books_dict[book_id]['lender_name'] = lender_name self.books_dict[book_id]['issue_date'] = issue_date self.books_dict[book_id]['quantity'] = str(int(self.books_dict[book_id]['quantity']) - 1) if int(self.books_dict[book_id]['quantity']) == 0: self.books_dict[book_id]['availability'] = 'no' rented_books.append(self.books_dict[book_id]['book_title']) print( f"\nCongratulations! You have rented the book '" f"{self.books_dict[book_id]['book_title']}' successfully !!!\n") another_rental = input("Do you want to rent another book? (y/n) : ") if another_rental.lower() == 'n': self.print_receipt(rented_books) break def add_books(self): books_title = input("Enter Book Title : ") author = input("Enter Author Name : ") category = input("Enter Book Category : ") quantity = input("Enter Book Quantity : ") if books_title == "" or author == "" or category == "" or quantity == "": print("Please enter valid input for book title, author, category and quantity!!!") return self.add_books() else: with open(self.list_of_books, "a") as b: b.writelines(f"\n{books_title},{author},{category}") book_id = str(uuid.uuid4()) # generate a unique ID self.books_dict.update({book_id: {"book_title": books_title, "author": author, "category": category, "quantity": quantity, "availability": "yes"}}) print(f"The book '{books_title}' has been added successfully !!!") def return_books(self): while True: book_id = input("Enter the book ID to return (6 to quit): ") if book_id == '6': break elif book_id not in self.books_dict.keys(): print("Invalid book ID. Please enter a valid book ID.") elif self.books_dict[book_id]['availability'] == 'yes': print("This book is not currently rented. Please enter a valid book ID.") else: lender_name = self.books_dict[book_id]['lender_name'] issue_date = self.books_dict[book_id]['issue_date'] due_date = issue_date + datetime.timedelta(days=30) return_date = datetime.date.today() days_late = (return_date - due_date).days if return_date > due_date else 0 late_fee = days_late * 0.10 self.books_dict[book_id]['lender_name'] = '' self.books_dict[book_id]['issue_date'] = '' self.books_dict[book_id]['quantity'] = str(int(self.books_dict[book_id]['quantity']) + 1) self.books_dict[book_id]['availability'] = 'yes' print(f"\nThank you for returning the book '{self.books_dict[book_id]['book_title']}'\n") print("Lender Name: ", lender_name) print("Issue Date: ", issue_date.strftime("%Y-%m-%d")) print("Due Date: ", due_date.strftime("%Y-%m-%d")) print("Return Date: ", return_date.strftime("%Y-%m-%d")) print("Days Late: ", days_late) print("Late Fee: $", late_fee) return_option = input("Do you want to return another book? (y/n): ") if return_option.lower() == 'n': break def search_books(self): while True: search_criteria = input("Enter a book title, author, or category to search for a book (q to quit): ") if search_criteria == 'q': break found_books = [] for book_id, book_info in self.books_dict.items(): if search_criteria.lower() in book_info['book_title'].lower() or search_criteria.lower() in \ book_info['author'].lower() or search_criteria.lower() in book_info['category'].lower(): found_books.append((book_id, book_info)) if not found_books: print(f"No books found for '{search_criteria}'") else: print("Found books :") print(" Book ID", "\t", "Title", "\t\t\t", "Author", "\t\t", "Category", "\t\t", "Quantity", "\t", "Availability") print("----------------------------------------------------------------------------") for book_id, book_info in found_books: print(book_id, "\t\t", book_info['book_title'], "\t\t", book_info['author'], "\t", book_info['category'], "\t\t", book_info['quantity'], "\t\t", book_info['availability']) rent_option = input("\nDo you want to rent a book? (y/n): ") if rent_option.lower() == 'y': book_id = input("\nEnter the book ID to rent: ") if book_id in self.books_dict.keys() and self.books_dict[book_id]['availability'] == 'yes': lender_name = input("Please Enter your name: ") issue_date = datetime.date.today() self.books_dict[book_id]['lender_name'] = lender_name self.books_dict[book_id]['issue_date'] = issue_date self.books_dict[book_id]['quantity'] = str(int(self.books_dict[book_id]['quantity']) - 1) if self.books_dict[book_id]['quantity'] == '0': self.books_dict[book_id]['availability'] = 'no' print( f"\nCongratulations! You have rented the book '{self.books_dict[book_id]['book_title']}' " f"successfully !!!\n") return else: print("Invalid book ID or the book is not available for rent. Please enter a valid book ID.") else: return def print_receipt(self, rented_books): print("\n--------------------- Receipt ---------------------") print("Book Title", "\t\t\t", "Return Due Date") print("---------------------------------------------------") due_date = datetime.date.today() + datetime.timedelta(days=30) for book_title in rented_books: print(book_title, "\t\t\t", due_date.strftime("%Y-%m-%d")) print("---------------------------------------------------\n") if __name__ == "__main__": try: mylms = LibrarySystem("list_of_books.txt", "Python's") press_key_list = {"1": "Display Books", "2": "Rent Books", "3": "Donate Books", "4": "Return Books", "5": "Search Books", "6": "Exit"} key_press = False while not (key_press == "6"): print(f"\n-------------Welcome TO {mylms.library_name}'s Library System---------------\n") for key, value in press_key_list.items(): print("Press", key, "To", value) key_press = input("Press Key : ").lower() if key_press == "1": print("\nCurrent Selection : Display Books\n") mylms.display_books() elif key_press == "2": print("\nCurrent Selection : Rent Books\n") mylms.rent_books() elif key_press == "3": print("\nCurrent Selection : Donate Books\n") mylms.add_books() elif key_press == "4": print("\nCurrent Selection : Return Books\n") mylms.rent_books() elif key_press == "5": print("\nCurrent Selection : Search Books\n") mylms.search_books() elif key_press == "6": break else: continue except Exception: print("Something went wrong. Please check again !!!!!")
My python program is not working the above is the code and the output l am getting
File Edit View Navigate Code Refactor Run Iools VCS Window Help Library Project - main.py Library Project main.py main.py x Structure Bookmarks U 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 Run: If A A F A A Library Project.py X list_of_books X Library Project X Version Control print(" Current Selection : mylms.add_books () elif key_press == "4": print(" Current Selection : Return Books ") mylms.rent books () Run elif key_press == "5": print(" Current Selection : Search Books ") mylms. search_books () elif key_press == "6": break else: continue except Exception: wwwwwwwwwwwwww print("Something went wrong. Please check again !!!!!") main x C:\python\Library_Project\Python Project\Scripts\python.exe C:\python\Library_Project\main.py Something went wrong. Please check again !!!!! Process finished with exit code o Donate Books ") TODO Problems >Terminal Python Packages Python Console Services Current File : A3 A4 V X 215:1 CRLF UTF-8 4 spaces Python 3.10 (Library_Project) | Notifications Database iii SciView
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started