#K94262. Inventory Management System
Inventory Management System
Inventory Management System
You are given an inventory management system that can store at most \(N\) distinct items. Each item has a name and a quantity. You need to implement commands to add or remove items from the inventory.
When adding an item:
- If the item already exists, increase its quantity by the given amount.
- If the item does not exist and the inventory has not reached its maximum distinct item capacity \(N\), add the item with the given quantity.
- If the inventory is full (i.e. it already contains \(N\) distinct items), ignore the add command.
When removing an item:
- If the item exists, reduce its quantity by the given amount. If the removal amount is greater than or equal to the current quantity, remove the item entirely from the inventory.
- If the item does not exist, ignore the command.
After processing each command, output the current state of the inventory. The inventory should be sorted lexicographically by item name.
Note: Input is read from stdin
and output is printed on stdout
. Each line of output corresponds to the inventory state after processing each command. Each item in the inventory is printed in the format item:quantity
and items are separated by a single space. If the inventory is empty, print EMPTY
.
inputFormat
The first line contains a positive integer \(N\) (maximum distinct items in the inventory).
The second line contains a positive integer \(M\), denoting the number of commands.
The following \(M\) lines each contain a command in one of the following formats:
ADD item_name quantity
REMOVE item_name quantity
All tokens in a command are separated by space. The quantity
is a positive integer.
outputFormat
For each command, output one line representing the current inventory state after processing that command. The inventory should be printed as a list of items sorted lexicographically by item name. Each item must be printed in the format item:quantity
and separated by a single space. If the inventory is empty, output EMPTY
.
5
5
ADD potion 5
ADD sword 3
REMOVE potion 2
ADD shield 4
REMOVE sword 3
potion:5
potion:5 sword:3
potion:3 sword:3
potion:3 shield:4 sword:3
potion:3 shield:4
</p>