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