Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. class CheckList<T>
  2. {
  3.     public delegate T Getter(List<T> list, int index);
  4.     public delegate void Setter(List<T> list, int index, T value);
  5.    
  6.     private List<T> list;
  7.     private Getter getter;
  8.     private Setter setter;
  9.    
  10.     public CheckList(List<T> list, Getter getter, Setter setter)
  11.     {
  12.         this.list = list;
  13.         this.getter = getter;
  14.         this.setter = setter;
  15.     }
  16.    
  17.     public T this[int index]
  18.     {
  19.         get
  20.         {
  21.             return getter(list, index);
  22.         }
  23.         set
  24.         {
  25.             setter(list, index, value);
  26.         }
  27.     }
  28.    
  29.     public int Count
  30.     {
  31.         get
  32.         { return list.Count; }
  33.     }
  34.    
  35.     public void Add(T value)
  36.     {
  37.         list.Add(value);
  38.     }
  39.    
  40.     public void Remove(T value)
  41.     {
  42.         list.Remove(value);
  43.     }
  44. }
  45.  
  46. enum EntryType
  47. {
  48.     Root,
  49.     Local,
  50.     House,
  51.     Category,
  52.     Product
  53. }
  54.  
  55. class Entry
  56. {
  57.     private string name;
  58.     private string country;
  59.     private EntryType type;
  60.     private Entry parent;
  61.     private List<Entry> children;
  62.    
  63.     public CheckList<Entry> Children;
  64.    
  65.     private Entry getChild(List<Entry> list, int index)
  66.     {
  67.         return list[index];
  68.     }
  69.    
  70.     private void setChild(List<Entry> list, int index, Entry value)
  71.     {
  72.         if (value.Type == EntryType.Root) // Класс CheckList существует для этого условия
  73.         {
  74.             throw new ArgumentException();
  75.         }
  76.         list[index] = value;
  77.     }
  78.    
  79.     public Entry()
  80.     {
  81.         children = new List<Entry>();
  82.         Children = new CheckList<Entry>(children, getChild, setChild);
  83.     }
  84.    
  85.     public string Name
  86.     {
  87.         get { return name; }
  88.         set
  89.         {
  90.             if (value.Length == 0)
  91.                 throw new ArgumentException("некорректное название");
  92.            
  93.             name = value;
  94.         }
  95.     }
  96.    
  97.     public string Country
  98.     {
  99.         get { return country; }
  100.         set
  101.         {
  102.             if (value.Length == 0)
  103.                 throw new ArgumentException("некорректное название страны");
  104.            
  105.             country = value;
  106.         }
  107.     }
  108.    
  109.     public EntryType Type
  110.     {
  111.         get { return type; }
  112.         set { type = value; }
  113.     }
  114.    
  115.     public Entry Parent
  116.     {
  117.         get
  118.         { return parent; }
  119.         set
  120.         {
  121.             if (type == EntryType.Root
  122.                 || type == EntryType.Local && value.Type != EntryType.Root
  123.                 || type == EntryType.House && value.Type != EntryType.Local
  124.                 || type == EntryType.Category && value.Type != EntryType.House && value.Type != EntryType.Category
  125.                 || type == EntryType.Product && value.Type != EntryType.Category)
  126.                 throw new ArgumentException("некорректный родитель");
  127.            
  128.             parent = value;
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement