#C14138. Vending Machine Simulation
Vending Machine Simulation
Vending Machine Simulation
This problem requires you to simulate a basic vending machine operation. The machine holds various items, each with a price and available stock. On receiving a purchase request, the machine should check if the item exists, has sufficient stock, and if the inserted money is enough for the price. It then dispenses the item (if possible) and returns the correct change formatted to two decimal places. If the item is not available or the money is insufficient, an appropriate error message should be printed.
When the inserted money is less than the price, the machine should specify the additional amount required using a message in the following format:
"Insufficient money. Please insert at least $\(\texttt{\Delta}\) more." where \(\Delta\) is the difference between the price and the inserted money formatted to two decimals.
The four possible outputs are:
- For a valid transaction: "Dispensing {item_name}. Your change is $X.XX."
- If the item is out of stock: "Sorry, {item_name} is out of stock."
- If the inserted money is insufficient: "Insufficient money. Please insert at least $Y.YY more."
- If the item does not exist: "Invalid item selection." </p>
- The first line contains an integer N, representing the number of items available in the vending machine.
- The next N lines each contain an item description in the following format:
item_name price stock
, whereitem_name
is a string (without spaces),price
is a floating-point number, andstock
is an integer. - The next line contains an integer Q, representing the number of purchase queries.
- The following Q lines each contain a query in the format:
item_name money_inserted
(wheremoney_inserted
is a floating-point number). - "Dispensing {item_name}. Your change is $X.XX."
- "Sorry, {item_name} is out of stock."
- "Insufficient money. Please insert at least $Y.YY more."
- "Invalid item selection."
inputFormat
The input is read from standard input (stdin) in the following format:
outputFormat
For each query, output the result of the vending machine's operation on a separate line. Depending on the scenario, the outputs can be:
3
Soda 1.25 5
Chips 0.75 2
Candy 0.50 0
5
Soda 2.00
Candy 1.00
Chips 0.50
Water 1.00
Soda 1.25
Dispensing Soda. Your change is $0.75.
Sorry, Candy is out of stock.
Insufficient money. Please insert at least $0.25 more.
Invalid item selection.
Dispensing Soda. Your change is $0.00.
</p>