Advertisement
MatthijsFontys

More iterator examples

Jan 21st, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. public class MyDiner{
  2.  
  3. private string[] _menuitems;
  4. private int _position;
  5. private IEnumerable<string>[] _subMenus;
  6.  
  7. private int _innerPosition;
  8. private IEnumerable _currentIterator;
  9.  
  10. public MyDiner(ICollection<string> subMenus) : IEnumerable{
  11. _position = 0;
  12. _innerPosition = 0;
  13. _menuItems = new string[] {"Coffee", "Latte machiato", "Cappuchino"};
  14. _submenus = submenus
  15. _currentIterator = _submenus[_innerPosition].Iterator();
  16. }
  17.  
  18.  
  19. public bool HasNext(){
  20. return _innerPosition == _submenus.length - 1 || _currentIterator.HasNext();
  21. }
  22.  
  23.  
  24. public string Next(){
  25. if(_position < _menuitems.length){
  26. _position ++;
  27. return = __menuItems[_position - 1];
  28. }
  29. else if(_currentIterator.HasNext){
  30. return _currentIterator.Next();
  31. }
  32. else if (_innerPosition < _submenus.length -1){
  33. _innerPosition ++;
  34. _currentIterator = _submenus[_innerPosition].Iterator();
  35. return _currentIterator.Next();
  36. }
  37. else {
  38. thow new IllegalOperationException("I'm out of elements!");
  39. }
  40. }
  41.  
  42.  
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement