I'm a little confused on how to append a new object to a list. I feel like I'm overthinking it, but let me know if I'm on the right track here.
EDIT: Removed old code
To be clear, I don't want to edit the text file referenced in display() directly (and then read off that), I want to use the append method to add a new book to the end of the list. I get the objects through a split using a semicolon, so when I'm adding new books, should they also have the semicolon present to match the rest of the list?
EDIT: Here is what I have now.
class Book:
def __init__(self, title, author, isbn, callnumber, stock, loaned):
self.title = title
self.author = author
self.isbn = isbn
self.callnumber = callnumber
self.stock = stock
self.loaned = loaned
self.available = int(self.stock)-int(self.loaned)
def getTitle(self):
return self.title
def getAuthor(self):
return self.author
def getISBN(self):
return self.isbn
def getCallNumber(self):
return self.callnumber
def getStock(self):
return self.stock
def getLoaned(self):
return self.loaned
def getAvailable(self):
return self.available
def __repr__(self):
return self.title '\t' self.author '\t' self.isbn '\t' self.callnumber '\t' self.stock '\t' self.loaned '\n'
def inventory():
fmtstring = '''{:<50}\t{:<20}\t{:<13}\t{:<13}\t{:<10}\t{:<10}\t{:<10}'''
print(fmtstring.format("Name","Author","ISBN","Call Number","Stock","Loaned","Available"))
books = []
with open("books.txt", "r") as inventoryfile:
for line in inventoryfile:
strip_lines=line.strip()
inventory = strip_lines.split(";")
book = (Book(inventory[0],inventory[1],inventory[2],inventory[3],inventory[4],inventory[5]))
books.append(book)
print(fmtstring.format(*fields(book)))
@staticmethod
def input_book():
title = input("Provide the title of the book> ")
author = input("Provide the author of the book> ")
isbn = input("Provide the ISBN of the book> ")
callnumber = input("Provide the call number of the book> ")
stock = input("Provide the stock of the book> ")
return Book(title, author, isbn, callnumber, stock, 0)
And these are called on with menu options.
while True:
print()
print("Westlands Book Inventory Management Subsystem")
print("1. Display Inventory")
print("2. Add a Book")
print("3. Remove a Book")
print("4. Export Inventory")
print("5. Quit IMS")
choice=eval(input("Select an option from the menu> "))
if(choice==1):
print()
print("Displaying Westlands Books Inventory")
print()
Book.inventory()
elif(choice==2):
print()
print("Adding a Book")
print()
Book.input_book()
print()
print('Book added successfully.')
This is the "safe" version of my code that doesn't break anything. I've tried the append command in various places but doesn't seem to be working. I can't seem to return what was input into input_books at all. Thanks
CodePudding user response:
Your function that creates a Book
from user input should just return
that Book
rather than trying to add it to a list (primarily because: what list?). If it's a method on Book
, it should be a static method (or a class method), not an instance method, since the point of it is to create an instance. For example:
@staticmethod
def input_book():
title = input("Provide the title of the book> ")
author = input("Provide the author of the book> ")
isbn = input("Provide the ISBN of the book> ")
callnumber = input("Provide the call number of the book> ")
stock = input("Provide the stock of the book> ")
return Book(title, author, isbn, callnumber, stock, 0)
Now outside your Book
class, you can do:
books = []
while True:
print()
print("Westlands Book Inventory Management Subsystem")
print("1. Display Inventory")
print("2. Add a Book")
choice = input("Select an option from the menu> ")
if choice == "1":
print(books)
if choice == "2":
print()
print("Adding a Book")
print()
books.append(Book.input_book())
print()
print('Book added successfully.')
Note that books
isn't a Book
, it's a list
of Book
s.