-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathWaitress.java
More file actions
36 lines (30 loc) · 1.18 KB
/
Waitress.java
File metadata and controls
36 lines (30 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package MenuCompositeWithIterator;
import java.util.Iterator;
public class Waitress {
MenuComponent allMenus;
public Waitress(MenuComponent allMenus) {
this.allMenus = allMenus;
}
// print the entire menu hierarchy
public void printMenu() {
allMenus.print();
}
// takes the allMenu's composite and gets its iterator
public void printVegetarianMenu() {
Iterator iterator = allMenus.createIterator();
System.out.println("\nVEGETARIAN MENU\n----");
// iterate through every element of the composite
while (iterator.hasNext()) {
MenuComponent menuComponent = (MenuComponent)iterator.next();
try {
// call each element's isVegetarian() and
// if is true we call its print() method
if (menuComponent.isVegetarian()) {
menuComponent.print();
}
// we implemented isVegetarian on the Menus to always throw an exception
// if that happens we catch the exception, but continue with our iteraction
} catch (UnsupportedOperationException e) {}
}
}
}