#C10005. Vending Machine Simulator
Vending Machine Simulator
Vending Machine Simulator
You are given a vending machine that stores a list of items. Each item has a unique code, a name, a price, and a quantity. Your task is to simulate a series of operations on the vending machine. The operations include making a purchase, restocking an item, and checking the inventory.
For a purchase operation, if the item exists and is in stock, and the amount paid is at least the price of the item, then the purchase is successful: the quantity of that item is decreased by one and the change (\(\text{Change} = \text{amount paid} - \text{price}\)) is returned (formatted to exactly two decimal places). Otherwise, the appropriate error message is returned:
- If the item does not exist:
Item not found
- If the item is out of stock:
Item out of stock
- If the amount paid is insufficient:
Insufficient funds
For a restock operation, if the item exists, add the specified quantity to it and output Item restocked
; otherwise, output Item not found
.
For the inventory operation, output the current inventory in the order of the initial items. Each item is output on its own line with the format: code name price quantity
where the price is printed with two decimal places.
The input and output follow a standard competitive programming format, with all input taken from standard input and all output printed to standard output.
inputFormat
The input begins with an integer \(T\) indicating the number of test cases. Each test case is formatted as follows:
- An integer \(M\), the number of initial items in the vending machine.
- \(M\) lines, each containing an item description with four space-separated values:
code
(a string),name
(a string),price
(a floating-point number), andquantity
(an integer). - An integer \(Q\) indicating the number of operations.
- \(Q\) lines, each describing an operation. An operation is one of the following:
PURCHASE code amount
whereamount
is a float.RESTOCK code quantity
wherequantity
is an integer.INVENTORY
which asks you to print the current state of the inventory.
There is no extra whitespace in the input.
outputFormat
For each operation, output the result on a separate line:
- For a
PURCHASE
operation, output one of:Item: {name}, Change: {change}
(if the purchase is successful, with the change formatted to two decimals)Insufficient funds
Item not found
Item out of stock
- For a
RESTOCK
operation, output either:Item restocked
if successfulItem not found
if the item does not exist
- For an
INVENTORY
operation, output \(M\) lines (one for each item in the initial order) formatted as:code name price quantity
. The price should be printed with two decimal places.
3
3
A1 Soda 1.50 10
B2 Chips 1.00 5
C3 Candy 0.65 15
5
PURCHASE B2 1.00
PURCHASE A1 1.00
RESTOCK B2 10
PURCHASE B2 1.00
INVENTORY
Item: Chips, Change: 0.00
Insufficient funds
Item restocked
Item: Chips, Change: 0.00
A1 Soda 1.50 10
B2 Chips 1.00 13
C3 Candy 0.65 15
</p>