001package headfirst.iterator.dinermergercafe;
002
003import java.util.Iterator;
004
005@SuppressWarnings("rawtypes")
006public class DinerMenuIterator implements Iterator {
007        MenuItem[] list;
008        int position = 0;
009
010        public DinerMenuIterator(MenuItem[] list) {
011                this.list = list;
012        }
013
014        public Object next() {
015                MenuItem menuItem = list[position];
016                position = position + 1;
017                return menuItem;
018        }
019
020        public boolean hasNext() {
021                if (position >= list.length || list[position] == null) {
022                        return false;
023                } else {
024                        return true;
025                }
026        }
027
028        public void remove() {
029                if (position <= 0) {
030                        throw new IllegalStateException
031                        ("You can't remove an item until you've done at least one next()");
032                }
033                if (list[position-1] != null) {
034                        for (int i = position-1; i < (list.length-1); i++) {
035                                list[i] = list[i+1];
036                        }
037                        list[list.length-1] = null;
038                }
039        }
040}