Advertisement
robjones90

J Hamburger 2

Jun 23rd, 2022
937
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import operator
  2. fields = operator.attrgetter('title','author','isbn','callnumber','stock','loaned','available')
  3.  
  4. class Book:
  5.  
  6.     def __init__(self, title, author, isbn, callnumber, stock, loaned):
  7.         if len(title) > 50:
  8.             self.title = title[:50]
  9.         else:
  10.             self.title = title
  11.         if len(author) > 20:
  12.             self.author = author[:20]
  13.         else:
  14.             self.author = author
  15.         if len(isbn) > 13:
  16.             self.isbn = isbn[:13]
  17.         else:
  18.             self.isbn = isbn
  19.         if len(callnumber) > 13:
  20.             self.callnumber = callnumber[:13]
  21.         else:
  22.             self.callnumber = callnumber
  23.         if len(stock) > 10:
  24.             self.stock = stock[:10]
  25.         else:
  26.             self.stock = stock
  27.         if len(loaned) > 10:
  28.             self.loaned = loaned[:10]
  29.         else:
  30.             self.loaned = loaned
  31.  
  32.             self.available = int(self.stock)-int(self.loaned)
  33.  
  34.     def __repr__(self):
  35.         return f"{self.title}\t{self.author}\t{self.isbn}\t{self.callnumber}\t{self.stock}\t{self.loaned}\t{self.available}\n"
  36.  
  37.     @staticmethod
  38.     def input_book():
  39.         title = input("Provide the title of the book> ")
  40.         author = input("Provide the author of the book> ")
  41.         isbn = input("Provide the ISBN of the book> ")
  42.         callnumber = input("Provide the call number of the book> ")
  43.         stock = input("Provide the stock of the book> ")
  44.         return Book(title, author, isbn, callnumber, stock, "0")
  45.  
  46.     def deleteBook():
  47.         del_author = input("Enter the name of the author of the book you wish to delete> ")
  48.         for del_author in books:
  49.             del_number = input("Enter the call number of the book you wish to delete> ")
  50.             for del_number in books:
  51.                 books.remove(del_number)
  52.                 print()
  53.                 print("Book deleted successfully.")
  54.                 print()
  55.             else:
  56.                 print()
  57.                 print("Cannot find that call number.")
  58.                 print()
  59.         else:
  60.             print()
  61.             print("Cannot find that author.")
  62.             print()
  63.  
  64.  
  65. books = list()
  66.  
  67. def inventory():
  68.     with open("books.txt", "r") as inventoryfile:
  69.         for line in inventoryfile:
  70.             strip_lines=line.strip()
  71.             inventory = strip_lines.split(";")
  72.             book = (Book(inventory[0],inventory[1],inventory[2],inventory[3],inventory[4],inventory[5]))
  73.             books.append(book)
  74.  
  75.  
  76. def display():
  77.     print()
  78.     print("Displaying Westlands Books Inventory")
  79.     print()
  80.     fmtstring = '''{:50}\t{:20}\t{:13}\t{:13}\t{:^10}\t{:^10}\t{:^10}'''
  81.     print(fmtstring.format("Name","Author","ISBN","Call Number","Stock","Loaned","Available"))
  82.     for book in books:
  83.         print(fmtstring.format(*fields(book)))
  84.  
  85. '''
  86. #This example works  for printing just two. Keeping it as a reference.
  87. def exportBooks():
  88.    with open ('testbooks.txt', 'w') as f:
  89.            f.write(str(books[0]) + ";" + str(books[1]))
  90.            print()
  91.            print("Export complete.")
  92.            print()
  93. '''
  94. '''
  95. #Failed attempt at exporting, used YouTube video as a guide.
  96. def exportBooks():
  97.    presentsfile = open("testbooks.txt", "r")
  98.    lines = presentsfile.readlines()
  99.    names = lines[0].replace("[","").replace("]","").replace("'","").replace(",","")
  100.    presents = lines[1].replace("[","").replace("]","").replace("'","").replace(",","")
  101.    presentsfile.write(names)
  102. '''
  103. '''
  104. #Failed attempt at overcoming the error.
  105. def exportBooks():
  106.    with open ('testbooks.txt', 'w+') as f:
  107.            expy = f.read()
  108.            expy2 = expy.replace("[","")
  109.            expy3 = expy2.replace("]","")
  110.            f.write(str(books))
  111.            print()
  112.            print("Export complete.")
  113.            print()
  114. '''
  115.  
  116. #This is the working one for now. It writes the list to the file correctly, albeit unformatted.
  117. def exportBooks():
  118.     with open ('testbooks.txt', 'w') as f:
  119.             for book in books:
  120.                 f.write(str(book))
  121.             print()
  122.             print("Export complete.")
  123.             print()
  124.  
  125.  
  126.  
  127.  
  128.  
  129. inventory()
  130. while True:
  131.     print()
  132.     print("Westlands Book Inventory Management Subsystem")
  133.     print("1. Display Inventory")
  134.     print("2. Add a Book")
  135.     print("3. Remove a Book")
  136.     print("4. Export Inventory")
  137.     print("5. Quit IMS")
  138.     choice=int(input("Select an option from the menu> "))
  139.     if(choice==1):
  140.         display()
  141.         print()
  142.  
  143.     elif(choice==2):
  144.         print()
  145.         print("Adding a Book")
  146.         print()
  147.         books.append(Book.input_book())
  148.         print()
  149.         print('Book added successfully.')
  150.  
  151.     elif(choice==3):
  152.         print()
  153.         print("Removing a Book")
  154.         print()
  155.         Book.deleteBook()
  156.  
  157.     #Setup for later, currently doesn't work as intended. It's meant to overwrite but since it's incomplete, it's set to append for now so the data of books.txt isn't replaced with nothing.
  158.     elif(choice==4):
  159.         print()
  160.         print("Exporting Inventory")
  161.         print()
  162.         exportBooks()
  163.  
  164.  
  165.  
  166.     elif(choice==5):
  167.         break
  168.  
  169.     else:
  170.         print()
  171.         print("Invalid input. Please select one of the 5 options specified.")
  172.         print()
Advertisement
RAW Paste Data Copied
Advertisement