示例#1
0
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            string input = Console.ReadLine();

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

                string animalType = animalInfo[0];
                string name       = animalInfo[1];
                double weight     = double.Parse(animalInfo[2]);

                string foodType     = foodInfo[0];
                int    foodQuantity = int.Parse(foodInfo[1]);

                Animal animal = default;

                if (animalType == "Cat")
                {
                    animal = new Cat(name, weight, animalInfo[3], animalInfo[4]);
                }
                else if (animalType == "Tiger")
                {
                    animal = new Tiger(name, weight, animalInfo[3], animalInfo[4]);
                }
                else if (animalType == "Dog")
                {
                    animal = new Dog(name, weight, animalInfo[3]);
                }
                else if (animalType == "Mouse")
                {
                    animal = new Mouse(name, weight, animalInfo[3]);
                }
                else if (animalType == "Hen")
                {
                    animal = new Hen(name, weight, double.Parse(animalInfo[3]));
                }
                else if (animalType == "Owl")
                {
                    animal = new Owl(name, weight, double.Parse(animalInfo[3]));
                }

                Food food = default;

                if (foodType == "Vegetable")
                {
                    food = new Vegetable(foodQuantity);
                }
                else if (foodType == "Fruit")
                {
                    food = new Fruit(foodQuantity);
                }
                else if (foodType == "Meat")
                {
                    food = new Meat(foodQuantity);
                }
                else if (foodType == "Seeds")
                {
                    food = new Seeds(foodQuantity);
                }

                Console.WriteLine(animal.ProduceSound());

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

                animals.Add(animal);

                input = Console.ReadLine();
            }

            Console.WriteLine(string.Join(Environment.NewLine, animals));
        }
示例#2
0
        static void Main(string[] args)
        {
            string        input      = Console.ReadLine();
            List <Animal> animalList = new List <Animal>();

            while (input != "End")
            {
                string type   = input.Split(" ")[0];
                string name   = input.Split(" ")[1];
                double weight = double.Parse(input.Split(" ")[2]);

                string foodInput = Console.ReadLine();
                string foodType  = foodInput.Split(" ")[0];
                int    quantity  = int.Parse(foodInput.Split(" ")[1]);

                switch (foodType)
                {
                case "Vegetable": Food vegetable = new Vegetable(quantity); break;

                case "Fruit": Food fruit = new Fruit(quantity); break;

                case "Meat": Food meat = new Meat(quantity); break;

                case "Seeds": Food seeds = new Seeds(quantity); break;
                }

                int foodEaten = 0;
                switch (type)
                {
                case "Owl":
                    double wingSize = double.Parse(input.Split(" ")[3]);
                    Animal owl      = new Owl(name, weight, foodEaten, wingSize);
                    animalList.Add(owl);
                    owl.AskFood();

                    if (foodType == "Meat")
                    {
                        owl.Weight   += 0.25 * quantity;
                        owl.FoodEaten = quantity;
                    }
                    else
                    {
                        Console.WriteLine
                            ($"{type} does not eat {foodType}!");
                    }

                    break;

                //
                //
                case "Hen":
                    wingSize = double.Parse(input.Split(" ")[3]);
                    Animal hen = new Hen(name, weight, foodEaten, wingSize);
                    hen.AskFood();
                    animalList.Add(hen);
                    hen.Weight   += 0.35 * quantity;
                    hen.FoodEaten = quantity;

                    break;

                //
                //
                case "Dog":
                    string livingRegion = input.Split(" ")[3];
                    Animal dog          = new Dog(name, weight, foodEaten, livingRegion);
                    animalList.Add(dog);
                    dog.AskFood();
                    if (foodType == "Meat")
                    {
                        dog.Weight   += 0.40 * quantity;
                        dog.FoodEaten = quantity;
                    }
                    else
                    {
                        Console.WriteLine
                            ($"{type} does not eat {foodType}!");
                    }

                    break;

                //
                //
                case "Mouse":
                    livingRegion = input.Split(" ")[3];
                    Animal mouse = new Mouse(name, weight, foodEaten, livingRegion);
                    animalList.Add(mouse);
                    mouse.AskFood();
                    if (foodType == "Vegetable" || foodType == "Fruit")
                    {
                        mouse.Weight   += 0.10 * quantity;
                        mouse.FoodEaten = quantity;
                    }
                    else
                    {
                        Console.WriteLine
                            ($"{type} does not eat {foodType}!");
                    }

                    break;

                //
                //
                case "Cat":
                    string breed = input.Split(" ")[4];
                    livingRegion = input.Split(" ")[3];
                    Animal cat = new Cat(name, weight, foodEaten, livingRegion, breed);
                    animalList.Add(cat);
                    cat.AskFood();
                    if (foodType == "Vegetable" || foodType == "Meat")
                    {
                        cat.Weight   += 0.30 * quantity;
                        cat.FoodEaten = quantity;
                    }
                    else
                    {
                        Console.WriteLine
                            ($"{type} does not eat {foodType}!");
                    }

                    break;

                //
                //
                case "Tiger":
                    breed        = input.Split(" ")[4];
                    livingRegion = input.Split(" ")[3];
                    Animal tiger = new Tiger(name, weight, foodEaten, livingRegion, breed);
                    animalList.Add(tiger);
                    tiger.AskFood();
                    if (foodType == "Meat")
                    {
                        tiger.Weight   += 1.00 * quantity;
                        tiger.FoodEaten = quantity;
                    }
                    else
                    {
                        Console.WriteLine
                            ($"{type} does not eat {foodType}!");
                    }

                    break;
                }
                input = Console.ReadLine();
            }
            foreach (var animal in animalList)
            {
                Console.WriteLine(animal.ToString());
            }
        }