001package headfirst.iterator.dinermerger;
002
003public class Waitress {
004        PancakeHouseMenu pancakeHouseMenu;
005        DinerMenu dinerMenu;
006
007        public Waitress(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) {
008                this.pancakeHouseMenu = pancakeHouseMenu;
009                this.dinerMenu = dinerMenu;
010        }
011
012        public void printMenu() {
013                Iterator pancakeIterator = pancakeHouseMenu.createIterator();
014                Iterator dinerIterator = dinerMenu.createIterator();
015
016                System.out.println("MENU\n----\nBREAKFAST");
017                printMenu(pancakeIterator);
018                System.out.println("\nLUNCH");
019                printMenu(dinerIterator);
020        }
021
022        private void printMenu(Iterator iterator) {
023                while (iterator.hasNext()) {
024                        MenuItem menuItem = (MenuItem)iterator.next();
025                        System.out.print(menuItem.getName() + ", ");
026                        System.out.print(menuItem.getPrice() + " -- ");
027                        System.out.println(menuItem.getDescription());
028                }
029        }
030
031        public void printVegetarianMenu() {
032                printVegetarianMenu(pancakeHouseMenu.createIterator());
033                printVegetarianMenu(dinerMenu.createIterator());
034        }
035
036        public boolean isItemVegetarian(String name) {
037                Iterator breakfastIterator = pancakeHouseMenu.createIterator();
038                if (isVegetarian(name, breakfastIterator)) {
039                        return true;
040                }
041                Iterator dinnerIterator = dinerMenu.createIterator();
042                if (isVegetarian(name, dinnerIterator)) {
043                        return true;
044                }
045                return false;
046        }
047
048
049        private void printVegetarianMenu(Iterator iterator) {
050                while (iterator.hasNext()) {
051                        MenuItem menuItem = (MenuItem)iterator.next();
052                        if (menuItem.isVegetarian()) {
053                                System.out.print(menuItem.getName());
054                                System.out.println("\t\t" + menuItem.getPrice());
055                                System.out.println("\t" + menuItem.getDescription());
056                        }
057                }
058        }
059
060        private boolean isVegetarian(String name, Iterator iterator) {
061                while (iterator.hasNext()) {
062                        MenuItem menuItem = (MenuItem)iterator.next();
063                        if (menuItem.getName().equals(name)) {
064                                if (menuItem.isVegetarian()) {
065                                        return true;
066                                }
067                        }
068                }
069                return false;
070        }
071}