Im trying to simulate a bookstore using python with using a book class and a bookstore class. How do i relate the 2 classes and how do I make so that the book class will be automatically added to the BookStore class thats used to manipulate the Books
class Book(object):
def __init__(self, ID, title, ISBN, authorsList, edition,
publication_date, no_of_pages, purchase_price,
selling_price, quantity, discount):
try:
self.ID = int(ID)
self.title = str(title)
self.ISBN = str(ISBN)
self.authorsList = str(authorsList)
self.edition = str(edition)
self.publication_date = str(publication_date)
self.no_of_pages = int(no_of_pages)
self.purchase_price = float(purchase_price)
self.selling_price = float(selling_price)
self.quantity = int(quantity)
self.discount = float(discount)
except:
print("Error has occurred!")
def getID(self):
try:
return self.ID
except:
print("Error has occurred!")
def setID(self, ID):
try:
self.ID = ID
except:
print("Error has occurred!")
def gettitle(self):
try:
return self.title
except:
print("Error has occurred!")
def __str__(self):
return "Book ID: {}\n Book Title: {}\n Book ISBN: {}\n Book Authors List: {}\n Book Edition: {}\n Book Publication Date: {}\n Book Number of pages: {}\n Book Purchase Price: {}\n Book Selling Price: {}\n Book Quantity: {}\n Book Discount: {}".format(self.ID, self.title, self.ISBN, self.authorsList, self.edition, self.publication_date, self.no_of_pages, self.purchase_price, self.selling_price, self.quantity, self.discount)
class BookStore(Book):
def __init__(self):
self.itemlist =[]
def addBook(self, Book):
return self.itemlist.append(Book)
def DisplayBook():
for i in self.itemlist:
print(i.__str__())
def SearchBookID():
self.itemlist = []
for Book in self.ID:
if Book.ID == ID:
ID.append(Book)
return ID
def delBookID():
return self.itemlist.remove(Book)
If you really want to -- although it's not the recommended way -- you can create something like a class variable
>>> class BookStore:
booklist = []
>>> BookStore.booklist.append(1)
>>> BookStore.booklist.append(2)
>>> BookStore.booklist
[1, 2]
now in __init__
method in your Book
class to add BookStore.booklist.append(book)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.