-
Notifications
You must be signed in to change notification settings - Fork 0
first draft iterator #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KKSmith125
wants to merge
1
commit into
main
Choose a base branch
from
IteratorPatternAssignment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,23 @@ | ||
| package assignments.iterator_pattern; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| //Concrete Iterable Menu w/ ArrayList | ||
| public class BreakfastMenu implements Menu { | ||
| private List<MenuItem> items; | ||
|
|
||
| public BreakfastMenu(List<MenuItem> items) { | ||
| this.items = items; | ||
| } | ||
|
|
||
| @Override | ||
| public void addItem(String name, String description, boolean vegetarian, double price) { | ||
| MenuItem item = new MenuItem(name, description, vegetarian, price); | ||
| items.add(item); | ||
| } | ||
|
|
||
| @Override | ||
| public BreakfastMenuIterator createIterator() { | ||
| return new BreakfastMenuIterator(items); | ||
| } | ||
| } |
This file contains hidden or 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,28 @@ | ||
| package assignments.iterator_pattern; | ||
|
|
||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
|
|
||
| //Concrete Iterator for Breakfast | ||
| public class BreakfastMenuIterator implements MyIterator { | ||
| private int currentIndex = 0; | ||
| private List<MenuItem> menuItems; | ||
|
|
||
| public BreakfastMenuIterator(List<MenuItem> menuItems) { | ||
| this.menuItems = menuItems; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| return currentIndex < menuItems.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public MenuItem next() { | ||
| if (!hasNext()) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
|
|
||
| return menuItems.get(currentIndex++); | ||
| } | ||
| } |
This file contains hidden or 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,30 @@ | ||
| package assignments.iterator_pattern; | ||
|
|
||
| //Concrete Iterable w/ Array | ||
| public class DinnerMenu implements Menu { | ||
| private int capacity = 0; | ||
| private MenuItem[] items = new MenuItem[capacity]; | ||
|
|
||
| public DinnerMenu(MenuItem[] items) { | ||
| this.items = items; | ||
| } | ||
|
|
||
| @Override | ||
| public void addItem(String name, String description, boolean vegetarian, double price) { | ||
| MenuItem item = new MenuItem(name, description, vegetarian, price); | ||
| MenuItem[] increase = new MenuItem[capacity + 1]; | ||
|
|
||
| for (int i = 0; i < increase.length; i++) { | ||
| if (i != increase.length) { | ||
| increase[i] = items[i]; | ||
| } else { | ||
| increase[i] = item; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public DinnerMenuIterator createIterator() { | ||
| return new DinnerMenuIterator(items); | ||
| } | ||
| } |
This file contains hidden or 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,31 @@ | ||
| package assignments.iterator_pattern; | ||
|
|
||
| import java.util.NoSuchElementException; | ||
|
|
||
| //Concrete Iterator for Dinner | ||
| public class DinnerMenuIterator implements MyIterator { | ||
| private int currentIndex = 0; | ||
| private MenuItem[] menuItems; | ||
|
|
||
| public DinnerMenuIterator(MenuItem[] menuItems) { | ||
| this.menuItems = menuItems; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| return currentIndex < menuItems.length; | ||
| } | ||
|
|
||
| @Override | ||
| public MenuItem next() { | ||
| if (!hasNext()) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
|
|
||
| MenuItem returnItem = menuItems[currentIndex]; | ||
|
|
||
| currentIndex += 1; | ||
|
|
||
| return returnItem; | ||
| } | ||
| } | ||
This file contains hidden or 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,8 @@ | ||
| package assignments.iterator_pattern; | ||
|
|
||
| //Menu Interface for Iterable Menus | ||
| public interface Menu { | ||
| public void addItem(String name, String description, boolean vegetarian, double price); | ||
|
|
||
| public MyIterator createIterator(); | ||
| } |
This file contains hidden or 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,32 @@ | ||
| package assignments.iterator_pattern; | ||
|
|
||
| //Class for menu item | ||
| public class MenuItem { | ||
| String name; | ||
| String description; | ||
| boolean vegetarian; | ||
| 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 double getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| public boolean isVegertarian() { | ||
| return vegetarian; | ||
| } | ||
| } |
This file contains hidden or 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,24 @@ | ||
| package assignments.iterator_pattern; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| //Main | ||
| public class MenuTest { | ||
| public static void main(String[] args) { | ||
| List<MenuItem> breakfastList = new ArrayList<>(); | ||
|
|
||
| breakfastList.add(new MenuItem("Pancakes", "Sweet Circle Bread", false, 10)); | ||
| breakfastList.add(new MenuItem("Eggs", "Non-Meat Protein", false, 8)); | ||
|
|
||
| Menu dinnerMenu = new DinnerMenu(new MenuItem[] { | ||
| new MenuItem("Sushi", "Fish", false, 10), | ||
| new MenuItem("Chicken Tenders", "Battered Chicken", false, 10) | ||
| }); | ||
|
|
||
| Menu breakfastMenu = new BreakfastMenu(breakfastList); | ||
| Waiter waiter = new Waiter(breakfastMenu, dinnerMenu); // Client | ||
|
|
||
| waiter.printMenu(); | ||
| } | ||
| } |
This file contains hidden or 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,8 @@ | ||
| package assignments.iterator_pattern; | ||
|
|
||
| //Iterator Interface | ||
| public interface MyIterator { | ||
| public boolean hasNext(); | ||
|
|
||
| public MenuItem next(); | ||
| } |
This file contains hidden or 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,34 @@ | ||
| package assignments.iterator_pattern; | ||
|
|
||
| // The Client | ||
| public class Waiter { | ||
| private Menu breakfastMenu; | ||
| private Menu dinnerMenu; | ||
|
|
||
| public Waiter(Menu breakfastMenu, Menu dinnerMenu) { | ||
| this.breakfastMenu = breakfastMenu; | ||
| this.dinnerMenu = dinnerMenu; | ||
| } | ||
|
|
||
| public void printMenu() { | ||
| System.out.println("------ Breakfast ------"); | ||
| printMenu(breakfastMenu.createIterator()); | ||
| System.out.println("------ Dinner ------"); | ||
| printMenu(dinnerMenu.createIterator()); | ||
| } | ||
|
|
||
| public void printMenu(MyIterator myIterator) { | ||
| while (myIterator.hasNext()) { | ||
| MenuItem item = myIterator.next(); | ||
| System.out.println("Item: " + item.getName()); | ||
| System.out.println("Description: " + item.getDescription()); | ||
| System.out.println("Price: $" + item.getPrice()); | ||
| if (item.isVegertarian() == true) { | ||
| System.out.println("Vegetarian: Yes"); | ||
| } else { | ||
| System.out.println("Vegetarian: No"); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AM-1(F): What if we have a menu length of 0?