コード例 #1
0
       public static void Main()
        {
            string[] dataAnimal = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            while (dataAnimal[0] != "End")
            {
                string[] foodData = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                string animalType = dataAnimal[0].ToLower();
                string animalName = dataAnimal[1];
                double animalWeight = double.Parse(dataAnimal[2]);
                string catBreed = null;
                string livingRegion = null;

                if (dataAnimal.Length == 5)
                {
                    catBreed = dataAnimal[3];
                    livingRegion = dataAnimal[4];
                }
                else
                {
                    livingRegion = dataAnimal[3];
                }
                Animal animal = new Cat("",0.0,"","");
                switch (animalType)
                {                    
                    case "cat":
                        {
                            animal = new Cat(animalName, animalWeight, livingRegion, catBreed);
                        }
                        break;

                    case "tiger":
                        {
                            animal = new Tiger(animalName, animalWeight, livingRegion);
                        }
                        break;

                    case "mouse":
                        {
                            animal = new Mouse(animalName, animalWeight, livingRegion);
                        }
                        break;

                    case "zebra":
                        {
                            animal = new Zebra(animalName, animalWeight, livingRegion);
                        }
                        break;
                }

                Console.WriteLine(animal.MakeSound());

                string foodType = foodData[0].ToLower();
                int foodQuantity = int.Parse(foodData[1]);
                Food food = new Vegetable(0);

                if (foodType == "vegetable")
                {
                    food = new Vegetable(foodQuantity);
                }
                else if (foodType == "meat")
                {
                    food = new Meat(foodQuantity);
                }
                try
                {
                    animal.EatFood(food);
                }
                catch (ArgumentException ex)
                {

                    Console.WriteLine(ex.Message);
                }
                
                Console.WriteLine(animal);

                dataAnimal = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            }
        }
コード例 #2
0
        public static void Main()
        {
            List <Animal> animals = new List <Animal>();

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                var animalInfo = input.Split();
                var foodInfo   = Console.ReadLine().Split();

                var animalType = animalInfo[0];
                var animalName = animalInfo[1];
                var weight     = double.Parse(animalInfo[2]);

                var  foodType = foodInfo[0];
                var  quantity = int.Parse(foodInfo[1]);
                Food food     = null;
                switch (foodType)
                {
                case "Fruit":
                    food = new Fruit(quantity);
                    break;

                case "Meat":
                    food = new Meat(quantity);
                    break;

                case "Seeds":
                    food = new Seeds(quantity);
                    break;

                case "Vegetable":
                    food = new Vegetable(quantity);
                    break;

                default:
                    Console.WriteLine("foodsProblem");
                    break;
                }

                if (animalType == "Owl" || animalType == "Hen")
                {
                    var wingSize = double.Parse(animalInfo[3]);

                    if (animalType == "Owl")
                    {
                        Bird owl = new Owl(animalName, weight, wingSize);

                        Console.WriteLine(owl.ProduceSound());

                        owl.Eat(food, quantity);

                        animals.Add(owl);
                    }

                    else if (animalType == "Hen")
                    {
                        Bird hen = new Hen(animalName, weight, wingSize);

                        Console.WriteLine(hen.ProduceSound());

                        hen.Eat(food, quantity);

                        animals.Add(hen);
                    }
                }

                else if (animalType == "Mouse" || animalType == "Dog")
                {
                    var livingRegion = animalInfo[3];

                    if (animalType == "Mouse")
                    {
                        Mammal mouse = new Mouse(animalName, weight, livingRegion);

                        Console.WriteLine(mouse.ProduceSound());

                        mouse.Eat(food, quantity);

                        animals.Add(mouse);
                    }

                    else if (animalType == "Dog")
                    {
                        Mammal dog = new Dog(animalName, weight, livingRegion);

                        Console.WriteLine(dog.ProduceSound());

                        dog.Eat(food, quantity);

                        animals.Add(dog);
                    }
                }

                else if (animalType == "Cat" || animalType == "Tiger")
                {
                    var livingRegion = animalInfo[3];
                    var breed        = animalInfo[4];

                    if (animalType == "Cat")
                    {
                        Feline cat = new Cat(animalName, weight, livingRegion, breed);

                        Console.WriteLine(cat.ProduceSound());

                        cat.Eat(food, quantity);

                        animals.Add(cat);
                    }

                    else if (animalType == "Tiger")
                    {
                        Feline tiger = new Tiger(animalName, weight, livingRegion, breed);

                        Console.WriteLine(tiger.ProduceSound());

                        tiger.Eat(food, quantity);

                        animals.Add(tiger);
                    }
                }
            }

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