Implement a Vending Machine
Problem: Implement a Vending Machine
Design and implement the core logic for a vending machine, adhering to object-oriented principles.
The vending machine must manage product inventory, accept various bill denominations, track the current customer's balance, dispense selected items, and return change.
Requirements
- Each product is defined by an ID, a price in cents, and its remaining quantity.
- The machine accepts a predefined set of bill denominations (e.g., $1, $5, $10, $20 bills).
insertBill(amount): Adds the specified amount to the current customer's transaction balance.selectProduct(productId): This operation must perform the following steps:- Verify that the product exists and is currently in stock.
- Confirm that the customer has inserted sufficient funds for the purchase.
- Deduct one unit from the product's inventory.
- Calculate and dispense the correct change using the bills available within the machine.
- Gracefully handle scenarios where exact change cannot be made.
cancel(): Refunds all money inserted by the customer and resets the current transaction state.
Implementation and Testing
- The implementation should be highly testable.
- Candidates are expected to write unit tests for critical scenarios, including:
- Insufficient funds
- Exact payment
- Overpayment
- Out-of-stock items
- Inability to make exact change
Assumptions
- Starter code for the main classes will be provided; the task is to implement the missing behavior and tests.
AI interview prep
Core dilemma
Ensuring the atomicity and consistency of a financial transaction (product dispensed, money taken, change returned) while managing multiple mutable states (product inventory, machine's cash reserves, customer's balance) and gracefully handling the inability to make exact change.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement a Vending Machine
Problem: Implement a Vending Machine
Design and implement the core logic for a vending machine, adhering to object-oriented principles.
The vending machine must manage product inventory, accept various bill denominations, track the current customer's balance, dispense selected items, and return change.
Requirements
- Each product is defined by an ID, a price in cents, and its remaining quantity.
- The machine accepts a predefined set of bill denominations (e.g., $1, $5, $10, $20 bills).
insertBill(amount): Adds the specified amount to the current customer's transaction balance.selectProduct(productId): This operation must perform the following steps:- Verify that the product exists and is currently in stock.
- Confirm that the customer has inserted sufficient funds for the purchase.
- Deduct one unit from the product's inventory.
- Calculate and dispense the correct change using the bills available within the machine.
- Gracefully handle scenarios where exact change cannot be made.
cancel(): Refunds all money inserted by the customer and resets the current transaction state.
Implementation and Testing
- The implementation should be highly testable.
- Candidates are expected to write unit tests for critical scenarios, including:
- Insufficient funds
- Exact payment
- Overpayment
- Out-of-stock items
- Inability to make exact change
Assumptions
- Starter code for the main classes will be provided; the task is to implement the missing behavior and tests.