Topic 9: Aggregation - Objects within Objects

So far we have considered single objects, or objects within arrays or ArrayLists. However, real object-oriented systems typically have many objects interacting with one another. A common scenario is to have objects within other objects and this is called aggregation.

Why is aggregation useful?

Imagine we wanted to write a program to manage stock for a shop. We could write a main() which creates an ArrayList of Product objects (as in the example last week) and implement functionality within the main() to search for products, sells products, adds new products and so on. However a more object-oriented approach would be to create a class which represents the shop as a whole. This class could be called Shop. It could contain methods to add a new product, search for a product, or sell a product, and could contain, within it, an ArrayList of Product objects (this would be the aggregation). The advantage of creating a Shop class is that it could be reusable: we could create a Shop class which represents a shop, and then reuse it in many different programs.

Example

This example shows the use of a Shop class, as well as a Product class and a test main. Note how, as for last week, I have removed the this. when referring to attributes.

// Product class
public class Product
{
    private String name;
    private double price;
    private int quantityInStock;
    
    public Product (String nameIn, double priceIn, int quantityIn)
    {
        name = nameIn;
        price = priceIn;
        quantityInStock = quantityIn;
    }
    
    public void print()
    {
        System.out.println("Name=" + name + " Price=" + price + " Quantity in stock=" + quantityInStock);
    }
    
    public void sell()
    {
        if(quantityInStock > 0)
        {
            quantityInStock--;
        }
        else
        {
            System.out.println("Insufficient quantity in stock");
        }
    }
    
    public String getName()
    {
        return name;
    }
}
// Shop class
import java.util.ArrayList;

public class Shop
{
    private String name;
    private ArrayList<Product> products;
    
    public Shop(String nameIn)
    {
        name = nameIn;
        products = new ArrayList<Product> ();
    }
    
    public void addProduct (Product p)
    {
        products.add(p);
    }

    // Search for a product by name 
    public Product searchForProduct (String searchName)
    {
        for(int count=0; count < products.size(); count++)
        {
            Product currentProduct = products.get(count);
            if(currentProduct.getName().equals(searchName))
            {
                return currentProduct;
            }
        }
        return null;
    }
    
    public void sellProduct (String productName)
    {
        Product p = this.searchForProduct (productName);
        if (p != null)
        {
            p.sell();
            p.print();
        }
        else
        {
            System.out.println("No product with that name");
        }
    }
}
// Test main
public class ShopTestApp
{
    public static void main (String[] args)
    {
        Shop shop = new Shop("Cottage Stores");
        
        shop.addProduct
            (new Product ("Cheese", 1.99, 10));
        shop.addProduct
            (new Product("Milk", 0.50, 20));
        shop.addProduct
            (new Product("Bread", 1.19, 15));
        
        shop.sellProduct("Cheese");
        shop.sellProduct("Spam");
    }
}
How is this working?

Exercise - Address Book

The exercise involves writing a simple address book system. There are two classes: Contact (representing a contact in the address book) and AddressBook (representing the address book as a whole).