#K42972. Inventory Manager
Inventory Manager
Inventory Manager
This problem simulates an inventory management system. You need to implement an InventoryManager
that supports three types of operations:
- ADD: Adds or updates an item in the inventory. If an item exists, its price is updated and the new quantity is added to the existing quantity.
- UPDATE: Sets the quantity of an item to a specific value. If the item does not exist, this operation is ignored.
- BUY: Processes a purchase request. If the item exists and the inventory has at least the requested quantity (i.e. if \(quantity \leq available\)), then the purchase is successful, the inventory is updated and a success message is printed. Otherwise, the transaction fails and an appropriate failure message is printed.
The commands will be provided line by line, and the command END
signals the end of input. All output messages must be printed immediately when a BUY command is processed.
inputFormat
The input consists of several lines. Each line represents a command with the following formats:
ADD item_name price quantity
UPDATE item_name quantity
BUY customer_name item_name quantity
END
(signals end of input)
Each field is separated by a space. The input is read from standard input (stdin).
outputFormat
For each BUY command processed, output a single line to standard output (stdout) indicating the result of the transaction in one of the following formats:
SUCCESS: customer_name bought quantity of item_name
(if the purchase succeeds)FAIL: customer_name could not buy quantity of item_name
(if the purchase fails)
ADD apple 50 10
BUY John apple 5
END
SUCCESS: John bought 5 of apple
</p>