Chapter 12 - Java for Beginners Course

Defining our vending machine

By: Andres Perez

The VendingMachine class will contain the business logic for the operation of the vending machine. It receives a ProductCatalog at construction that can’t be changed at any time later.

Similarly to Chapter 4, it also stores the current balance in the machine and maximum balance the machine can hold, this time as the static constant MAX_ALLOWED_BALANCE.

The VendingMachine constructor

public class VendingMachine {

    private static final double MAX_ALLOWED_BALANCE = 1000.00;
    private final ProductCatalog catalog;
    private double balance = 0.00;

    public VendingMachine(ProductCatalog catalog) {
        if (catalog == null || catalog.getProducts() == null || catalog.getProducts().isEmpty()) {
            throw new IllegalArgumentException("catalog is null or empty");
        }
        for (ProductDetail product : catalog.getProducts()) {
            if (product.getPrice() > MAX_ALLOWED_BALANCE) {
                throw new IllegalArgumentException("Price of a product exceeds " + MAX_ALLOWED_BALANCE);
            }
        }
        this.catalog = catalog;
    }

    public ProductCatalog getProductCatalog() {
        return catalog;
    }

}

Following the requirements, the constructor will throw an IllegalArgumentException if the catalog passed is null, if its Set of ProductDetail objects is null, or if it doesn’t contain any products.

Also, we go over all products in the catalog and check if any of the products exceeds the MAX_ALLOWED_BALANCE, and also throw an IllegalArgumentException in that case.

There are multiple ways of checking all elements in the catalog. We’ll go through them in the next section.
We could also check the prices by creating a checkProductPrices() method to improve readability.

Furthermore, we also created a method that returns the ProductCatalog that was used to create the VendingMachine.

We will use the same implementations for the getBalance, giveChange and addMoney methods as in Chapter 4. The only change will be using the MAX_ALLOWED_BALANCE as the limit inside addMoney:

public double getBalance() {
    return balance;
}

public void addMoney(double amountToAdd) {
    // only add the money to the balance if it doesn't exceed the allowed maximum
    if (balance + amountToAdd <= MAX_ALLOWED_BALANCE) {
        balance += amountToAdd;
    }
}

public double giveChange() {
    double changeToGive = balance;
    balance = 0;
    return changeToGive;
}

The new deliverItem method

Our new deliverItem method will now receive as an argument the productCode of the desired product.

It will go over all products in the catalog and check if an item with the specified code exists and check if there is enough balance for the purchase.

If the purchase is successful, the price will be deducted from the balance and the method will return true. Otherwise, the method will return false:

public boolean deliverItem(String productCode) {
    // Go through all items until finding the chosen one,
    // then deduct the cost and return true only if there
    // is enough balance.
    for (ProductDetail product : catalog.getProducts()) {
        if (product.getProductCode().equals(productCode)) {
            if (balance >= product.getPrice()) {
                balance -= product.getPrice();
                return true;
            } else {
                return false;
            }
        }
    }
    return false;
}