Advertisement
LoganBlackisle

PancakeHouseMenuIterator

Jun 14th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. package prep_29_iteratorpattern;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class PancakeHouseMenuIterator implements Iterator {
  6. private ArrayList items;
  7. private int position = 0; // current position of the iteration over the array
  8.  
  9. public PancakeHouseMenuIterator(ArrayList menuItems) {
  10. this.items = menuItems; // the items we're going to iterate over
  11. }
  12.  
  13. public Object next() { // returns next item in array, increments position
  14. MenuItem menuItem = items<position>();
  15. position = position + 1;
  16. return menuItem;
  17. }
  18.  
  19. public boolean hasNext() {
  20. if (position >= items.size() || items<position>() == null) {
  21. return false;
  22. } else {
  23. return true; // return true if there are more elements we haven't looked at
  24. }
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement