#C14570. Vending Machine Simulator
Vending Machine Simulator
Vending Machine Simulator
This problem requires you to simulate a vending machine that can handle various operations including adding money, selecting an item, checking the current balance, and viewing the remaining inventory. The vending machine is initialized with a fixed inventory and price list. Specifically, the machine starts with 5 units of Soda priced at $1.50 each and 3 units of Chips priced at $1.00 each.
The operations are given via standard input. For the ADD operation, an amount is added to the current balance. For the SELECT operation, the machine checks:
- If the item does not exist, output:
Item not found.
- If the item is out of stock, output:
Item is out of stock.
- If there is insufficient money (i.e. if \(\text{balance} < \text{price}\)), output:
Not enough money inserted.
- If the purchase is successful, the machine dispenses the item, deducts its price from the balance and returns the change. Formally, the change is computed as \(\text{change} = \text{balance} - \text{price}\), and then the balance is reset to 0. The output should be:
Dispensed [item]. Change returned: $[change]
formatted to two decimal places.
Other commands include:
BALANCE
: Output the current balance formatted as a floating-point number with two decimals.INVENTORY
: Output the current inventory in the formatSoda:X, Chips:Y
where X and Y are the remaining quantities.
Your solution should read input from standard input (stdin) and print answers to standard output (stdout). Make sure that your implementation passes all the provided test cases.
inputFormat
The first line contains an integer T, the number of operations.
The following T lines each contain one of the following commands:
ADD <amount>
- Add money to the machine.SELECT <item>
- Attempt to purchase the specified item.BALANCE
- Print the current balance.INVENTORY
- Print the current inventory status.
All input is provided via standard input.
outputFormat
For each operation that requires output (SELECT
, BALANCE
, and INVENTORY
), print the corresponding result on a new line.
The outputs must match exactly as specified below:
- For a successful selection:
Dispensed [item]. Change returned: $[change]
(change printed with two decimals). - If not enough money is inserted:
Not enough money inserted.
- If the item is out of stock:
Item is out of stock.
- If the item does not exist:
Item not found.
BALANCE
: The current balance printed as a number with two decimals.INVENTORY
: A string in the formatSoda:X, Chips:Y
.
Output is produced to standard output.
## sample5
BALANCE
INVENTORY
ADD 2
SELECT Soda
BALANCE
0.00
Soda:5, Chips:3
Dispensed Soda. Change returned: $0.50
0.00
</p>