Ejemplo n.º 1
0
        public void Run()
        {
            string input = string.Empty;

            while ((input = Console.ReadLine()) != "End")
            {
                string[] animalInput = input
                                       .Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();

                IAnimal newAnimal = AnimalCreator.CreateAnimal(animalInput);

                string[] foodInput = Console.ReadLine()
                                     .Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();

                newAnimal.Eat(foodInput[0], int.Parse(foodInput[1]));

                listOfAnimals.Add(newAnimal);
            }

            Printing();
        }
Ejemplo n.º 2
0
        static void Main()
        {
            List <Animal> animals = new List <Animal>();
            string        command = Console.ReadLine();

            while (command != "End")
            {
                Animal newAnimal = AnimalCreator.CreateAnimal(command);

                try
                {
                    Food newFood = FoodCreator.CreateFood(Console.ReadLine());
                    newAnimal.AskForFood();
                    if (newAnimal.AcceptedFoods.Contains(newFood.GetType().Name))
                    {
                        newAnimal.FoodEaten += newFood.Quantity;
                        newAnimal.Weight    += newAnimal.WeightIncrease * newFood.Quantity;
                    }
                    else
                    {
                        throw new ArgumentException($"{newAnimal.GetType().Name} does not eat {newFood.GetType().Name}!");
                    }
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }

                animals.Add(newAnimal);
                command = Console.ReadLine();
            }

            foreach (Animal animal in animals)
            {
                Console.WriteLine(animal.ToString());
            }
        }