#C13175. Shopping Cart Operations
Shopping Cart Operations
Shopping Cart Operations
This problem requires you to simulate a shopping cart system. You will be given a series of commands to add items to the cart, remove items, or view the current cart state. When adding an item, if it already exists, update its quantity and price. When removing an item, if the quantity to be removed is greater than or equal to the current quantity, remove the item completely. The total cost is computed using the formula: (Total;cost = \sum (\text{price} \times \text{quantity})).
Implement all operations so that for every VIEW command, you output the current status of the cart as described below.
inputFormat
The input begins with an integer N (the number of operations).
Each of the following N lines contains one of the following commands:
- ADD item_name price quantity: Add an item to the cart. If the item exists, update its price and increase its quantity.
- REMOVE item_name quantity: Remove the specified quantity of an item. If the current quantity is less than or equal to the specified removal quantity, remove the item entirely.
- VIEW: Print the current status of the cart.
outputFormat
For each VIEW command, output the current state of the cart. The output should have the following format:
1. The first line is exactly "Items in cart:".
2. For each item in the cart (in insertion order), output a line in the format "item_name: quantity @ $price each".
3. Finally, output a line "Total cost: $total" where total is the overall cost of all items in the cart, formatted to two decimal places.## sample
4
ADD Apple 0.5 10
ADD Banana 0.2 5
ADD Apple 0.5 5
VIEW
Items in cart:
Apple: 15 @ $0.5 each
Banana: 5 @ $0.2 each
Total cost: $8.50
</p>