示例#1
0
        /// <summary>
        /// Adds a new animal to the zoo.
        /// </summary>
        /// <param name="zoo">The current zoo.</param>
        private static void AddAnimal(Zoo zoo)
        {
            AnimalType animalType = ConsoleUtil.ReadAnimalType();

            Animal animal = AnimalFactory.CreateAnimal(animalType, string.Empty, 0, 1, Gender.Female);

            if (animal == null)
            {
                throw new NullReferenceException("Animal could not be found.");
            }

            animal.Name   = ConsoleUtil.InitialUpper(ConsoleUtil.ReadAlphabeticValue("Name"));
            animal.Gender = ConsoleUtil.ReadGender();
            animal.Age    = ConsoleUtil.ReadIntValue("Age");
            animal.Weight = ConsoleUtil.ReadDoubleValue("Weight");

            zoo.AddAnimal(animal);

            ConsoleHelper.ShowAnimal(zoo, animal.Name);
        }
示例#2
0
        /// <summary>
        /// Adds the animal.
        /// </summary>
        /// <param name="zoo"> The zoo being added.</param>
        private static void AddAnimal(Zoo zoo)
        {
            // Creates a dingo with all of the inputted values.
            Animal animal = new Dingo("Joey", 43, 344, Gender.Male);

            bool success = false;

            AnimalType animalType = ConsoleUtil.ReadAnimalType();

            animal = AnimalFactory.CreateAnimal(animalType, "null", 1, 1, Gender.Male);

            // Sets the name for the animal.
            while (!success)
            {
                try
                {
                    // Gets the name and makes it upper case.

                    animal.Name = ConsoleUtil.ReadAlphabeticValue("Name");
                    animal.Name = ConsoleUtil.InitialUpper(animal.Name);

                    success = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Value must only contain letters and spaces.");
                }
            }

            success = false;

            // Sets the gender for the animal.
            while (!success)
            {
                try
                {
                    animal.Gender = ConsoleUtil.ReadGender();

                    success = true;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }

            success = false;

            // Sets the age for the animal.
            while (!success)
            {
                try
                {
                    animal.Age = ConsoleUtil.ReadIntValue("Age");

                    success = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Value must be between 0 and 100.");
                }
            }

            success = false;

            // Sets the weight for the animal.
            while (!success)
            {
                try
                {
                    animal.Weight = ConsoleUtil.ReadDoubleValue("Weight");

                    success = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Value must be between 0 and 1000.");
                }
            }

            zoo.AddAnimal(animal);

            ShowAnimal(zoo, animal.Name);
        }
示例#3
0
        /// <summary>
        /// Adds an animal to the zoo.
        /// </summary>
        /// <param name="zoo">The zoo in which to add an animal.</param>
        private static void AddAnimal(Zoo zoo)
        {
            // Reads the animal type from the console.
            AnimalType readAnimal = ConsoleUtil.ReadAnimalType();

            // Creates the entered animal type.
            Animal animal = AnimalFactory.CreateAnimal(readAnimal, "Max", 2, 78, Gender.Female) as Animal;

            // Prompt for animal name.
            bool nameSuccess = false;

            while (!nameSuccess)
            {
                try
                {
                    string animalName = ConsoleUtil.ReadAlphabeticValue("Name");
                    animal.Name = ConsoleUtil.InitialUpper(animalName);

                    nameSuccess = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            // prompt for animal gender.
            bool genderSuccess = false;

            while (!genderSuccess)
            {
                try
                {
                    animal.Gender = ConsoleUtil.ReadGender();

                    genderSuccess = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            // Prompt for animal age.
            bool ageSuccess = false;

            while (!ageSuccess)
            {
                try
                {
                    animal.Age = ConsoleUtil.ReadIntValue("Age");

                    ageSuccess = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            // Prompt for animal weight.
            bool weightSuccess = false;

            while (!weightSuccess)
            {
                try
                {
                    animal.Weight = ConsoleUtil.ReadDoubleValue("Weight");

                    weightSuccess = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            // Add the newly created animal to the list.
            zoo.AddAnimal(animal);

            // Show the new animal.
            ShowAnimal(zoo, animal.Name);
        }
        /// <summary>
        /// Adds a new animal to the zoo.
        /// </summary>
        /// <param name="zoo">The current zoo.</param>
        private static void AddAnimal(Zoo zoo)
        {
            bool success = false;

            AnimalType animalType = ConsoleUtil.ReadAnimalType();

            Animal animal = AnimalFactory.CreateAnimal(animalType, string.Empty, 0, 1, Gender.Female);

            if (animal == null)
            {
                throw new NullReferenceException("Animal could not be found.");
            }

            while (!success)
            {
                try
                {
                    string name = ConsoleUtil.ReadAlphabeticValue("Name");

                    animal.Name = ConsoleUtil.InitialUpper(name);

                    success = true;
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            success = false;

            while (!success)
            {
                try
                {
                    animal.Gender = ConsoleUtil.ReadGender();

                    success = true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            success = false;

            while (!success)
            {
                try
                {
                    int age = ConsoleUtil.ReadIntValue("Age");

                    animal.Age = age;
                    success    = true;
                }
                catch (ArgumentOutOfRangeException e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            success = false;

            while (!success)
            {
                try
                {
                    double weight = ConsoleUtil.ReadDoubleValue("Weight");

                    animal.Weight = weight;
                    success       = true;
                }
                catch (ArgumentOutOfRangeException e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            zoo.AddAnimal(animal);

            ConsoleHelper.ShowAnimal(zoo, animal.Name);
        }