#C13803. Simple Inventory Management System
Simple Inventory Management System
Simple Inventory Management System
This problem requires you to simulate a simple inventory management system. The system supports three operations:
- add: Add an item with a given quantity and price. If the item already exists, increase its quantity while retaining the price.
- delete: Remove an item from the inventory if it exists.
- search: This operation does not modify the inventory.
You will be given a sequence of operations provided via stdin. The first line contains an integer n denoting the number of operations. Each of the next n lines contains an operation string. An operation string is formatted as follows:
- For add:
add,item,quantity,price
- For delete:
delete,item
- For search:
search,item
Your task is to process these operations and then output the final inventory. The final inventory should be printed as a JSON object on stdout where keys are item names and values are an array of two elements: the total quantity and the price. The keys in the output JSON object must be in lexicographical order.
Note: The search
operation does not alter the state of the inventory.
inputFormat
The first line of input contains an integer n representing the number of operations. Each of the following n lines contains a comma-separated operation command as described above.
Example:
5 add,apple,10,0.5 add,banana,5,0.3 search,banana delete,apple add,banana,2,0.3
outputFormat
Output the final state of the inventory as a JSON object. For each item, the key is the item's name and the value is an array containing two elements: the accumulated quantity (an integer) and the price (a float). The keys should be output in lexicographical order. If the inventory is empty, output an empty JSON object {}
.
Example Output for the sample input above:
{"banana": [7, 0.3]}## sample
1
add,apple,10,0.5
{"apple": [10, 0.5]}