Beispiel #1
0
        private static Animal GetAnimal(string input, string[] animalArgs)
        {
            string name = animalArgs[0];
            int    age  = int.Parse(animalArgs[1]);

            Animal animal = null;

            if (input == "Dog")
            {
                animal = new Dog(name, age, animalArgs[2]);
            }
            else if (input == "Cat")
            {
                animal = new Cat(name, age, animalArgs[2]);
            }
            else if (input == "Frog")
            {
                animal = new Frog(name, age, animalArgs[2]);
            }
            else if (input == "Kitten")
            {
                animal = new Kitten(name, age);
            }
            else if (input == "Tomcat")
            {
                animal = new Tomcat(name, age);
            }
            else
            {
                throw new ArgumentException("Invalid input!");
            }

            return(animal);
        }
        public static void Main(string[] args)
        {
            string type;

            while (!(type = Console.ReadLine()).Equals("Beast!"))
            {
                var tokens = Console.ReadLine()
                             .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                try
                {
                    Animal animal = null;
                    switch (type)
                    {
                    case "Cat":
                        animal = new Cat(tokens[0], int.Parse(tokens[1]), tokens[2]);
                        break;
                        ;

                    case "Dog":
                        animal = new Dog(tokens[0], int.Parse(tokens[1]), tokens[2]);
                        break;

                    case "Frog":
                        animal = new Frog(tokens[0], int.Parse(tokens[1]), tokens[2]);
                        break;
                        ;

                    case "Kittens":
                        animal = new Kitten(tokens[0], int.Parse(tokens[1]), tokens[2]);
                        break;

                    case "Tomcat":
                        animal = new Tomcat(tokens[0], int.Parse(tokens[1]), tokens[2]);
                        break;

                    default:
                        throw new ArgumentException("Invalid input!");
                    }
                    Console.WriteLine(animal);
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid input!");
                }
            }
        }
Beispiel #3
0
        private static void ReadAndCreateAnimal(List <Animal> animals, string animalType)
        {
            string[] tokens = Console.ReadLine().Split();
            string   name   = tokens[0];
            int      age    = int.Parse(tokens[1]);
            string   gender = null;

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

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

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

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

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

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

            default:
                throw new ArgumentException("Invalid input!");
            }
        }
Beispiel #4
0
        static void AddAnimal(string command, List <Animal> animals)
        {
            var animalType = command;
            var info       = Console.ReadLine().Split();
            var name       = info[0];
            var age        = int.Parse(info[1]);
            var gender     = info[2];

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

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

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

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

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

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