Advertisement
Blessing988

Untitled

Apr 26th, 2020
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.98 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. import random
  6. import datetime
  7.  
  8. today = datetime.datetime.now()
  9. year = today.year
  10. month = today.month + 2
  11. day = today.day
  12. deadline = datetime.datetime(year, month, day)
  13.  
  14. print("\t\t\tBLESSING'S LIBRARY")
  15. print( )
  16.  
  17. StudentDetails = { }
  18.  
  19.  
  20. class Library():
  21.     def __init__(self, dict_of_books):
  22.         self.availablebooks = dict_of_books
  23.  
  24.     def display_available_books(self):
  25.         print( )
  26.         print("Available Books: ")
  27.         print("______________________________________")
  28.         for book in self.availablebooks:
  29.             print("\t\t\t", book)
  30.         print("_______________________________________")
  31.                
  32.  
  33.     def lend_a_book(self,requestedBook):
  34.         print( )
  35.         global deadline
  36.         my_deadline = deadline.strftime("%A, %B %d, %Y")
  37.  
  38.         if requestedBook in self.availablebooks and self.availablebooks[requestedBook] >=1:
  39.             print(f"You have borrowed \"{requestedBook}\".")
  40.             print(f"The deadline for submission is on {my_deadline}")
  41.             print()
  42.             self.availablebooks[requestedBook]-=1
  43.         elif requestedBook not in self.availablebooks:
  44.             print(f"Sorry \"{requestedBook}\" not available. Modifications will be made latter")
  45.             print()
  46.         elif requestedBook in self.availablebooks and self.availablebooks[requestedBook]==0:
  47.             print(f"Sorry \"{requestedBook}\" is out of stock. Check up later")
  48.             print()
  49.  
  50.                    
  51.    
  52.                    
  53.     def add_a_book(self, returnedBook):
  54.         self.availablebooks[returnedBook]+=1
  55.         print(f"You have returned {returnedBook}. Thank You!")
  56.        
  57.        
  58.  
  59. class Customer():
  60.  
  61.     def request_a_book(self):
  62.         print("Enter the book you want to request: ")
  63.         self.book = input( ).title()
  64.  
  65.         return self.book  
  66.  
  67.     def return_a_book(self):
  68.         print("Enter the name of the book you are returning : ")
  69.         self.book = input( ).title()
  70.         return self.book
  71.  
  72.  
  73.  
  74.  
  75. library = Library({"Lord Of The Rings": 5,"The Colour Of Magic":12,"A Game Of Thrones":25, "The Fellowship Of The Ring":16,"Pride And Prejudice":10,
  76.                    "Fifty Shades Of Grey":4,"The Hating Game":6,"Vision White":14,"Gone With The Wind":2,"The Thorn Birds":7,
  77.                     "The Calculating Stars":3,"Semiosis":13,"Space Opera":7,"The Book Of M":11,"The Gone World":13,"Blackfish City":15,
  78.                     "Into Thin Air":8,"Into The World":12,"Treasure Island":5,"Journey To The Centre Of The Earth":6, "Heart of Darkness":4,
  79.                      "The Power Of Positive Thinking":3,"Think And Grow Rich":2, "You Are A Badass":17, "You Can Heal Your Life":16})
  80.                  
  81.  
  82. customer = Customer()
  83.    
  84.  
  85.  
  86.  
  87. while True:
  88.  
  89.     print("Press 1 to Create Account")
  90.     print("Press 2 to login")
  91.     print("Press 3 to exit")
  92.     print()
  93.     inp = int(input( ))
  94.  
  95.     if inp == 1:
  96.         accountID = random.randint(100000, 999999)
  97.         def CreateAccount():
  98.             name = input("Enter your Name: ")
  99.             email = input("Enter your Email Address ")
  100.             global accountID
  101.             global StudentDetails
  102.             StudentDetails[name] = [accountID, email]
  103.             return True
  104.  
  105.         CreateAccount = CreateAccount()
  106.  
  107.  
  108.  
  109.         if CreateAccount is True:
  110.             print("Account Creation Successful")
  111.  
  112.             print(f"Your Account ID is {accountID}")
  113.             print()
  114.             while True:
  115.                 print("Press 1 to display the available books")
  116.                 print("Press 2 to request a book")
  117.                 print("Press 3 to return a book")
  118.                 print("Press 4 to return to previous menu")
  119.                 print()
  120.  
  121.                 useroption = int(input())
  122.                 if useroption ==1:
  123.                     library.display_available_books()
  124.                     print()
  125.                 elif useroption == 2:
  126.                     request = customer.request_a_book()
  127.                     library.lend_a_book(request)
  128.                 elif useroption == 3:
  129.                     returnedBook = customer.return_a_book()
  130.                     library.add_a_book(returnedBook)
  131.                 elif useroption ==4:
  132.                     break
  133.         else:
  134.             print("Account Creation Failed")
  135.     elif inp == 2:
  136.         def login():
  137.             name = input("Enter your name:")
  138.             email = input("Enter your email address: ")
  139.             accountid = (input("Enter your account Id: "))
  140.             global StudentDetails
  141.             if name in StudentDetails:
  142.                 if email and accountid in name:
  143.                     return True
  144.                 else:
  145.  
  146.                     return False
  147.             else:
  148.  
  149.                 return False
  150.  
  151.  
  152.         login = login()
  153.         if login is True:
  154.             print("Login Successful")
  155.             print()
  156.             while True:
  157.                 print("Press 1 to display the available books")
  158.                 print("Press 2 to request a book")
  159.                 print("Press 3 to return a book")
  160.                 print("Press 4 to return to previous menu")
  161.                 print()
  162.  
  163.                 useroption = int(input())
  164.                 if useroption == 1:
  165.                     library.display_available_books()
  166.                     print()
  167.                 elif useroption == 2:
  168.                     request = customer.request_a_book()
  169.                     library.lend_a_book(request)
  170.                 elif useroption == 3:
  171.                     returnedBook = customer.return_a_book()
  172.                     library.add_a_book(returnedBook)
  173.                 elif useroption == 4:
  174.                     break
  175.         else:
  176.             print("Login not Successful. Invalid Name or Account ID")
  177.     elif inp ==3:
  178.         quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement