#K92982. Inventory Management System
Inventory Management System
Inventory Management System
You are required to implement a simple inventory management system. In this system, each item is represented by an id (an integer), a name (a string), and a quantity (an integer). The system supports the following operations:
- ADD: Add an item to the inventory. If an item with the same id already exists, its quantity will be incremented by the given amount.
- REMOVE: Remove a specified quantity of an item. If the quantity to remove exceeds the available quantity, report an error. If the quantity becomes zero, the item should be removed entirely.
- GET: Retrieve and output the current quantity for a given item id. If the item does not exist, output 0.
- LIST: List all items in the inventory sorted by name in ascending order. Each item is displayed in the format:
id name quantity
.
If an operation results in an error (for example, trying to remove a non-existent item or trying to remove more than available), the program should output the error message and terminate further processing.
The internal implementation details use an inventory class that supports the aforementioned operations.
inputFormat
The first line of input contains a single integer N, representing the number of commands. The following N lines each contain a command. Each command is one of the following four types:
ADD id name quantity
REMOVE id quantity
GET id
LIST
All numeric values are integers and commands are executed sequentially.
outputFormat
For each GET
command, output the quantity of the specified item on a new line. For each LIST
command, output each item in the inventory on a separate line in the format id name quantity
(sorted by name in ascending order). If an error occurs (due to invalid removal), output the error message and terminate the program immediately.
4
ADD 1 apple 30
GET 1
GET 2
LIST
30
0
1 apple 30
</p>