#K38327. Inventory Management System
Inventory Management System
Inventory Management System
You are tasked with implementing an inventory management system for a small shop. The system supports the following operations:
- ADD: Add a new item with a given name, price, and quantity. If the item already exists, update its price to the new price and increase its quantity accordingly.
- UPDATE: Update the quantity of an existing item by a given amount (which can be positive or negative). If the updated quantity is less than or equal to zero, remove the item from the inventory.
- REMOVE: Remove an item from the inventory if it exists.
- PRICE: Retrieve and output the price of the specified item. If the item does not exist, output
None
. - SUMMARY: Output an inventory summary where the items are listed in alphabetical order. For each item, output its name, quantity, and price.
All input is received from stdin
and all output should be sent to stdout
. Note that the price should be printed as a floating-point number with two decimal places when applicable.
The total number of operations is specified on the first line of input followed by one operation per line. Each operation is described by a command and its arguments, separated by spaces.
For example, an input command:
\( \texttt{ADD apple 0.50 10} \)will add an item "apple" with price \(0.50\) and quantity 10.
inputFormat
The first line of input contains a single integer \(n\) (\(1 \le n \le 10^4\)), which denotes the number of operations. Each of the following \(n\) lines contains an operation command in one of the following formats:
ADD name price quantity
UPDATE name delta
REMOVE name
PRICE name
SUMMARY
Note:
name
is a string without spaces.price
is a floating-point number.quantity
anddelta
are integers.
outputFormat
For each operation that requires output, print the result on a separate line:
- PRICE: Print the price of the item (with two decimal places) if it exists; otherwise, print
None
. - SUMMARY: For each item in the inventory (sorted alphabetically by item name), print a line containing the
name
,quantity
, andprice
(with two decimal places), separated by a space. If the inventory is empty, output nothing for this command.
6
ADD apple 0.50 10
ADD banana 0.20 30
PRICE apple
UPDATE apple -5
PRICE apple
SUMMARY
0.50
apple 5 0.50
banana 30 0.20
</p>