Chapter 12 - Java for Beginners Course

Defining our product catalog

By: Andres Perez

In order to store the list of products that will be available in the vending machine, we will use a product catalog.

As mentioned in the previous sections, it should hold the ProductDetail objects in a Set, to avoid any two products in the catalog to have the same productCode.

The ProductCatalog interface

We will first create a ProductCatalog interface that establishes and separates the concerns of the VendingMachine from the specific implementation of the catalog.

This is useful if, for example, after initially creating a catalog that receives the products directly, we want to create another catalog that reads all the products from a file or a different one that reads them from a database.

The specific implementation in each case will vary, but they still need to follow certain rules in order to be used correctly by the VendingMachine. In this case, the ProductCatalog interface acts as the contract that guarantees that the requirements of the VendingMachine are met correctly even if the implementation of each catalog varies.

The ProductCatalog interface will require that any class that implements it, also implements a getProducts method that returns a Set<ProductDetail> object, since that is how the VendingMachine will make use of the product catalog:

public interface ProductCatalog {

    Set<ProductDetail> getProducts();

}

The InMemoryProductCatalog class

Now that we have the ProductCatalog interface, we can implement a catalog.

In our case we will create an InMemoryProductCatalog class, that implements the interface and receives a Set directly with the ProductDetail objects:

public class InMemoryProductCatalog implements ProductCatalog {

    private Set<ProductDetail> products;

    public InMemoryProductCatalog(Set<ProductDetail> products) {
        this.products = new HashSet<ProductDetail>(products);
    }

    @Override
    public Set<ProductDetail> getProducts() {
        return products;
    }

}
We should return/store an immutable set instead. However, as we haven’t covered that in this course we’re returning the products object as is. If you want to check that out, please take a look at: the unmodifiableSet method in java.util.Collections

We store the products in a Set instead of a List with the objective of avoiding the repetition of a productCode in our catalog. This is important since the user will request products from the vending machine using the product codes.

Since we are using a HashSet, we had to override the hashCode and equals methods for the ProductDetail class. Also, we made the class immutable to avoid changes in the value returned by the hashCode method and guarantee the HashSet will behave correctly. The Set will not allow the insertion of two products in the same catalog with the same productCode, because the hashCode and equals methods called by the HashSet only take into consideration the code.

As you can see, we implemented the Set<ProductDetail> getProducts method as specified by the ProductCatalog interface, which will be called by the VendingMachine class that will be covered in the following section.