Chapter 12 - Java for Beginners Course

Example project - Vending machine with a CLI

By: Andres Perez

For this example project, we will expand on the Simple Vending Machine explained in Chapter 4 using what we have learned so far.

In summary, we want our new vending machine example to:

  1. Support multiple products instead of a single product.

  2. Provide a Command Line Interface (CLI) such that our users can interact with it.

For the purpose of this example we want our vending machine to be separated into multiple classes.

We’ll be creating 6 classes in the following sections that will model different parts of our example project:

1) A ProductCatalog class

As we’ll now support multiple products, we’ll introduce the concept of a catalog:

The ProductCatalog is an Interface that defines a getProducts() method that returns the set of available products.

2) An InMemoryProductCatalog implementation

Our ProductCatalog is an interface as defined above, so we’ll need a concrete implementation.

For the purpose of the example we’ll use an In-Memory version, but we’ll talk a bit about some of the benefits of doing this separation in the upcoming sections of this chapter.

3) A ProductDetail class

This represents the types of products available in the ProductCatalog.

Our ProductDetail will define a product code, a price and a product name. We’ll model it as an immutable class.

For example, you can think of a filled ProductCatalog like this, where each row is a ProductDetail instance:

code name price


Potato Chips









Gummy Bears





4) The VendingMachine class

This represents our main piece of functionality based on our requirements. We’ll base it upon the VendingMachine class that we saw in Chapter 4 but it will have some changes in requirements and properties.

At construction time, the VendingMachine should:

  1. Receive a ProductCatalog that lists the different products that the VendingMachine can sell. It can’t be changed once the VendingMachine is created.

  2. The VendingMachine should throw an IllegalArgumentException at construction time if:

    1. The ProductCatalog is null or if it returns a null or an empty set of products.

    2. If any of the products provided exceeds 1000 as its price.

Once the VendingMachine is created, it should support the following operations:

  1. Customers can add money to the vending machine (same as in Chapter 4).

  2. Customers can request change if available (same as in Chapter 4).

  3. The vending machine can return the current balance (same as in Chapter 4).

  4. Customers can ask the vending machine to deliver an item given the product code. The vending machine will deliver the item if there is enough balance and will deduct the money from the current balance. If there is not enough balance, the vending machine should return false and the balance should remain the same.

For the purpose of this example project, we’ll make the maximum balance 1000 instead of the 100 in chapter 4. Also, for simplicity, we’re going to assume that there is an unlimited supply of all products defined in the ProductCatalog, so there’s no need to keep track of inventory levels.

  1. The VendingMachine should provide a getProductCatalog() method that returns the catalog that the vending machine was created with.

5) A Command Line Interface (CLI)

We’ll create a class that is responsible of managing the input/output of our CLI. Our CLI will receive the VendingMachine object that we want it to interact with at construction time.

This CLI should allow a user to perform the following operations and should display the following details:

  1. Add money.

  2. Request change.

  3. Deliver a product given a product code.

  4. In the "main menu" of the CLI the "current balance" and the list of available products should always be displayed.

  5. The CLI should be a separate class or set of classes from the main classes mentioned in items 1-3.

6) Our Application entry-point

Finally, we want to put everything together and create an VendingMachineApp class that will create the required objects and put all the things together.

As such, we’ll create a VendingMachineApp class that contains the main method that ties everything together and starts the CLI: . Inside the VendingMachineApp we’ll create the InMemoryProductCatalog with 5 dummy products. . Afterwards instantiate the VendingMachine passing in the catalog. . Then create the CLI that makes use of that VendingMachine object.

There are multiple ways to approach this. We’ll present an approach with the purpose of covering different concepts from the previous chapters. However, if you come up with a slightly different approach that’s fine!