Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #class=> Library
- #Layer of Encapsulation=> Display available books, lend a book, add a book
- #class=> Customer
- #Layer of abstraction => request a book, return a book
- print("\t\t\tBLESSING'S LIBRARY")
- print( )
- class Library():
- def __init__(self, dict_of_books):
- self.availablebooks = dict_of_books
- def display_available_books(self):
- print( )
- print("Which genre of book do you want read? The genres available are: Fantasy,Romance, Science Fiction, Adventure, Motivational\n")
- self.genre = input().title()
- for genre,books in self.availablebooks.items():
- for book in books:
- if self.genre ==genre:
- print(book)
- def lend_a_book(self,requestedBook):
- print( )
- for genre, books in self.availablebooks.items():
- for book in books :
- if requestedBook.title() == book:
- print(f"You have borrowed {requestedBook} ")
- self.availablebooks[genre][requestedBook]-=1
- break
- if requestedBook not in books:
- print(f"Sorry {requestedBook} isn't available. Modifications would be made later")
- break
- if requestedBook in books and self.availablebooks[genre][requestedBook] ==0:
- print(f"Sorry {requestedBook} is out of stock.Check up later please")
- break
- break
- break
- def add_a_book(self, returnedBook):
- for genre in books:
- for books in genre:
- if returnedBook in books:
- self.availablebooks[genre][returnedBook]+=1
- print("You have returned the book. Thank You!")
- class Customer():
- def request_a_book(self):
- print("Enter the book you want to request: ")
- self.book = input( ).title()
- return self.book
- def return_a_book(self):
- print("Enter the name of the book you are returning : ")
- self.book = input( )
- return self.book
- library = Library({
- "Fantasy":{"Lord Of The Rings": 5,"The Colour Of Magic":12,"A Game Of Thrones":25, "The Fellowship Of The Ring":16, "The Colour Of Magic":3},
- "Romance":{"Pride And Prejudice":10,"Fifty Shades Of Grey":4,"The Hating Game":6,"Vision White":14,"Gone With The Wind":2,"The Thorn Birds":7},
- "Science Fiction":{"The Calculating Stars":3,"Semiosis":13,"Space Opera":7,"The Book Of M":11,"The Gone World":13,"Blacfish City":15},
- "Adventure":{"Into Thin Air":8,"Into The World":12,"Treasure Island":5,"Journey To The Centre Of The Earth":6, "Heart of Darkness":1},
- "Motivational":{"The Power Of Positive Thinking":3,"Think And Grow Rich":2, "You Are A Badass":17, "You Can Heal Your Life":16}
- })
- customer = Customer()
- while True:
- print("Press 1 to display the available books")
- print("Press 2 to request a book")
- print("Press 3 to return a book")
- print("Press 4 to exit")
- print()
- useroption = int(input( ))
- if useroption == 1:
- library.display_available_books()
- print()
- elif useroption ==2:
- request = customer.request_a_book()
- library.lend_a_book(request)
- elif useroption ==3:
- returnedBook = customer.return_a_book()
- library.add_a_book(returnedBook)
- elif useroption ==4:
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement