From 982a301cd3a15fa62e76ed5ccdd9117cb9415968 Mon Sep 17 00:00:00 2001 From: Hammy Date: Tue, 22 Dec 2020 07:22:29 +0000 Subject: [PATCH] Implement Iterator Design Pattern --- Design Patterns/src/iterator/DinerMenu.java | 44 +++++++++++++++++++ .../src/iterator/DinerMenuIterator.java | 23 ++++++++++ Design Patterns/src/iterator/Iterator.java | 7 +++ Design Patterns/src/iterator/Main.java | 8 ++++ Design Patterns/src/iterator/MenuItem.java | 31 +++++++++++++ .../src/iterator/PancakeHouseMenu.java | 43 ++++++++++++++++++ .../iterator/PancakeHouseMenuIterator.java | 25 +++++++++++ Design Patterns/src/iterator/Waitress.java | 32 ++++++++++++++ 8 files changed, 213 insertions(+) create mode 100644 Design Patterns/src/iterator/DinerMenu.java create mode 100644 Design Patterns/src/iterator/DinerMenuIterator.java create mode 100644 Design Patterns/src/iterator/Iterator.java create mode 100644 Design Patterns/src/iterator/Main.java create mode 100644 Design Patterns/src/iterator/MenuItem.java create mode 100644 Design Patterns/src/iterator/PancakeHouseMenu.java create mode 100644 Design Patterns/src/iterator/PancakeHouseMenuIterator.java create mode 100644 Design Patterns/src/iterator/Waitress.java diff --git a/Design Patterns/src/iterator/DinerMenu.java b/Design Patterns/src/iterator/DinerMenu.java new file mode 100644 index 0000000..f665807 --- /dev/null +++ b/Design Patterns/src/iterator/DinerMenu.java @@ -0,0 +1,44 @@ +package 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/DinerMenuIterator.java b/Design Patterns/src/iterator/DinerMenuIterator.java new file mode 100644 index 0000000..d000939 --- /dev/null +++ b/Design Patterns/src/iterator/DinerMenuIterator.java @@ -0,0 +1,23 @@ +package 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 Object next() { + if (this.hasNext()) { + return menuItems[currentIndex++]; + } + return null; + } +} diff --git a/Design Patterns/src/iterator/Iterator.java b/Design Patterns/src/iterator/Iterator.java new file mode 100644 index 0000000..d41e700 --- /dev/null +++ b/Design Patterns/src/iterator/Iterator.java @@ -0,0 +1,7 @@ +package iterator; + +public interface Iterator { + boolean hasNext(); + Object next(); +} + diff --git a/Design Patterns/src/iterator/Main.java b/Design Patterns/src/iterator/Main.java new file mode 100644 index 0000000..ecf85a2 --- /dev/null +++ b/Design Patterns/src/iterator/Main.java @@ -0,0 +1,8 @@ +package 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/MenuItem.java b/Design Patterns/src/iterator/MenuItem.java new file mode 100644 index 0000000..4f03de5 --- /dev/null +++ b/Design Patterns/src/iterator/MenuItem.java @@ -0,0 +1,31 @@ +package 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/PancakeHouseMenu.java b/Design Patterns/src/iterator/PancakeHouseMenu.java new file mode 100644 index 0000000..c8e3550 --- /dev/null +++ b/Design Patterns/src/iterator/PancakeHouseMenu.java @@ -0,0 +1,43 @@ +package iterator; + +import java.util.ArrayList; +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/PancakeHouseMenuIterator.java b/Design Patterns/src/iterator/PancakeHouseMenuIterator.java new file mode 100644 index 0000000..ea4911f --- /dev/null +++ b/Design Patterns/src/iterator/PancakeHouseMenuIterator.java @@ -0,0 +1,25 @@ +package 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 Object next() { + if (this.hasNext()) { + return menuItems.get(currentIndex++); + } + return null; + } +} diff --git a/Design Patterns/src/iterator/Waitress.java b/Design Patterns/src/iterator/Waitress.java new file mode 100644 index 0000000..6924986 --- /dev/null +++ b/Design Patterns/src/iterator/Waitress.java @@ -0,0 +1,32 @@ +package 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 = (MenuItem) iterator.next(); + + System.out.println(menuItem.getName() + ", "); + System.out.println(menuItem.getPrice() + " -- "); + System.out.println(menuItem.getDescription()); + } + } +}