コード例 #1
0
        static void Main(string[] args)
        {
            string input;
            Animal beast = null;

            while ((input = Console.ReadLine()) != "End")
            {
                var animalInfo = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var foodInfo   = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

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

                    switch (animalType)
                    {
                    case "Tiger":
                        beast = new Tiger(name, animalType, weight, livingRegion);
                        break;

                    case "Zebra":
                        beast = new Zebra(name, animalType, weight, livingRegion);
                        break;

                    case "Mouse":
                        beast = new Mouse(name, animalType, weight, livingRegion);
                        break;

                    case "Cat":
                        beast = new Cat(name, animalType, animalInfo[4], weight, livingRegion);
                        break;

                    default:
                        break;
                    }

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

                    Food food = null;

                    if (foodType == "Meat")
                    {
                        food = new Meet(quantity);
                    }
                    else
                    {
                        food = new Vegetable(quantity);
                    }

                    beast.MakeSound();
                    beast.Eat(food);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }


                Console.WriteLine(beast);
            }
        }
コード例 #2
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();
            }
        }
コード例 #3
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();
            }
        }