Design a vending machine that allows a user to select a product, insert money, purchase the product, and receive change if required.

The machine should maintain inventory and handle invalid operations such as selecting an unavailable product or inserting insufficient money.

Example 1

Coke price = ₹40

User selects Coke
User inserts ₹20
User inserts ₹20

Machine dispenses Coke.

Example 2

Chips price = ₹30

User selects Chips
User inserts ₹50

Machine dispenses Chips
and returns ₹20 change.

We should also handle:

Product out of stock
Insufficient money
Cancel transaction
Invalid operation depending on machine state

Solution