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