Traceback (most recent call last):
File "/home/Store.py", line 116, in
Store.main()
~~~~~~~~~~^^
File "/home/Store.py", line 16, in main
store = Store()
File "/home/Store.py", line 6, in __init__
Product("Whiteboard Marker", 85, 1.50),
^^^^^^^
NameError: name 'Product' is not defined
Store.py
class Store:
def __init__(self):
# Insert 6 lines of code to initialise the fields
self.products = [
Product("Whiteboard Marker", 85, 1.50),
Product("Whiteboard Eraser", 45, 5.00),
Product("Black Pen", 100, 1.50),
Product("Red Pen", 100, 1.50),
Product("Blue Pen", 100, 1.50)
]
self.cash_register = CashRegister()
@staticmethod
def main():
store = Store()
store.menu()
def menu(self):
choice = ''
while choice != 'x':
choice = self.read_choice()
if choice == 's':
self.sell()
elif choice == 'r':
self.restock()
elif choice == 'v':
self.view_stock()
elif choice == 'c':
self.view_cash()
elif choice == 'p':
self.prune_products()
else:
self.help()
print("Done")
def read_choice(self):
choice = input("Choice (s/r/v/c/p/x): ")
return choice
def sell(self):
name = self.read_name().lower()
count = 0
i = 0
for product in self.products:
if product.get_name().lower() == name:
print(f"Selling {product.get_name()}")
n = self.read_number()
total = product.sell(n)
self.cash_register.add(total)
elif name in product.get_name().lower():
if i == 0:
print("Multiple products match:")
print(product)
i += 1
else:
count += 1
if count == len(self.products):
print("No such product")
def restock(self):
name = self.read_name()
count = 0
i = 0
for product in self.products:
if name.lower() in product.get_name().lower():
print(f"Restocking {product.get_name()}")
n = self.read_number()
product.restock(n)
else:
count += 1
if count == len(self.products):
print("Adding new product")
stock = self.read_number()
price = self.read_price()
self.products.append(Product(name, stock, price))
def view_stock(self):
for product in self.products:
print(product)
def view_cash(self):
print(self.cash_register)
def empty(self, products):
matches = []
for product in products:
if product.is_empty():
matches.append(product)
return matches
def prune_products(self):
empty_products = self.empty(self.products)
self.products = [product for product in self.products if product not in empty_products]
def read_name(self):
return input("Name: ")
def read_price(self):
return float(input("Price: $"))
def read_number(self):
return int(input("Number: "))
def help(self):
print("Menu options")
print("s = sell")
print("r = restock")
print("v = view stock")
print("c = view cash")
print("p = prune products")
print("x = exit")
if __name__ == "__main__":
Store.main()
Product.py
class Product:
def __init__(self, product_name, initial_stock, product_price):
# insert 3 lines of code to initialise the fields.
self.name = product_name
self.stock = initial_stock
self.price = product_price
def get_name(self):
return self.name
def is_empty(self):
if self.stock == 0:
return True
else:
return False
# Return true iff this product has at least n stock
def has(self, quantity):
return self.stock >= quantity
# Sell n stock of this product (decrease stock by n)
# and return the amount of money earned (price * n)
def sell(self, quantity):
if quantity <= self.stock:
self.stock -= quantity
else:
print("Not enough stock")
total = quantity * self.price
return total
# Increase stock by n
def restock(self, quantity):
self.stock += quantity
def __str__(self):
return f"{self.name} - {self.stock} at ${self.formatted(self.price)}"
def formatted(self, money):
return f"{money:.2f}"
CashRegister.py
class CashRegister:
def __init__(self):
self.cash = 0.0
def add(self, money):
self.cash += money
def __str__(self):
return "Cash: $" + self.formatted(self.cash)
def formatted(self, amount):
return "{:,.2f}".format(amount)