#K34322. Vending Machine Simulation
Vending Machine Simulation
Vending Machine Simulation
In this problem, you need to simulate a vending machine. The vending machine is initialized with a fixed inventory and coin storage. It supports three operations:
- insert: Insert a coin into the machine. Only accepted coin denominations are processed and added to the machine's coin reserve.
- select: Select an item from the machine. The item will be dispensed only if it exists, is in stock, and if enough money has been inserted.
- change: Dispense change after an item has been selected. The change is computed by subtracting the item price from the inserted total. The machine will try to give exact change from the available coins.
The operations will be provided via standard input, and your program should output the results on standard output. For the select
operation, output a boolean value (in lowercase, i.e. "true" or "false"). For the change
operation, if the machine is able to return the exact change, output the coin denominations and the quantity in the format {denomination:count}
sorted in descending order of denomination, or output "0" if no change is due.
Note: The initial configuration is fixed as follows:
- Items: "water" with quantity 1 and price \(1.0\).
- Coins: denominations 0.25, 0.1, 0.05, 0.01 with 10 coins each.
After a purchase, the inserted amount resets and the machine updates its inventory and coin counts accordingly.
inputFormat
The input is read from standard input. The first line contains an integer N representing the number of operations. The next N lines each contain an operation. The operations are of three types:
insert X
— Insert a coin with value X (a floating point number, e.g. 0.25).select ITEM
— Attempt to purchase the item named ITEM (for this problem, only "water" is available).change
— Dispense change after an item selection.
There is no extra input.
outputFormat
For each select
and change
command, output the following:
- For a
select
command, output eithertrue
orfalse
(without quotes) on a separate line. - For a
change
command, if the change dictionary is empty, output0
. Otherwise, output the coin denominations and their counts in descending order of denomination in the formatdenom:count
separated by a single space.
6
insert 0.25
insert 0.25
insert 0.25
insert 0.25
select water
change
true
0
</p>