#C7911. Inventory Management System
Inventory Management System
Inventory Management System
You are given an initial inventory of a store and a series of operations to update this inventory. The inventory is represented as items with their corresponding quantity. Each operation is of one of the following two types:
restock
: Increase the quantity of the specified item. If the item is not present, add it to the inventory with the given quantity.sell
: Decrease the quantity of the specified item. If the sales quantity is greater than the available stock, set the quantity to zero.
The operations affect the inventory as follows:
\( \text{For restock: } inventory[item] = inventory[item] + \text{quantity} \)
\( \text{For sell: } inventory[item] = \max(inventory[item] - \text{quantity}, 0) \)
After performing all operations, output the final inventory sorted in lexicographical order by the item name. Each line of the output should contain the item name followed by its quantity, separated by a space.
inputFormat
The input is read from standard input (stdin) with the following format:
- The first line contains an integer N, the number of items initially in the inventory.
- The next N lines each contain a string and an integer separated by a space, representing the item name and its quantity respectively.
- The following line contains an integer M, the number of operations to perform.
- The next M lines each contain an operation in the format: <operation_type> <item_name> , where <operation_type> is either 'restock' or 'sell'.
outputFormat
Print the updated inventory to standard output (stdout). For each item in the final inventory, output a line containing the item name and its quantity separated by a space. The items should be printed in lexicographical order by their names.## sample
3
apple 5
banana 8
orange 2
4
restock apple 3
sell banana 5
sell orange 5
restock grape 10
apple 8
banana 3
grape 10
orange 0
</p>