01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package horstmann.ch05_invoice;
import java.util.ArrayList;

/**
   A bundle of line items that is again a line item.
 */
public class Bundle implements LineItem
{
  /**
      Constructs a bundle with no items.
   */
  public Bundle() { items = new ArrayList<LineItem>(); }

  /**
      Adds an item to the bundle.
      @param item the item to add
   */
  public void add(LineItem item) { items.add(item); }

  public double getPrice()
  {
    double price = 0;

    for (LineItem item : items)
      price += item.getPrice();
    return price;
  }

  public String toString()
  {
    String description = "Bundle: ";
    for (int i = 0; i < items.size(); i++)
    {
      if (i > 0) description += ", ";
      description += items.get(i).toString();
    }
    return description;
  }

  private ArrayList<LineItem> items;
}