示例#1
0
        static void Main()
        {
            string[] input = Console.ReadLine().Split();
            while (input[0] != "End")
            {
                List <Animal> animal = new List <Animal>();
                switch (input[0])
                {
                case "Cat":
                    Animal cat = new Cat(input[1], input[0], double.Parse(input[2]), 0, input[3], input[4]);
                    animal.Add(cat);
                    break;

                case "Tiger":
                    Animal tiger = new Tiger(input[1], input[0], double.Parse(input[2]), 0, input[3]);
                    animal.Add(tiger);
                    break;

                case "Mouse":
                    Animal mouse = new Mouse(input[1], input[0], double.Parse(input[2]), 0, input[3]);
                    animal.Add(mouse);
                    break;

                case "Zebra":
                    Animal zebra = new Zebra(input[1], input[0], double.Parse(input[2]), 0, input[3]);
                    animal.Add(zebra);
                    break;
                }

                input = Console.ReadLine().Split();

                switch (input[0])
                {
                case "Meat":
                    Food meat = new Meat(int.Parse(input[1]), input[0]);
                    foreach (var one in animal)
                    {
                        one.MakeSound();
                        try
                        {
                            one.EatFood(meat);
                        }
                        catch (ArgumentException ae)
                        {
                            Console.WriteLine(ae.Message);
                        }
                        Console.WriteLine(one.ToString());
                    }
                    break;

                case "Vegetable":
                    Food vegetable = new Vegetable(int.Parse(input[1]), input[0]);

                    foreach (var one in animal)
                    {
                        one.MakeSound();
                        try
                        {
                            one.EatFood(vegetable);
                        }
                        catch (ArgumentException ae)
                        {
                            Console.WriteLine(ae.Message);
                        }
                        Console.WriteLine(one.ToString());
                    }

                    break;
                }

                animal.Clear();

                input = Console.ReadLine().Split();
            }
        }
示例#2
0
        public static void Main()
        {
            string animalName;
            string animalType;
            double animalWeight;
            string animalLivingRegion;
            string catBreed;
            string foodType;
            int    foodQuantiy;

            var command = Console.ReadLine();

            while (command != "End")
            {
                var animalInfo = command.Split();
                animalType         = animalInfo[0];
                animalName         = animalInfo[1];
                animalWeight       = double.Parse(animalInfo[2]);
                animalLivingRegion = animalLivingRegion = animalInfo[3];
                Animal currentAnimal;

                switch (animalType.ToLower())
                {
                case "cat":
                    catBreed      = animalInfo[4];
                    currentAnimal = new Cat(animalName, animalType, animalWeight, animalLivingRegion, catBreed);
                    break;

                case "mouse":
                    currentAnimal = new Mouse(animalName, animalType, animalWeight, animalLivingRegion);
                    break;

                case "tiger":
                    currentAnimal = new Tiger(animalName, animalType, animalWeight, animalLivingRegion);
                    break;

                case "zebra":
                    currentAnimal = new Zebra(animalName, animalType, animalWeight, animalLivingRegion);
                    break;

                default:
                    currentAnimal = new Tiger("", "", 1, "");
                    break;
                }
                currentAnimal.MakeSound();

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

                Food currentFood;
                switch (foodType.ToLower())
                {
                case "vegetable":
                    currentFood = new Vegetable(foodQuantiy);
                    break;

                case "meat":
                    currentFood = new Meat(foodQuantiy);
                    break;

                default:
                    currentFood = new Vegetable(0);
                    break;
                }
                currentAnimal.Eat(currentFood);
                Console.WriteLine(currentAnimal.ToString());

                command = Console.ReadLine();
            }
        }