|
| 1 | +class Library: |
| 2 | + def __init__(self, bookslist, name): # Here, name is the library name |
| 3 | + self.booksList = bookslist |
| 4 | + self.name = name |
| 5 | + self.lendDict = {} |
| 6 | + |
| 7 | + def displayBooks(self): |
| 8 | + print(f"We have following books in our library: {self.name}") |
| 9 | + for book in self.booksList: |
| 10 | + print(book) |
| 11 | + |
| 12 | + def lendBook(self, book, user): |
| 13 | + if book in booksList: |
| 14 | + if book not in self.lendDict.keys(): |
| 15 | + self.lendDict.update({book: user}) |
| 16 | + print("Book has been lended. Database updated.") |
| 17 | + else: |
| 18 | + print(f"Book is already being used by {self.lendDict[book]}") |
| 19 | + else: |
| 20 | + print("Apologies! We don't have this book in our library") |
| 21 | + |
| 22 | + def addBook(self, book): |
| 23 | + if book in booksList: |
| 24 | + print("Book already exists") |
| 25 | + else: |
| 26 | + self.booksList.append(book) |
| 27 | + bookDatabase = open(databaseName, "a") |
| 28 | + bookDatabase.write("\n") |
| 29 | + bookDatabase.write(book) |
| 30 | + print("Book added") |
| 31 | + |
| 32 | + def returnBook(self, book): |
| 33 | + if book in self.lendDict.keys(): |
| 34 | + self.lendDict.pop(book) |
| 35 | + print("Book Returned Successfully") |
| 36 | + else: |
| 37 | + print("The book does not exist in the Book Lending Database") |
| 38 | + |
| 39 | + |
| 40 | +def main(): |
| 41 | + while(True): |
| 42 | + print( |
| 43 | + f"Welcome to the {library.name} library. Following are the options,") |
| 44 | + choice = """ |
| 45 | + 1. Display Books |
| 46 | + 2. Lend a Book |
| 47 | + 3. Add a Book |
| 48 | + 4. Return a Book |
| 49 | + """ |
| 50 | + print(choice) |
| 51 | + |
| 52 | + userInput = input("Press Q to quit and C to continue:") |
| 53 | + if userInput == "C": |
| 54 | + userChoice = int(input("Select an option to continue:")) |
| 55 | + if userChoice == 1: |
| 56 | + library.displayBooks() |
| 57 | + |
| 58 | + elif userChoice == 2: |
| 59 | + book = input("Enter the name of the book you want to lend:") |
| 60 | + user = input("Enter the name of the user:") |
| 61 | + library.lendBook(book, user) |
| 62 | + |
| 63 | + elif userChoice == 3: |
| 64 | + book = input("Enter the name of the book you want to add:") |
| 65 | + library.addBook(book) |
| 66 | + |
| 67 | + elif userChoice == 4: |
| 68 | + book = input("Enter the name of the book you want to return:") |
| 69 | + library.returnBook(book) |
| 70 | + |
| 71 | + else: |
| 72 | + print("Please choose a valid option") |
| 73 | + |
| 74 | + elif userInput == "Q": |
| 75 | + break |
| 76 | + |
| 77 | + else: |
| 78 | + print("Please enter a valid option") |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + booksList = [] |
| 83 | + databaseName = input("Enter the name of the database file with extension:") |
| 84 | + bookDatabase = open(databaseName, "r") |
| 85 | + for book in bookDatabase: |
| 86 | + booksList.append(book) |
| 87 | + library = Library(booksList, "PythonX") |
| 88 | + main() |
0 commit comments