private int GetSmallestHerbivoor() { // Animal smallestHerbivoor = animals.Where(a => a is Herbivoor).OrderBy(a => a.GetSize()).FirstOrDefault(); // return smallestHerbivoor?.GetSize() ?? -1; Animal smallestHerbivoor = null; foreach (Animal animal in animals) { if (animal.GetType() == typeof(Herbivoor)) { if (smallestHerbivoor == null) { smallestHerbivoor = animal; } else if (animal.GetSize() < smallestHerbivoor.GetSize()) { smallestHerbivoor = animal; } } } if (smallestHerbivoor != null) { return(smallestHerbivoor.GetSize()); } return(0); }
public bool CanAddAnimal(Animal a) { if (_size < a.GetSize() || a is Carnivoor && HasCarnivoor()) { return(false); } switch (a) { case Herbivoor _ when a.GetSize() > GetLargestCarnivoor(): case Carnivoor _ when a.GetSize() < GetSmallestHerbivoor(): return(true); default: return(false); } }
public void Add(Animal a) { animals.Add(a); _size -= a.GetSize(); }