Exemplo n.º 1
0
        public static void Main()
        {
            string input;

            while ((input = Console.ReadLine()) != "Beast!")
            {
                try
                {
                    string[] animalElements = Console.ReadLine()
                                              .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

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

                    Animal animal;

                    if (input.ToLower() == "cat")
                    {
                        animal = new Cat(animalElements[0], age, gender);

                        Console.WriteLine(animal);
                    }
                    else if (input.ToLower() == "tomcat")
                    {
                        animal = new Tomcat(name, age, gender);

                        Console.WriteLine(animal);
                    }
                    else if (input.ToLower() == "kitten")
                    {
                        animal = new Kitten(name, age, gender);

                        Console.WriteLine(animal);
                    }
                    else if (input.ToLower() == "dog")
                    {
                        animal = new Dog(name, age, gender);

                        Console.WriteLine(animal);
                    }
                    else if (input.ToLower() == "frog")
                    {
                        animal = new Frog(name, age, gender);

                        Console.WriteLine(animal);
                    }
                    else
                    {
                        animal = new Animal(name, age, gender);
                        Console.WriteLine(animal);
                    }
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }
                catch (Exception)
                {
                    Console.WriteLine("Invalid input!");
                }
            }
        }
Exemplo n.º 2
0
        static void Main()
        {
            string input;

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

                try
                {
                    if (tokens.Length < 3)
                    {
                        throw new ArgumentException("Invalid input!");
                    }

                    string name = tokens[0];
                    int    age;
                    string gender = tokens[2];

                    if (int.TryParse(tokens[1], out age) == false)
                    {
                        throw new ArgumentException("Invalid input!");
                    }

                    switch (classType)
                    {
                    case "Cat":
                    {
                        var cat = new Cat(name, age, gender);
                        Console.WriteLine(cat);
                        break;
                    }

                    case "Dog":
                    {
                        var dog = new Dog(name, age, gender);
                        Console.WriteLine(dog);
                        break;
                    }

                    case "Frog":
                    {
                        var frog = new Frog(name, age, gender);
                        Console.WriteLine(frog);
                        break;
                    }

                    case "Kitten":
                    {
                        var kitten = new Kitten(name, age, "Female");
                        Console.WriteLine(kitten);
                        break;
                    }

                    case "Tomcat":
                    {
                        var tomcat = new Tomcat(name, age, "Male");
                        Console.WriteLine(tomcat);
                        break;
                    }

                    default:
                    {
                        throw new ArgumentException("Invalid input!");
                    }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }