Advertisement
StreetKatya

6

Dec 12th, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1. public class Box : IEnumerable
  2.     {
  3.         private List<PrintEdition> books = new List<PrintEdition>();
  4.  
  5.         public Box()
  6.         {
  7.             books.Add(new Book("Биба", 15, 2, 3, true));
  8.             books.Add(new Book("Боба", 40, 22, 5, true));
  9.             books.Add(new Journal("Вообще не книга", 3, 20, 4, 5));
  10.             books.Add(new CopyBook("Какой-то мусор", 60, 66, 9, 6));
  11.         }
  12.         #region IEnumenator Members
  13.         public IEnumerator GetEnumerator()
  14.         {
  15.             return new BoxEnumerator(books);
  16.         }
  17.         #endregion
  18.         public void DeleteAt(int position)
  19.         {
  20.             books.Remove(books.ElementAt(position));
  21.         }
  22.         public void Add(PrintEdition item)
  23.         {
  24.             books.Add(item);
  25.         }
  26.     }
  27.     public class BoxEnumerator : IEnumerator
  28.     {
  29.         private List<PrintEdition> books;
  30.         public BoxEnumerator(List<PrintEdition> books)
  31.         {
  32.             this.books = books;
  33.         }
  34.         int position = -1;
  35.         public object Current
  36.         {
  37.             get
  38.             {
  39.                 if (position == -1 || position > books.Count)
  40.                 {
  41.                     throw new InvalidOperationException();
  42.                 }
  43.                 return books.ElementAt(position);
  44.             }
  45.         }
  46.  
  47.         public bool MoveNext()
  48.         {
  49.             if (position < books.Count - 1)
  50.             {
  51.                 position++;
  52.                 return true;
  53.             }
  54.             return false;
  55.         }
  56.  
  57.         public void Reset()
  58.         {
  59.             position = -1;
  60.         }
  61.     }
  62.     class NameComparer : IComparer<PrintEdition>
  63.     {
  64.         public int Compare(PrintEdition x, PrintEdition y)
  65.         {
  66.             PrintEdition t1 = (PrintEdition)x;
  67.             PrintEdition t2 = (PrintEdition)y;
  68.             return String.Compare(t1.name, t2.name);
  69.         }
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement