private static void CreateAnimal(List <Animal> animals, string animal, string name, int age, string gender)
        {
            Animal currentAnimal;

            switch (animal)
            {
            case "Cat":
                currentAnimal = new Cat(name, age, gender);
                animals.Add(currentAnimal);
                break;

            case "Kitten":
                currentAnimal = new Kitten(name, age, gender);
                animals.Add(currentAnimal);
                break;

            case "Tomcat":
                currentAnimal = new Tomcat(name, age, gender);
                animals.Add(currentAnimal);
                break;

            case "Dog":
                currentAnimal = new Dog(name, age, gender);
                animals.Add(currentAnimal);
                break;

            case "Frog":
                currentAnimal = new Frog(name, age, gender);
                animals.Add(currentAnimal);
                break;

            default:
                Console.WriteLine("Invalid input!");
                break;
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            string animalType = string.Empty;

            while ((animalType = Console.ReadLine()) != "Beast!")
            {
                try
                {
                    string[] animalArgs = Console.ReadLine().Split();
                    string   animalName = animalArgs[0];

                    int animalAge;
                    if (!int.TryParse(animalArgs[1], out animalAge))
                    {
                        throw new ArgumentException("Invalid input!");
                    }
                    string animalGander = animalArgs[2];

                    switch (animalType)
                    {
                    case "Dog":
                        Dog dog = new Dog(animalName, animalAge, animalGander);

                        PrintAnimalProperties(dog);
                        Console.WriteLine(dog.ProduceSound());
                        break;

                    case "Cat":
                        Cat cat = new Cat(animalName, animalAge, animalGander);

                        PrintAnimalProperties(cat);
                        Console.WriteLine(cat.ProduceSound());
                        break;

                    case "Frog":
                        Frog frog = new Frog(animalName, animalAge, animalGander);

                        PrintAnimalProperties(frog);
                        Console.WriteLine(frog.ProduceSound());
                        break;

                    case "Kittens":
                        Kittens kittens = new Kittens(animalName, animalAge, animalGander);

                        PrintAnimalProperties(kittens);
                        Console.WriteLine(kittens.ProduceSound());
                        break;

                    case "Tomcat":
                        Tomcat tomcat = new Tomcat(animalName, animalAge, animalGander);

                        PrintAnimalProperties(tomcat);
                        Console.WriteLine(tomcat.ProduceSound());
                        break;
                    }
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }
            }
        }