SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Classes And Object Oriented Programming
Exercise:
Programming Exercises
Question:7 | ISBN:9780132576376 | Edition: 2

Question

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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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

 

0 0

Discussions

Post the discussion to improve the above solution.