From ffa4bcbdc35427789ca4a6dbb15cbb1ac5a9d8db Mon Sep 17 00:00:00 2001 From: Hammy Date: Tue, 22 Dec 2020 07:23:04 +0000 Subject: [PATCH] Implement Iterator Design Pattern Without Custom Iterator Interface --- .../without/custom/iterator/DinerMenu.java | 46 +++++++++++++++++++ .../custom/iterator/DinerMenuIterator.java | 25 ++++++++++ .../without/custom/iterator/Main.java | 8 ++++ .../without/custom/iterator/MenuItem.java | 31 +++++++++++++ .../custom/iterator/PancakeHouseMenu.java | 44 ++++++++++++++++++ .../iterator/PancakeHouseMenuIterator.java | 26 +++++++++++ .../without/custom/iterator/Waitress.java | 34 ++++++++++++++ 7 files changed, 214 insertions(+) create mode 100644 Design Patterns/src/iterator/without/custom/iterator/DinerMenu.java create mode 100644 Design Patterns/src/iterator/without/custom/iterator/DinerMenuIterator.java create mode 100644 Design Patterns/src/iterator/without/custom/iterator/Main.java create mode 100644 Design Patterns/src/iterator/without/custom/iterator/MenuItem.java create mode 100644 Design Patterns/src/iterator/without/custom/iterator/PancakeHouseMenu.java create mode 100644 Design Patterns/src/iterator/without/custom/iterator/PancakeHouseMenuIterator.java create mode 100644 Design Patterns/src/iterator/without/custom/iterator/Waitress.java diff --git a/Design Patterns/src/iterator/without/custom/iterator/DinerMenu.java b/Design Patterns/src/iterator/without/custom/iterator/DinerMenu.java new file mode 100644 index 0000000..25c0571 --- /dev/null +++ b/Design Patterns/src/iterator/without/custom/iterator/DinerMenu.java @@ -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 createIterator() { + return new DinerMenuIterator(menuItems); + } +} diff --git a/Design Patterns/src/iterator/without/custom/iterator/DinerMenuIterator.java b/Design Patterns/src/iterator/without/custom/iterator/DinerMenuIterator.java new file mode 100644 index 0000000..0311482 --- /dev/null +++ b/Design Patterns/src/iterator/without/custom/iterator/DinerMenuIterator.java @@ -0,0 +1,25 @@ +package iterator.without.custom.iterator; + +import java.util.Iterator; + +public class DinerMenuIterator implements Iterator { + 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; + } +} diff --git a/Design Patterns/src/iterator/without/custom/iterator/Main.java b/Design Patterns/src/iterator/without/custom/iterator/Main.java new file mode 100644 index 0000000..add500c --- /dev/null +++ b/Design Patterns/src/iterator/without/custom/iterator/Main.java @@ -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(); + } +} diff --git a/Design Patterns/src/iterator/without/custom/iterator/MenuItem.java b/Design Patterns/src/iterator/without/custom/iterator/MenuItem.java new file mode 100644 index 0000000..29feed4 --- /dev/null +++ b/Design Patterns/src/iterator/without/custom/iterator/MenuItem.java @@ -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; + } +} diff --git a/Design Patterns/src/iterator/without/custom/iterator/PancakeHouseMenu.java b/Design Patterns/src/iterator/without/custom/iterator/PancakeHouseMenu.java new file mode 100644 index 0000000..e45b396 --- /dev/null +++ b/Design Patterns/src/iterator/without/custom/iterator/PancakeHouseMenu.java @@ -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 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 getMenuItems() { + return menuItems; + } + + public Iterator createIterator() { + return new PancakeHouseMenuIterator(menuItems); + } +} diff --git a/Design Patterns/src/iterator/without/custom/iterator/PancakeHouseMenuIterator.java b/Design Patterns/src/iterator/without/custom/iterator/PancakeHouseMenuIterator.java new file mode 100644 index 0000000..7d0ca1a --- /dev/null +++ b/Design Patterns/src/iterator/without/custom/iterator/PancakeHouseMenuIterator.java @@ -0,0 +1,26 @@ +package iterator.without.custom.iterator; + +import java.util.Iterator; +import java.util.List; + +public class PancakeHouseMenuIterator implements Iterator { + private final List menuItems; + private int currentIndex; + + public PancakeHouseMenuIterator(List 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; + } +} diff --git a/Design Patterns/src/iterator/without/custom/iterator/Waitress.java b/Design Patterns/src/iterator/without/custom/iterator/Waitress.java new file mode 100644 index 0000000..38d4c37 --- /dev/null +++ b/Design Patterns/src/iterator/without/custom/iterator/Waitress.java @@ -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 pancakeIterator = pancakeHouseMenu.createIterator(); + Iterator dinerIterator = dinerMenu.createIterator(); + + System.out.println("MENU\n----\n\n||--BREAKFAST--||"); + printMenu(pancakeIterator); + + System.out.println("\n||--LUNCH--||"); + printMenu(dinerIterator); + } + + private void printMenu(Iterator iterator) { + while (iterator.hasNext()) { + MenuItem menuItem = iterator.next(); + + System.out.println(menuItem.getName() + ", "); + System.out.println(menuItem.getPrice() + " -- "); + System.out.println(menuItem.getDescription()); + } + } +}