Advertisement
Blessing988

Untitled

Apr 24th, 2020
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. #class=> Library
  2. #Layer of Encapsulation=> Display available books, lend a book, add a book
  3. #class=> Customer
  4. #Layer of abstraction => request a book, return a book
  5.  
  6. print("\t\t\tBLESSING'S LIBRARY")
  7. print( )
  8.  
  9.  
  10.  
  11.  
  12. class Library():
  13.     def __init__(self, dict_of_books):
  14.         self.availablebooks = dict_of_books
  15.  
  16.     def display_available_books(self):
  17.         print( )
  18.         print("Which genre of book do you want read? The genres available are: Fantasy,Romance, Science Fiction, Adventure, Motivational\n")
  19.         self.genre = input().title()
  20.         for genre,books in self.availablebooks.items():
  21.             for book in books:
  22.                 if self.genre ==genre:
  23.                     print(book)
  24.                
  25.  
  26.     def lend_a_book(self,requestedBook):
  27.         print( )
  28.         for genre, books in self.availablebooks.items():
  29.             for book in books :
  30.                 if requestedBook.title() == book:
  31.                     print(f"You have borrowed {requestedBook} ")
  32.                     self.availablebooks[genre][requestedBook]-=1
  33.                     break
  34.                 if requestedBook not in books:
  35.                     print(f"Sorry {requestedBook} isn't available. Modifications would be made later")
  36.                     break
  37.                 if requestedBook in books and self.availablebooks[genre][requestedBook] ==0:
  38.                     print(f"Sorry {requestedBook} is out of stock.Check up later please")
  39.                     break
  40.                 break
  41.             break
  42.                
  43.                
  44.                
  45.                    
  46.                    
  47.     def add_a_book(self, returnedBook):
  48.         for genre in books:
  49.             for books in genre:
  50.                 if returnedBook in books:
  51.                     self.availablebooks[genre][returnedBook]+=1
  52.                     print("You have returned the book. Thank You!")
  53.        
  54.        
  55.  
  56. class Customer():
  57.  
  58.     def request_a_book(self):
  59.         print("Enter the book you want to request: ")
  60.         self.book = input( ).title()
  61.         return self.book  
  62.  
  63.     def return_a_book(self):
  64.         print("Enter the name of the book you are returning : ")
  65.         self.book = input( )
  66.         return self.book
  67.  
  68.  
  69.    
  70.  
  71.  
  72. library = Library({
  73.                   "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},
  74.                    "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},
  75.                    "Science Fiction":{"The Calculating Stars":3,"Semiosis":13,"Space Opera":7,"The Book Of M":11,"The Gone World":13,"Blacfish City":15},
  76.                    "Adventure":{"Into Thin Air":8,"Into The World":12,"Treasure Island":5,"Journey To The Centre Of The Earth":6, "Heart of Darkness":1},
  77.                    "Motivational":{"The Power Of Positive Thinking":3,"Think And Grow Rich":2, "You Are A Badass":17, "You Can Heal Your Life":16}
  78.                  
  79.                   })
  80. customer = Customer()
  81.    
  82. while True:
  83.     print("Press 1 to display the available books")
  84.     print("Press 2 to request a book")
  85.     print("Press 3 to return a book")
  86.     print("Press 4 to exit")
  87.     print()
  88.    
  89.  
  90.     useroption = int(input( ))
  91.    
  92.     if useroption == 1:
  93.         library.display_available_books()
  94.         print()
  95.     elif useroption ==2:
  96.         request = customer.request_a_book()
  97.         library.lend_a_book(request)
  98.     elif useroption ==3:
  99.         returnedBook = customer.return_a_book()
  100.         library.add_a_book(returnedBook)
  101.     elif useroption ==4:
  102.         quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement