コード例 #1
0
 public override void Add(ZooUnit unit)
 {
     if (unit is Animal && (unit as Animal).IsInZoo) throw new CannotAddUnitException("Это животное уже в другой клетке!");
     if (unit is Predator)
     {
         if (this.Units.Count == 0)
         {
             (unit as Animal).IsInZoo = true;
             Units.Add(unit);
         }
         else
         {
             List<ZooUnit> zu = new List<ZooUnit>();
             foreach (var item in this.Units)
                 if (item is Predator)
                     zu.Add(item);
             if (zu.Count != 0 && zu[0].GetType() == unit.GetType())
             {
                 (unit as Animal).IsInZoo = true;
                 Units.Add(unit);
             }
             else if(zu.Count !=0) throw new CannotAddUnitException("Тут уже животные другого вида!");
         }
     }
     else if (unit is Animal) throw new CannotAddUnitException("Нельзя добавить травоядное!");
     if (unit is CageForBears || unit is CageForWolves)
         Units.Add(unit);
     else if (!(unit is Animal)) throw new CannotAddUnitException("Нельзя добавить клетку для травоядных!");
     
 }
コード例 #2
0
 public override void Add(ZooUnit unit)
 {
     if (unit is Animal && (unit as Animal).IsInZoo)
     {
         throw new CannotAddUnitException("Это животное уже в другой клетке!");
     }
     if (unit is Predator)
     {
         if (this.Units.Count == 0)
         {
             (unit as Animal).IsInZoo = true;
             Units.Add(unit);
         }
         else
         {
             List <ZooUnit> zu = new List <ZooUnit>();
             foreach (var item in this.Units)
             {
                 if (item is Predator)
                 {
                     zu.Add(item);
                 }
             }
             if (zu.Count != 0 && zu[0].GetType() == unit.GetType())
             {
                 (unit as Animal).IsInZoo = true;
                 Units.Add(unit);
             }
         }
     }
     if (unit is CageForBears || unit is CageForWolves)
     {
         Units.Add(unit);
     }
 }
コード例 #3
0
 public override void Add(ZooUnit unit)
 {
     if (unit is Animal && (unit as Animal).IsInZoo)
     {
         throw new CannotAddUnitException("Это животное уже в другой клетке!");
     }
     if (unit is Herbivorous)
     {
         if (this.Units.Count == 0)
         {
             Units.Add(unit);
             (unit as Animal).IsInZoo = true;
         }
         else
         {
             List <ZooUnit> zu = new List <ZooUnit>();
             foreach (var item in this.Units)
             {
                 if (item is Herbivorous)
                 {
                     zu.Add(item);
                 }
             }
             if (zu.Count != 0 && zu[0].GetType() == unit.GetType())
             {
                 Units.Add(unit);
                 (unit as Animal).IsInZoo = true;
             }
         }
     }
     else if (unit is Animal)
     {
         throw new CannotAddUnitException("Нельзя добавить хищника!");
     }
     if (unit is CageForGiraffes)
     {
         Units.Add(unit);
     }
     else if (!(unit is Animal))
     {
         throw new CannotAddUnitException("Нельзя добавить клетку для хищников!");
     }
 }