#C12226. Vending Machine Simulation

    ID: 41630 Type: Default 1000ms 256MiB

Vending Machine Simulation

Vending Machine Simulation

Implement a vending machine simulation that supports several operations including displaying available items, selecting an item, inserting money, and vending the item. The machine should process commands from standard input and output results to standard output.

The operations are defined as follows:

  • display: Print the available items in JSON format.
  • select [item_code]: Select an item using its code. If the code is invalid, no item is selected.
  • insert [amount]: Insert money into the machine and print the total inserted so far.
  • vend: Attempt to vend the selected item. If enough money is inserted, dispense the item and print its name along with the change (change = m - p, where \(m\) is the inserted money and \(p\) is the item price). Otherwise, print an error message.

inputFormat

The input is read from standard input and has the following format:

  1. An integer \(N\) representing the number of items.
  2. \(N\) lines each containing an item code (string), an item name (string), and a price (float) separated by spaces.
  3. An integer \(M\) representing the number of commands.
  4. \(M\) lines, each containing one command. Commands can be one of the following:
    • display
    • select [item_code]
    • insert [amount]
    • vend

outputFormat

The output is written to standard output. For each command that produces output, follow these rules:

  • display: Print the items dictionary in JSON format.
  • insert: Print the total money inserted so far (float).
  • vend: Print the result of vending in the format item_name change. If vending cannot be completed, print either Insufficient funds 0 or No item selected 0.
## sample
2
A1 Soda 1.25
A2 Chips 1.00
5
display
select A1
insert 1.00
insert 0.50
vend
{"A1":["Soda",1.25],"A2":["Chips",1.0]}

1.0 1.5 Soda 0.25

</p>