Cash Register:
This exercise assumes that you have created the RetailItem class for Programming
Exercise 5. Create a CashRegister class that can be used with the RetailItem class. The CashRegister class should be able to internally keep a list of RetailItem objects. The class should have the following methods:
• A method named purchase_item that accepts a RetailItem object as an argument.
Each time the purchase_item method is called, the RetailItem object that is passed as an argument should be added to the list.
• A method named get_total that returns the total price of all the RetailItem objects stored in the CashRegister object’s internal list.
• A method named show_items that displays data about the RetailItem objects stored in the CashRegister object’s internal list.
• A method named clear that should clear the CashRegister object’s internal list.
Demonstrate the CashRegister class in a program that allows the user to select several items for purchase. When the user is ready to check out, the program should display a list of all the items he or she has selected for purchase, as well as the total price.
This program helps to develop the Cash Register system.
# Create a RetailItem class
class RetailItem:
def __init__(self, description, units_on_hand, price):
self.description = description
self.units_on_hand = units_on_hand
self.price = price
# Create a CashRegister class that can be used with the RetailItem class
class CashRegister:
def __init__(self):
self.items_list = []
def purchase_item(self, retail_item):
self.items_list.append(retail_item)
# method named get_total that returns the total price of all the
# RetailItem objects stored in the CashRegister object’s internal list.
def get_total(self):
total_price = 0
for item in self.items_list:
total_price += item.price
return total_price
# A method named show_items that displays data about the
# RetailItem objects stored in the CashRegister object’s internal list.
def show_items(self):
print("Items in the Cash Register:")
for item in self.items_list:
print(f"Description: {item.description}")
print(f"Price: ${item.price:.2f}")
print()
# A method named clear that should clear the
# CashRegister object’s internal list.
def clear(self):
self.items_list = []
def main():
# Sample RetailItem objects
item1 = RetailItem("Shirt", 10, 19.99)
item2 = RetailItem("Jeans", 5, 29.99)
item3 = RetailItem("Shoes", 3, 49.99)
# The CashRegister class should be able to
# internally keep a list of RetailItem objects
cash_register = CashRegister()
while True:
print("\nSelect an item for purchase:")
print("1. Shirt")
print("2. Jeans")
print("3. Shoes")
print("4. Checkout")
print("5. Clear cart")
choice = input("Enter your choice (1-5): ")
if choice == '1':
cash_register.purchase_item(item1)
print("Shirt added to the cart.")
elif choice == '2':
cash_register.purchase_item(item2)
print("Jeans added to the cart.")
elif choice == '3':
cash_register.purchase_item(item3)
print("Shoes added to the cart.")
elif choice == '4':
cash_register.show_items()
total_price = cash_register.get_total()
print(f"Total Price: ${total_price:.2f}")
break
elif choice == '5':
cash_register.clear()
print("Cart cleared.")
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Sample output:
Select an item for purchase:
1. Shirt
2. Jeans
3. Shoes
4. Checkout
5. Clear cart
Enter your choice (1-5): 1
Shirt added to the cart.
Select an item for purchase:
1. Shirt
2. Jeans
3. Shoes
4. Checkout
5. Clear cart
Enter your choice (1-5): 3
Shoes added to the cart.
Select an item for purchase:
1. Shirt
2. Jeans
3. Shoes
4. Checkout
5. Clear cart
Enter your choice (1-5): 4
Items in the Cash Register:
Description: Shirt
Price: $19.99
Description: Shoes
Price: $49.99
Total Price: $69.98