8 The vending machine

 

In this capstone, you will

  • Implement functions and classes
  • Code using variables and values
  • Use if-else constructs

In this capstone, you’ll implement a class, load it into the REPL, and interact with it. In particular, you’ll represent a vending machine that sells two products: a white chocolate bar for $1.50 and a granola bar for $1.00. Let’s keep things simple and assume that your appliance doesn’t give any change back.

A customer can buy an item by selecting a specific product and inserting money into the vending machine. Once the vending machine receives the request, it should check that the product is available and the given money is enough; if all the checks are successful, it should collect the money and release the product.

Let’s analyze the business requirements and identify the main components of your vending machine.

8.1 Setting up the vending machine

Figure 8.1 summarizes the execution flow of the interaction between a customer and the vending machine. A customer requests a product to buy. Following this operation, the vending machine should check if the product is in stock and if the money is enough to cover its cost. When rejecting the request, it should display a human-readable text explaining what went wrong. When successful, it should collect the money and release the product and a message that acknowledges the purchase.

8.1.1 The VendingMachine class and its APIs

8.1.2 The vending machine and its operations

8.1.3 Let’s try it out

8.2 Possible improvements to our solution

Summary