Example #1
0
        private static Animal ParseAnimal(string input)
        {
            string[] animalTokens = input.Split();
            string   animalType   = animalTokens[0];
            string   name         = animalTokens[1];
            double   weight       = double.Parse(animalTokens[2]);
            string   thirdToken   = animalTokens[3];
            Animal   animal       = null;

            switch (animalType)
            {
            case "Cat":
                string catBreed = animalTokens[4];
                animal = new Cat(name, weight, thirdToken, catBreed);
                break;

            case "Tiger":
                string tigerBreed = animalTokens[4];
                animal = new Tiger(name, weight, thirdToken, tigerBreed);
                break;

            case "Dog":
                animal = new Dog(name, weight, thirdToken);
                break;

            case "Mouse":
                animal = new Mouse(name, weight, thirdToken);
                break;

            case "Owl":
                double owlWingSize = double.Parse(thirdToken);
                animal = new Owl(name, weight, owlWingSize);
                break;

            case "Hen":
                double henWingSize = double.Parse(thirdToken);
                animal = new Hen(name, weight, henWingSize);
                break;

            default:
                throw new ArgumentException("Invalid animal type!");
            }

            return(animal);
        }
Example #2
0
        public static void Main()
        {
            string inputLine;

            while ((inputLine = Console.ReadLine()) != "End")
            {
                var animalInfo   = inputLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var animalType   = animalInfo[0];
                var animalName   = animalInfo[1];
                var animalWeight = double.Parse(animalInfo[2]);
                var animalRegion = animalInfo[3];

                var  foodInfo = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                Food food;
                if (foodInfo[0] == "Vegetable")
                {
                    food = new Vegetable(int.Parse(foodInfo[1]));
                }
                else
                {
                    food = new Meat(int.Parse(foodInfo[1]));
                }

                Animal animal;
                switch (animalType)
                {
                case "Mouse":
                    animal = new Mouse(animalName, animalType, animalWeight, animalRegion);
                    break;

                case "Zebra":
                    animal = new Zebra(animalName, animalType, animalWeight, animalRegion);
                    break;

                case "Cat":
                    animal = new Cat(animalName, animalType, animalWeight, animalRegion, animalInfo[4]);
                    break;

                case "Tiger":
                    animal = new Tiger(animalName, animalType, animalWeight, animalRegion);
                    break;

                default:
                    animal = null;
                    break;
                }

                Console.WriteLine(animal.MakeSound());

                try
                {
                    animal.Eat(food);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

                Console.WriteLine(animal);
            }
        }