Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace Iteratorsl
  6. {
  7. public class ListyIterator<T>
  8. {
  9. private List<T> list;
  10. private int index = 0;
  11. public ListyIterator()
  12. {
  13. this.list = new List<T>();
  14. this.index = 0;
  15. }
  16.  
  17. public void Create(List<T> elements)
  18. {
  19. this.list = new List<T>();
  20. this.list.AddRange(elements);
  21. index = 0;
  22. }
  23.  
  24. public bool hasNext()
  25. {
  26. if (this.list.Count > this.index + 1)
  27. {
  28. return true;
  29. }
  30. return false;
  31. }
  32.  
  33.  
  34. public bool Move()
  35. {
  36. if (this.index + 1 < this.list.Count)
  37. {
  38. this.index++;
  39. return true;
  40. }
  41. return false;
  42. }
  43.  
  44. public void Print()
  45. {
  46. try
  47. {
  48. Console.WriteLine(this.list[this.index]);
  49. }
  50. catch (ArgumentException)
  51. {
  52. throw new ArgumentException("Invalid Operation!");
  53. }
  54. }
  55.  
  56.  
  57. }
  58. class Program
  59. {
  60. static void Main(string[] args)
  61. {
  62. string line = Console.ReadLine();
  63. var list = new ListyIterator<string>();
  64. while (line!="END")
  65. {
  66. var firstLine = Regex.Split(line, "\\s+");
  67. string command = firstLine[0].Trim();
  68. switch (command)
  69. {
  70. case "Create":
  71. var collection = new List<string>();
  72. for (int i = 1; i < firstLine.Length; i++)
  73. {
  74. collection.Add(firstLine[i]);
  75. }
  76. list.Create(collection);
  77. break;
  78. case "Move":
  79. Console.WriteLine(list.Move()); ; break;
  80. case "HasNext":
  81. Console.WriteLine(list.hasNext()); ; break;
  82. case "Print":
  83. try
  84. {
  85. list.Print();
  86. }
  87. catch (Exception e)
  88. {
  89. Console.WriteLine(e.Message);
  90. };break;
  91.  
  92. default:
  93. break;
  94. }
  95. line=Console.ReadLine();
  96. }
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement