Ejemplo n.º 1
0
        private static Queue <Animal> GetAnimals()
        {
            var animals = new Queue <Animal>();
            var kind    = Console.ReadLine();

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

                var name   = animalDetails[0];
                var age    = int.Parse(animalDetails[1]);
                var gender = animalDetails[2];

                try
                {
                    var animal = AnimalFactory.GetAnimal(kind, name, age, gender);
                    animals.Enqueue(animal);
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }

                kind = Console.ReadLine();
            }

            return(animals);
        }
Ejemplo n.º 2
0
        public static void Main()
        {
            while (true)
            {
                var animal = Console.ReadLine();

                if (animal == "Beast!")
                {
                    break;
                }

                var tokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                var name = tokens[0];
                int age;
                var isParsable = int.TryParse(tokens[1], out age);

                if (!isParsable)
                {
                    Console.WriteLine("Invalid input!");
                }

                try
                {
                    if (animal != "Tomcat" && animal != "Kitten")
                    {
                        var    gender        = tokens[2];
                        Animal currentAnimal = AnimalFactory.GetAnimal(animal, name, age, gender);
                        Console.WriteLine(currentAnimal.ToString());
                    }
                    else
                    {
                        Animal currentAnimal = AnimalFactory.GetAnimal(animal, name, age);
                        Console.WriteLine(currentAnimal.ToString());
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
        static void Main(string[] args)
        {
            AnimalFactory factory = new AnimalFactory();
            List <Animal> animals = new List <Animal>();

            while (true)
            {
                try
                {
                    string line = Console.ReadLine();
                    if (line == "Beast!")
                    {
                        break;
                    }

                    string   typeAnimal      = line;
                    string[] characteristics = Console.ReadLine().Split();

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

                    Animal animal = factory.GetAnimal(typeAnimal, name, age, gender);
                    animals.Add(animal);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
Ejemplo n.º 4
0
        private static void AddNewAnimal(List <Animal> animals, string type, string name, int age, string gender)
        {
            var animal = AnimalFactory.GetAnimal(type, name, age, gender);

            animals.Add(animal);
        }