예제 #1
0
        public void Run()
        {
            var animals = new List <Animal>();

            var animalInfo = Console.ReadLine().Split();

            while (animalInfo[0] != "End")
            {
                Animal animal = AnimalFactory.CreateAnimal(animalInfo);

                animals.Add(animal);

                var foodInfo = Console.ReadLine().Split();
                var type     = foodInfo[0];
                var quantity = int.Parse(foodInfo[1]);

                Food food = FoodFactory.CreateFood(type, quantity);

                try
                {
                    animal.ProduceSound();
                    animal.Eat(food);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                animalInfo = Console.ReadLine().Split();
            }

            foreach (var item in animals)
            {
                Console.WriteLine(item);
            }
        }
        public static void Main()
        {
            List <Animal> animals = new List <Animal>();

            string line = Console.ReadLine();

            while (line != "End")
            {
                string[] animalInfo = line.Split();
                Animal   animal     = AnimalFactory.CreateAnimal(animalInfo);

                Console.WriteLine(animal.ProduceSound());

                string[] foodInfo = Console.ReadLine().Split();
                Food     food     = FoodFactory.CreateFood(foodInfo);

                try
                {
                    animal.Eat(food, 1);
                }
                catch (InvalidFoodException invFoodEx)
                {
                    Console.WriteLine(invFoodEx.Message);
                }

                animals.Add(animal);

                line = Console.ReadLine();
            }

            animals.ForEach(a => Console.WriteLine(a));
        }
예제 #3
0
        private static void Main()
        {
            List <Animal> animals = new List <Animal>();

            string line = Console.ReadLine();

            while (line != "End")
            {
                string[] animalsParams = line.Split();
                string[] foodParams    = Console.ReadLine().Split();

                Animal animal = AnimalFactory.CreateAnimal(animalsParams);
                Food   food   = FoodFactory.CreateFood(foodParams);

                Console.WriteLine(animal.ProduceSound());

                try
                {
                    animal.Eat(food);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                animals.Add(animal);

                line = Console.ReadLine();
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
예제 #4
0
        static void Main()
        {
            var    animalsFactory = new AnimalFactory();
            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                var    tokens        = input.Split();
                Animal currentAnimal = null;
                currentAnimal = animalsFactory.CreateAnimal(tokens);
                if (currentAnimal != null)
                {
                    list.Add(currentAnimal);
                    Console.WriteLine(currentAnimal.MakeSound());
                    var  foodTokens  = Console.ReadLine().Split();
                    Food currentFood = null;
                    switch (foodTokens[0])
                    {
                    case "Vegetable":
                        currentFood = new Vegetable(int.Parse(foodTokens[1]));
                        break;

                    case "Fruit":
                        currentFood = new Fruit(int.Parse(foodTokens[1]));
                        break;

                    case "Meat":
                        currentFood = new Meat(int.Parse(foodTokens[1]));
                        break;

                    case "Seeds":
                        currentFood = new Seeds(int.Parse(foodTokens[1]));
                        break;
                    }
                    try
                    {
                        currentAnimal.Feed(currentFood);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
            Console.WriteLine(string.Join(Environment.NewLine, list));
        }
예제 #5
0
        public static void Main()
        {
            List <Animal> animals = new List <Animal>();

            while (true)
            {
                string[] info = Console.ReadLine().Split();

                if (info[0] == "End")
                {
                    break;
                }

                Animal animal = AnimalFactory.CreateAnimal(info);

                info = Console.ReadLine().Split();
                Food food = FoodFactory.CreateFood(info);

                Console.WriteLine(animal.MakeSound());

                try
                {
                    animal.Feed(food);
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }

                animals.Add(animal);
            }

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