#C361. Vending Machine Simulator
Vending Machine Simulator
Vending Machine Simulator
You are required to simulate the behaviour of a vending machine. The machine dispenses products based on the user selection and an inserted amount of money. It also keeps track of the inventory of items. For a given transaction, the machine should:
- Check if the requested item exists in the inventory.
- Verify that the item is still in stock.
- Determine if the inserted money is sufficient to purchase the desired item.
- If sufficient funds are provided, dispense the item and provide the correct change (\(\text{change} = \text{money} - \text{price}\)).
- If any condition is not met, output a specific error message.
Note: All monetary values are in cents.
inputFormat
The input is read from standard input (stdin) in the following format:
<item> <money> <n> <item_1> <price_1> <stock_1> <item_2> <price_2> <stock_2> ... <item_n> <price_n> <stock_n>
- item: A string representing the requested item name.
- money: An integer representing the amount of money (in cents) inserted into the machine.
- n: An integer representing the number of items in the inventory.
- Each of the following n lines contains: the item name (a string), its price (an integer in cents) and the available stock (an integer) separated by spaces.
outputFormat
Output a single line to standard output (stdout) that contains one of the following messages:
- If the item does not exist in the inventory, output:
{item} is not available in this vending machine.
- If the item exists but is out of stock, output:
{item} is out of stock.
- If the provided money is insufficient, output:
Insufficient balance for {item}. Required: {price} cents.
- If the transaction is successful, output:
Dispensed {item}. Change: {change} cents.
wherechange = money - price
.
Soda
125
3
Soda 125 10
Chips 75 15
Candy 85 5
Dispensed Soda. Change: 0 cents.