public static void Main()
        {
            List <AnimalCs> animals = new List <AnimalCs>();

            string command = string.Empty;

            while ((command = Console.ReadLine()) != "End")
            {
                AnimalCs animal = AnimalFactory.Create(command.Split(" ", StringSplitOptions.RemoveEmptyEntries));
                animals.Add(animal);
                Console.WriteLine(animal.ProduceSound());

                FoodCs food = FoodFactory.Create(Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries));

                try
                {
                    animal.EatFood(food);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            animals.ForEach(Console.WriteLine);
        }
Exemple #2
0
        protected override void ValidateFood(FoodCs food)
        {
            string type = food.GetType().Name;

            if (type != nameof(Meat))
            {
                Throw(food);
            }
        }
        protected override void ValidateFood(FoodCs food)
        {
            string type = food.GetType().Name;

            if (type != nameof(Vegetable) && type != nameof(Fruit))
            {
                Throw(food);
            }
        }
 protected void Throw(FoodCs food)
 {
     throw new ArgumentException($"{this.GetType().Name} does not eat {food.GetType().Name}!");
 }
 protected abstract void ValidateFood(FoodCs food);
 public virtual void EatFood(FoodCs food)
 {
     ValidateFood(food);
     this.FootEaten += food.Quantity;
 }
Exemple #7
0
 protected override void ValidateFood(FoodCs food)
 {
 }