Implement Iterator Design Pattern Without Custom Iterator Interface
parent
982a301cd3
commit
ffa4bcbdc3
@ -0,0 +1,46 @@
|
|||||||
|
package iterator.without.custom.iterator;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class DinerMenu {
|
||||||
|
private static final int MAX_ITEMS = 6;
|
||||||
|
private int numberOfItems = 0;
|
||||||
|
private final MenuItem[] menuItems = new MenuItem[MAX_ITEMS];
|
||||||
|
|
||||||
|
public DinerMenu() {
|
||||||
|
|
||||||
|
addItem("Vegetarian BLT",
|
||||||
|
"(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99);
|
||||||
|
addItem("BLT",
|
||||||
|
"Bacon with lettuce & tomato on whole wheat", false, 2.99);
|
||||||
|
addItem("Soup of the day",
|
||||||
|
"Soup of the day, with a side of potato salad", false, 3.29);
|
||||||
|
addItem("Hotdog",
|
||||||
|
"A hot dog, with saurkraut, relish, onions, topped with cheese",
|
||||||
|
false, 3.05);
|
||||||
|
addItem("Steamed Veggies and Brown Rice",
|
||||||
|
"Steamed vegetables over brown rice", true, 3.99);
|
||||||
|
addItem("Pasta",
|
||||||
|
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
|
||||||
|
true, 3.89);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addItem(String name, String description, boolean vegetarian, double price) {
|
||||||
|
if (numberOfItems < MAX_ITEMS) {
|
||||||
|
this.menuItems[numberOfItems++] = new MenuItem(name, description, vegetarian, price);
|
||||||
|
} else {
|
||||||
|
System.out.println("Sorry, menu is full! Can't add item to menu");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Note: Using iterator to get rid of this code
|
||||||
|
public MenuItem[] getMenuItems() {
|
||||||
|
return menuItems;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
public Iterator<MenuItem> createIterator() {
|
||||||
|
return new DinerMenuIterator(menuItems);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package iterator.without.custom.iterator;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class DinerMenuIterator implements Iterator<MenuItem> {
|
||||||
|
private final MenuItem[] menuItems;
|
||||||
|
private int currentIndex;
|
||||||
|
|
||||||
|
public DinerMenuIterator(MenuItem[] menuItems) {
|
||||||
|
this.menuItems = menuItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return currentIndex < menuItems.length && menuItems[currentIndex] != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MenuItem next() {
|
||||||
|
if (this.hasNext()) {
|
||||||
|
return menuItems[currentIndex++];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package iterator.without.custom.iterator;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Waitress waitress = new Waitress(new PancakeHouseMenu(), new DinerMenu());
|
||||||
|
waitress.printMenu();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package iterator.without.custom.iterator;
|
||||||
|
|
||||||
|
public class MenuItem {
|
||||||
|
private final String name;
|
||||||
|
private final String description;
|
||||||
|
private final boolean vegetarian;
|
||||||
|
private final double price;
|
||||||
|
|
||||||
|
public MenuItem(String name, String description, boolean vegetarian, double price) {
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
this.vegetarian = vegetarian;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isVegetarian() {
|
||||||
|
return vegetarian;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package iterator.without.custom.iterator;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PancakeHouseMenu {
|
||||||
|
private final List<MenuItem> menuItems = new ArrayList<>();
|
||||||
|
|
||||||
|
public PancakeHouseMenu() {
|
||||||
|
|
||||||
|
addItem("K&B's Pancake Breakfast",
|
||||||
|
"Pancakes with scrambled eggs, and toast",
|
||||||
|
true,
|
||||||
|
2.99);
|
||||||
|
|
||||||
|
addItem("Regular Pancake Breakfast",
|
||||||
|
"Pancakes with fried eggs, sausage",
|
||||||
|
false,
|
||||||
|
2.99);
|
||||||
|
|
||||||
|
addItem("Blueberry Pancakes",
|
||||||
|
"Pancakes made with fresh blueberries",
|
||||||
|
true,
|
||||||
|
3.49);
|
||||||
|
|
||||||
|
addItem("Waffles",
|
||||||
|
"Waffles, with your choice of blueberries or strawberries",
|
||||||
|
true,
|
||||||
|
3.59);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addItem(String name, String description, boolean vegetarian, double price) {
|
||||||
|
menuItems.add(new MenuItem(name, description, vegetarian, price));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<MenuItem> getMenuItems() {
|
||||||
|
return menuItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterator<MenuItem> createIterator() {
|
||||||
|
return new PancakeHouseMenuIterator(menuItems);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package iterator.without.custom.iterator;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PancakeHouseMenuIterator implements Iterator<MenuItem> {
|
||||||
|
private final List<MenuItem> menuItems;
|
||||||
|
private int currentIndex;
|
||||||
|
|
||||||
|
public PancakeHouseMenuIterator(List<MenuItem> menuItems) {
|
||||||
|
this.menuItems = menuItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return currentIndex < menuItems.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MenuItem next() {
|
||||||
|
if (this.hasNext()) {
|
||||||
|
return menuItems.get(currentIndex++);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package iterator.without.custom.iterator;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class Waitress {
|
||||||
|
private final PancakeHouseMenu pancakeHouseMenu;
|
||||||
|
private final DinerMenu dinerMenu;
|
||||||
|
|
||||||
|
public Waitress(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) {
|
||||||
|
this.pancakeHouseMenu = pancakeHouseMenu;
|
||||||
|
this.dinerMenu = dinerMenu;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printMenu() {
|
||||||
|
Iterator<MenuItem> pancakeIterator = pancakeHouseMenu.createIterator();
|
||||||
|
Iterator<MenuItem> dinerIterator = dinerMenu.createIterator();
|
||||||
|
|
||||||
|
System.out.println("MENU\n----\n\n||--BREAKFAST--||");
|
||||||
|
printMenu(pancakeIterator);
|
||||||
|
|
||||||
|
System.out.println("\n||--LUNCH--||");
|
||||||
|
printMenu(dinerIterator);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void printMenu(Iterator<MenuItem> iterator) {
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
MenuItem menuItem = iterator.next();
|
||||||
|
|
||||||
|
System.out.println(menuItem.getName() + ", ");
|
||||||
|
System.out.println(menuItem.getPrice() + " -- ");
|
||||||
|
System.out.println(menuItem.getDescription());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue