-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing Waitress (Menu) and adding code for testing
- Loading branch information
1 parent
467ca08
commit 3ae3397
Showing
5 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/headfirst/designpatterns/iterator/transition/DinerMenu.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
40 changes: 40 additions & 0 deletions
40
src/headfirst/designpatterns/iterator/transition/DinerMenuIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/headfirst/designpatterns/iterator/transition/MenuTestDrive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/headfirst/designpatterns/iterator/transition/PancakeHouseMenu.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters