001package headfirst.composite.menu; 002 003import java.util.ArrayList; 004import java.util.Iterator; 005 006public class Menu extends MenuComponent { 007 ArrayList<MenuComponent> menuComponents = new ArrayList<MenuComponent>(); 008 String name; 009 String description; 010 011 public Menu(String name, String description) { 012 this.name = name; 013 this.description = description; 014 } 015 016 public void add(MenuComponent menuComponent) { 017 menuComponents.add(menuComponent); 018 } 019 020 public void remove(MenuComponent menuComponent) { 021 menuComponents.remove(menuComponent); 022 } 023 024 public MenuComponent getChild(int i) { 025 return menuComponents.get(i); 026 } 027 028 public String getName() { 029 return name; 030 } 031 032 public String getDescription() { 033 return description; 034 } 035 036 public void print() { 037 System.out.print("\n" + getName()); 038 System.out.println(", " + getDescription()); 039 System.out.println("---------------------"); 040 041 @SuppressWarnings("rawtypes") 042 Iterator iterator = menuComponents.iterator(); 043 while (iterator.hasNext()) { 044 MenuComponent menuComponent = 045 (MenuComponent)iterator.next(); 046 menuComponent.print(); 047 } 048 } 049}