#C14053. Shopping Cart Simulation
Shopping Cart Simulation
Shopping Cart Simulation
You are required to implement a shopping cart simulation program. The shopping cart supports the following operations:
-
add name price quantity: Adds an item to the cart. If an item with the same name already exists, update its quantity (i.e. add the new quantity to the existing quantity). The price is a floating-point number.
-
remove name: Removes the item with the specified name from the cart. If the item is not found, do nothing.
-
total: Computes and outputs the total cost of all items in the cart. The total cost is computed as (\sum_{i} (\text{price}_i \times \text{quantity}_i)).
The program will read commands from standard input (stdin) and output the result of every (total) command to standard output (stdout). The shopping cart persists throughout all operations.
inputFormat
The input begins with an integer (Q) representing the number of operations to perform. This is followed by (Q) lines, each containing one of the following commands:
- For adding an item:
add name price quantity
(e.g.,add apple 1.0 2
). - For removing an item:
remove name
(e.g.,remove apple
). - For computing total cost:
total
.
The commands are executed in the given order. The cart is initially empty.
outputFormat
Whenever the (total) command is encountered, output a single line with the total cost of all items currently in the cart. The total should be computed as the sum of (\text{price} \times \text{quantity}) for all items. Each output should be printed on its own line.## sample
6
add apple 1.0 2
total
add banana 0.5 3
total
remove apple
total
2.0
3.5
1.5
</p>