Problem 5 You\'ve been hired by a small book store to help them add technology t
ID: 3802658 • Letter: P
Question
Problem 5 You've been hired by a small book store to help them add technology to improve their business. You will start by building a class for a book. A book has a title, author, price, and quantity available. You're handed a piece of paper with their inventory Brave New World, Aldous Huxley, $12.75, 2 Jane Eyre, Charlotte Bronte, $10.50, 4 Pride and Prejudice, Jane Austin, $15.00, 1 In Search of Lost Time, Marcel Proust, $9.00, 0 Wuthering Heights, Emily Bronte, $10.50, 3 Rebecca, Daphne du Maurier, $9.00, 2 Building from the code provided in listing 2, Finish creating the Book objects and add them to an inventory list bookList. mplement the findBook function that, given a book title, returns the Book object for that book title Implement the following definition for the Book class.Explanation / Answer
class Book:
# constructor for the Book class
def __init__(self, title, firstName, lastName, price, quantity):
self.title = title
self.firstName = firstName
self.lastName = lastName
self.pric = price
self.quantity = quantity
def inStock(self):
return self.quantity > 0
def sold(self):
if self.inStock():
self.quantity -= 1
else:
print("Book: " + self.title + " is out-of-stock")
def price(self):
return self.pric
def available(self):
return self.quantity
# we will store our book objects in a list
bookList = []
bookList.append(Book("Brave New World", "Aldous", "Huxley", 12.75, 2))
bookList.append(Book("Jane Eyre", "Charlotte", "Bronte", 10.50, 4))
bookList.append(Book("Pride and Prejudice", "Jane", "Austin", 15.00, 1))
bookList.append(Book("In Search of Lost Time", "Marcel", "Proust", 9.00, 0))
bookList.append(Book("Wuthering Heights", "Emily", "Bronte", 10.50, 3))
bookList.append(Book("Rebecca", "Daphane", "du Maurier", 9.00, 2))
def findBook(title):
for book in bookList:
if book.title == title:
return book
# get book price
wh = "Wuthering Heights"
whBook = findBook(wh)
print("{0} sells for {1}".format(wh, whBook.price()))
# update quantity for a sale
isolt = "In Search of Lost Time"
isoltBook = findBook(isolt)
isoltBook.sold()
# update quantity for a sale
bnw = "Brave New World"
bnwBook = findBook(bnw)
print("{0}: {1} copies available".format(bnw, bnwBook.available()))
bnwBook.sold()
print("{0}: {1} copies available".format(bnw, bnwBook.available()))
'''
Sample run
python main.py
Wuthering Heights sells for 10.5
Book: In Search of Lost Time is out-of-stock
Brave New World: 2 copies available
Brave New World: 1 copies available
Code link: https://goo.gl/IwSd3j
'''