#C5818. Shopping Cart Command Processor
Shopping Cart Command Processor
Shopping Cart Command Processor
This problem requires you to implement a shopping cart system which processes a series of commands from standard input. The shopping cart supports three commands:
- add item_id quantity price: Adds the given quantity of an item with the specified price to the cart. If the item is already in the cart, its quantity increases by the given amount.
- remove item_id quantity: Removes the specified quantity of an item from the cart. If the quantity to remove equals the quantity in the cart, the item is removed entirely. If the item does not exist or if the removal quantity exceeds the available quantity, output "Item not found".
- total: Calculates and outputs the total cost of items in the cart. The total cost is computed by \(\text{Total} = \sum_{i=1}^{n} (\text{quantity}_i \times \text{price}_i)\).
If an invalid command is provided, output "Invalid command". Only commands that produce an output (i.e. "total", error messages from a faulty "remove", or an invalid command) should produce output on their own line.
inputFormat
The input starts with an integer n representing the number of commands. The following n lines each contain one of the commands as described above.
Input is read from standard input (stdin).
outputFormat
For each command that requires an output (i.e. the result of the "total" command or error messages), print the result to standard output (stdout) on a new line.
## sample5
add 1 2 10.0
add 2 1 20.0
total
remove 1 1
total
40.0
30.0
</p>