Skip to content

Commit

Permalink
Fixing Waitress (Menu) and adding code for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bethrobson committed Jul 5, 2016
1 parent 467ca08 commit 3ae3397
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 2 deletions.
51 changes: 51 additions & 0 deletions src/headfirst/designpatterns/iterator/transition/DinerMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package headfirst.designpatterns.iterator.transition;

import java.util.Iterator;

public class DinerMenu implements Menu {
static final int MAX_ITEMS = 6;
int numberOfItems = 0;
MenuItem[] menuItems;

public DinerMenu() {
menuItems = new MenuItem[MAX_ITEMS];

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)
{
MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
if (numberOfItems >= MAX_ITEMS) {
System.err.println("Sorry, menu is full! Can't add item to menu");
} else {
menuItems[numberOfItems] = menuItem;
numberOfItems = numberOfItems + 1;
}
}

public MenuItem[] getMenuItems() {
return menuItems;
}

public Iterator<MenuItem> createIterator() {
return new DinerMenuIterator(menuItems);
//return new AlternatingDinerMenuIterator(menuItems);
}

// other menu methods here
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package headfirst.designpatterns.iterator.transition;

import java.util.Iterator;

public class DinerMenuIterator implements Iterator<MenuItem> {
MenuItem[] list;
int position = 0;

public DinerMenuIterator(MenuItem[] list) {
this.list = list;
}

public MenuItem next() {
MenuItem menuItem = list[position];
position = position + 1;
return menuItem;
}

public boolean hasNext() {
if (position >= list.length || list[position] == null) {
return false;
} else {
return true;
}
}

public void remove() {
if (position <= 0) {
throw new IllegalStateException
("You can't remove an item until you've done at least one next()");
}
if (list[position-1] != null) {
for (int i = position-1; i < (list.length-1); i++) {
list[i] = list[i+1];
}
list[list.length-1] = null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package headfirst.designpatterns.iterator.transition;
import java.util.ArrayList;

public class MenuTestDrive {
public static void main(String args[]) {
PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
DinerMenu dinerMenu = new DinerMenu();
ArrayList<Menu> menus = new ArrayList<Menu>();
menus.add(pancakeHouseMenu);
menus.add(dinerMenu);
Waitress waitress = new Waitress(menus);
waitress.printMenu();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package headfirst.designpatterns.iterator.transition;

import java.util.ArrayList;
import java.util.Iterator;

public class PancakeHouseMenu implements Menu {
ArrayList<MenuItem> menuItems;

public PancakeHouseMenu() {
menuItems = new ArrayList<MenuItem>();

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, and blueberry syrup",
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)
{
MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
menuItems.add(menuItem);
}

public ArrayList<MenuItem> getMenuItems() {
return menuItems;
}

public Iterator<MenuItem> createIterator() {
return menuItems.iterator();
}

// other menu methods here
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@


public class Waitress {
ArrayList<MenuItem> menus;
ArrayList<Menu> menus;


public Waitress(ArrayList<MenuItem> menus) {
public Waitress(ArrayList<Menu> menus) {
this.menus = menus;
}

Expand Down

0 comments on commit 3ae3397

Please sign in to comment.