示例#1
0
        public static void Main()
        {
            while (true)
            {
                var type = Console.ReadLine();
                if (type == "Beast!")
                {
                    break;
                }

                var props  = Console.ReadLine().Split();
                var name   = props[0];
                var age    = int.Parse(props[1]);
                var gender = props[2];

                try
                {
                    if (type == "Dog")
                    {
                        Dog dog = new Dog(name, age, gender);

                        Console.WriteLine(dog);
                    }
                    else if (type == "Cat")
                    {
                        Cat cat = new Cat(name, age, gender);

                        Console.WriteLine(cat);
                    }
                    else if (type == "Frog")
                    {
                        Frog frog = new Frog(name, age, gender);

                        Console.WriteLine(frog);
                    }
                    else if (type == "Kitten")
                    {
                        Kitten kitten = new Kitten(name, age, "Female");

                        Console.WriteLine(kitten);
                    }
                    else if (type == "Tomcat")
                    {
                        Tomcat tomcat = new Tomcat(name, age, "Male");

                        Console.WriteLine(tomcat);
                    }
                    else
                    {
                        throw new ArgumentException("Invalid input!");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            string animalType;

            while ((animalType = Console.ReadLine()) != "Beast!")
            {
                string[] secondLine = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

                string name   = secondLine[0];
                int    age    = int.Parse(secondLine[1]);
                string gender = secondLine[2];

                try
                {
                    switch (animalType.ToLower())
                    {
                    case "dog":
                        Dog newDog = new Dog(name, age, gender);
                        animals.Add(newDog);
                        break;

                    case "cat":
                        Cat newCat = new Cat(name, age, gender);
                        animals.Add(newCat);
                        break;

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

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

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

                    default:
                        throw new ArgumentException("Invalid input!");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            foreach (var currAnimal in animals)
            {
                Console.WriteLine(currAnimal);
            }
        }
示例#3
0
        private static void ReadAndCreatAnimals(List <Animal> animals, string animalType)
        {
            var    tokens = Console.ReadLine().Split();
            var    name   = tokens[0];
            var    age    = int.Parse(tokens[1]);
            string gender = null;

            if (tokens.Length == 3)
            {
                gender = tokens[2];
            }

            switch (animalType)
            {
            case "Cat":
                var cat = new Cat(name, age, gender);
                animals.Add(cat);
                break;

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

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

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

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

            default:
                throw new ArgumentException("Invalid input!");
                break;
            }
        }
        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;
            }
        }
        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);
                }
            }
        }