#K77587. Shopping Cart Action Processor
Shopping Cart Action Processor
Shopping Cart Action Processor
This problem simulates a shopping cart system where a series of actions are performed on the cart. There are three types of actions:
- ADD: Add an item to the cart with a given price and quantity. The format is ADD item_name price quantity. If the item already exists, its quantity is increased and its price is updated.
- REMOVE: Remove a given quantity of an item. The format is REMOVE item_name quantity. If the quantity becomes zero or less, the item is removed from the cart.
- QUERY: Compute and report the total cost of the items currently in the cart.
For each QUERY action, output the total cost of the cart at that moment.
The cost computation is given by the formula:
\( Total = \sum_{i=1}^{m} (price_i \times quantity_i) \)
inputFormat
The input is read from standard input and has the following format:
- The first line contains an integer \( n \) representing the number of actions.
- The next \( n \) lines each contain one action command in one of the following formats:
- ADD item_name price quantity
- REMOVE item_name quantity
- QUERY
outputFormat
For every QUERY command encountered in the input, output a single line to standard output containing the total cost of the cart at that moment.
## sample6
ADD apple 10 5
ADD banana 20 3
QUERY
REMOVE apple 2
QUERY
ADD apple 10 2
110
90
</p>