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