예제 #1
0
        private static Food CreateFood(string[] parts)
        {
            string type = parts[0];

            Food food = null;

            if (type == "Meat")
            {
                food = new Meat(int.Parse(parts[1]));
            }
            else if (type == "Vegetable")
            {
                food = new Vegetable(int.Parse(parts[1]));
            }
            else if (type == "Fruit")
            {
                food = new Fruit(int.Parse(parts[1]));
            }
            else if (type == "Seeds")
            {
                food = new Seeds(int.Parse(parts[1]));
            }

            return(food);
        }
예제 #2
0
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            while (true)
            {
                string[] animalTokens = Console.ReadLine().Split();
                if (animalTokens[0] == "End")
                {
                    break;
                }
                string[] foodTokens = Console.ReadLine().Split();
                if (foodTokens[0] == "End")
                {
                    break;
                }

                string animalType   = animalTokens[0];
                string animalName   = animalTokens[1];
                double animalWeight = double.Parse(animalTokens[2]);

                Animal animal = null;

                if (animalType == "Cat")
                {
                    string animalLivingRegion = animalTokens[3];
                    string animalBreed        = animalTokens[4];
                    animal = new Cat(animalName, animalWeight, animalLivingRegion, animalBreed);
                }
                else if (animalType == "Tiger")
                {
                    string animalLivingRegion = animalTokens[3];
                    string animalBreed        = animalTokens[4];
                    animal = new Tiger(animalName, animalWeight, animalLivingRegion, animalBreed);
                }
                else if (animalType == "Hen")
                {
                    double animalWingSize = double.Parse(animalTokens[3]);
                    animal = new Hen(animalName, animalWeight, animalWingSize);
                }
                else if (animalType == "Owl")
                {
                    double animalWingSize = double.Parse(animalTokens[3]);
                    animal = new Owl(animalName, animalWeight, animalWingSize);
                }
                else if (animalType == "Mouse")
                {
                    string animalLivingRegion = animalTokens[3];
                    animal = new Mouse(animalName, animalWeight, animalLivingRegion);
                }
                else if (animalType == "Dog")
                {
                    string animalLivingRegion = animalTokens[3];
                    animal = new Dog(animalName, animalWeight, animalLivingRegion);
                }
                animals.Add(animal);

                string foodType     = foodTokens[0];
                int    foodQuantity = int.Parse(foodTokens[1]);
                Food   food         = null;
                if (foodType == "Vegetable")
                {
                    food = new Vegetable(foodQuantity);
                }
                else if (foodType == "Fruit")
                {
                    food = new Fruit(foodQuantity);
                }
                else if (foodType == "Seeds")
                {
                    food = new Seeds(foodQuantity);
                }
                else if (foodType == "Meat")
                {
                    food = new Meat(foodQuantity);
                }

                Console.WriteLine(animal.AskForFood());
                try
                {
                    animal.Eat(food);
                }
                catch (InvalidOperationException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            Console.WriteLine(string.Join(Environment.NewLine, animals));
        }
예제 #3
0
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();
            string        command;
            Animal        animal = null;
            Food          food   = null;

            while ((command = Console.ReadLine()) != "End")
            {
                string[] animalInfo = command.Split();
                string   type       = animalInfo[0];
                if (type == "Hen")
                {
                    double weight   = double.Parse(animalInfo[2]);
                    double wingSize = double.Parse(animalInfo[3]);
                    animal = new Hen(animalInfo[1], weight, wingSize);
                }
                else if (type == "Owl")
                {
                    double weight   = double.Parse(animalInfo[2]);
                    double wingSize = double.Parse(animalInfo[3]);
                    animal = new Owl(animalInfo[1], weight, wingSize);
                }
                else if (type == "Mouse")
                {
                    string name         = animalInfo[1];
                    double weight       = double.Parse(animalInfo[2]);
                    string livingRegion = animalInfo[3];
                    animal = new Mouse(name, weight, livingRegion);
                }
                else if (type == "Cat")
                {
                    string name         = animalInfo[1];
                    double weight       = double.Parse(animalInfo[2]);
                    string livingRegion = animalInfo[3];
                    string breed        = animalInfo[4];
                    animal = new Cat(name, weight, livingRegion, breed);
                }
                else if (type == "Tiger")
                {
                    string name         = animalInfo[1];
                    double weight       = double.Parse(animalInfo[2]);
                    string livingRegion = animalInfo[3];
                    string breed        = animalInfo[4];
                    animal = new Tiger(name, weight, livingRegion, breed);
                }
                else if (type == "Dog")
                {
                    string name         = animalInfo[1];
                    double weight       = double.Parse(animalInfo[2]);
                    string livingRegion = animalInfo[3];
                    animal = new Dog(name, weight, livingRegion);
                }
                animals.Add(animal);
                string   foods    = Console.ReadLine();
                string[] foodInfo = foods.Split();
                string   foodType = foodInfo[0];
                int      quantity = int.Parse(foodInfo[1]);
                if (foodType == "Vegetable")
                {
                    food = new Vegetable(quantity);
                }
                else if (foodType == "Fruit")
                {
                    food = new Fruit(quantity);
                }
                else if (foodType == "Meat")
                {
                    food = new Meat(quantity);
                }
                else if (foodType == "Seeds")
                {
                    food = new Seeds(quantity);
                }
                try
                {
                    animal.Eat(food);
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }
            }
            animals.ForEach(Console.WriteLine);
        }