Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- namespace Iteratorsl
- {
- public class ListyIterator<T>
- {
- private List<T> list;
- private int index = 0;
- public ListyIterator()
- {
- this.list = new List<T>();
- this.index = 0;
- }
- public void Create(List<T> elements)
- {
- this.list = new List<T>();
- this.list.AddRange(elements);
- index = 0;
- }
- public bool hasNext()
- {
- if (this.list.Count > this.index + 1)
- {
- return true;
- }
- return false;
- }
- public bool Move()
- {
- if (this.index + 1 < this.list.Count)
- {
- this.index++;
- return true;
- }
- return false;
- }
- public void Print()
- {
- try
- {
- Console.WriteLine(this.list[this.index]);
- }
- catch (ArgumentException)
- {
- throw new ArgumentException("Invalid Operation!");
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- string line = Console.ReadLine();
- var list = new ListyIterator<string>();
- while (line!="END")
- {
- var firstLine = Regex.Split(line, "\\s+");
- string command = firstLine[0].Trim();
- switch (command)
- {
- case "Create":
- var collection = new List<string>();
- for (int i = 1; i < firstLine.Length; i++)
- {
- collection.Add(firstLine[i]);
- }
- list.Create(collection);
- break;
- case "Move":
- Console.WriteLine(list.Move()); ; break;
- case "HasNext":
- Console.WriteLine(list.hasNext()); ; break;
- case "Print":
- try
- {
- list.Print();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- };break;
- default:
- break;
- }
- line=Console.ReadLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement