i) Creates a small number of Product instances that may be purchased ii) Accepts simulated 'scanning' of
Fantastic news! We've Found the answer you've been seeking!
Question:
i) Creates a small number of Product instances that may be purchased
ii) Accepts simulated 'scanning' of a Product to identify it (including refusal to identify products which do not exist)
iii) Adds a scanned Product to the CheckoutRegister's list of products being purchased
iv) Allows the checkout of multiple products
v) Accepts 'virtual money' to pay for those products (you must pay enough to cover the cost of the items checked out)
vi) Prints a final receipt of the products purchased, along with the total cost, total paid and any change given.
class Product:
def __init__(self, name, price, barcode, description):
self.name = name
self.price = price
self.barcode = barcode
self.description = description
class CheckoutRegister:
def __init__(self):
self.total_cost = 0
self.items_scanned = []
self.amount_paid = 0
def accept_payment(self, some_amount):
self.amount_paid += some_amount
def scan_item(self, some_product):
self.items_scanned.append(some_product)
self.total_cost += some_product.price
def print_receipt(self):
print("----- Final Receipt -----")
for item in self.items_scanned:
print(f"{item.name} ${item.price:.2f}")
print(" Total amount due: ${:.2f}".format(self.total_cost))
print("Amount received: ${:.2f}".format(self.amount_paid))
print("Change given: ${:.2f}".format(self.amount_paid - self.total_cost))
print(" Thank you for shopping at FedUni!")
from product import Product
from checkoutregister import CheckoutRegister
def main():
print("----- Welcome to FedUni checkout! -----")
# Create instances of Product and CheckoutRegister
product1 = Product("Milk, 2 Litres", 2.0, "123", "Dairy product")
product2 = Product("Bread", 3.5, "456", "Bakery product")
checkout = CheckoutRegister()
while True:
barcode = input("Please enter the barcode of your item: ").strip()
# Check if the barcode exists in your predefined products
if barcode == product1.barcode:
checkout.scan_item(product1)
elif barcode == product2.barcode:
checkout.scan_item(product2)
else:
print("This product does not exist in our inventory.")
another_product = input("Would you like to scan another product? (Y/N) ").strip().lower()
if another_product != 'y':
break
# Calculate the total cost and prompt for payment
print(" Payment due: ${:.2f}".format(checkout.total_cost))
while True:
payment = float(input("Please enter an amount to pay: "))
if payment < 0:
print("We don't accept negative money!")
else:
break
checkout.accept_payment(payment)
# Display receipt
checkout.print_receipt()
next_customer = input("(N)ext customer, or (Q)uit? ").strip().lower()
if next_customer != 'n':
main()
if __name__ == "__main__":
main()
Related Book For
Management Accounting
ISBN: 9780730369387
4th Edition
Authors: Leslie G. Eldenburg, Albie Brooks, Judy Oliver, Gillian Vesty, Rodney Dormer, Vijaya Murthy, Nick Pawsey
Posted Date: