|
| 1 | +class Library: |
| 2 | + def __init__(self, list, name): |
| 3 | + self.booksList = list |
| 4 | + self.name = name |
| 5 | + self.lendDict = {} |
| 6 | + def displayBooks(self): |
| 7 | + print(f"We have following books in our library: {self.name}") |
| 8 | + for book in self.booksList: |
| 9 | + print(book) |
| 10 | + def lendBook(self, user, book): |
| 11 | + if book not in self.lendDict.keys(): |
| 12 | + self.lendDict.update({book:user}) |
| 13 | + print("Lender-Book database has been updated. You can take the book now") |
| 14 | + else: |
| 15 | + print(f"Book is already being used by {self.lendDict[book]}") |
| 16 | + def addBook(self, book): |
| 17 | + self.booksList.append(book) |
| 18 | + print("Book has been added to the book list") |
| 19 | + def returnBook(self, book): |
| 20 | + self.lendDict.pop(book) |
| 21 | +if __name__ == '__main__': |
| 22 | + demo = Library(['Python', 'Rich Daddy Poor Daddy', 'Harry Potter', 'C++ Basics', 'Algorithms by CLRS'], "Programmer") |
| 23 | + while(True): |
| 24 | + print(f"Welcome to the {demo.name} library. Enter your choice to continue") |
| 25 | + print("1. Display Books") |
| 26 | + print("2. Lend a Book") |
| 27 | + print("3. Add a Book") |
| 28 | + print("4. Return a Book") |
| 29 | + user_choice = input() |
| 30 | + if user_choice not in ['1','2','3','4']: |
| 31 | + print("Please enter a valid option") |
| 32 | + continue |
| 33 | + else: |
| 34 | + user_choice = int(user_choice) |
| 35 | + if user_choice == 1: |
| 36 | + demo.displayBooks() |
| 37 | + elif user_choice == 2: |
| 38 | + book = input("Enter the name of the book you want to lend:") |
| 39 | + user = input("Enter your name") |
| 40 | + demo.lendBook(user, book) |
| 41 | + elif user_choice == 3: |
| 42 | + book = input("Enter the name of the book you want to add:") |
| 43 | + demo.addBook(book) |
| 44 | + |
| 45 | + elif user_choice == 4: |
| 46 | + book = input("Enter the name of the book you want to return:") |
| 47 | + demo.returnBook(book) |
| 48 | + else: |
| 49 | + print("Not a valid option") |
| 50 | + print("Press q to quit and c to continue") |
| 51 | + user_choice2 = "" |
| 52 | + while(user_choice2!="c" and user_choice2!="q"): |
| 53 | + user_choice2 = input() |
| 54 | + if user_choice2 == "q": |
| 55 | + exit() |
| 56 | + elif user_choice2 == "c": |
| 57 | + continue |
0 commit comments