Example #1
0
 /// <summary>
 /// Copy constructor.
 /// </summary>
 /// <param name="other">penguin to be copied</param>
 public Penguin(Penguin other) : base(other)
 {
     this.SwimSpeed = other.SwimSpeed;
 }
Example #2
0
        /// <summary>
        /// Return an animal of the wanted type, so that the MainForm or AnimalManager don't have to.
        /// Most arguments are strings. They need to be pre-validated.
        /// </summary>
        /// <param name="name">the name of this individual animal</param>
        /// <param name="age">the age of this individual animal</param>
        /// <param name="gender">the gender of this animal</param>
        /// <param name="categoryProperty">a property that is relevant for the category of this animal, such as for mammals or for birds.</param>
        /// <param name="speciesProperty">a property that is relevant for the species that this animal belongs to.</param>
        /// <param name="species">the species of the animal to be returned. Used to interpret the property arguments.</param>
        /// <returns>an Animal of the requested kind.</returns>
        public static Animal MakeAnimal(string name,
                                        int age,
                                        Gender gender,
                                        string categoryProperty,
                                        string speciesProperty,
                                        string species)
        {
            int    toothcount, numberEaten;
            double wingspan, speed;
            Animal animal = null;


            switch (species)
            {
            case "Bear":
                if (int.TryParse(categoryProperty, out toothcount))
                {
                    if (int.TryParse(speciesProperty, out numberEaten))
                    {
                        animal = new Bear(name, gender, age, toothcount, numberEaten);
                    }
                }
                break;

            case "Gnu":
                if (int.TryParse(categoryProperty, out toothcount))
                {
                    if (int.TryParse(speciesProperty, out numberEaten))
                    {
                        animal = new Gnu(name, gender, age, toothcount, numberEaten);
                    }
                }
                break;

            case "Eagle":
                if (double.TryParse(categoryProperty, out wingspan))
                {
                    if (double.TryParse(speciesProperty, out speed))
                    {
                        animal = new Eagle(name, gender, age, wingspan, speed);
                    }
                }
                break;

            case "Penguin":
                if (double.TryParse(categoryProperty, out wingspan))
                {
                    if (double.TryParse(speciesProperty, out speed))
                    {
                        animal = new Penguin(name, gender, age, wingspan, speed);
                    }
                }
                break;

            default:
                // If execution arrives here, an unsupported species has been requested.
                throw new NotImplementedException();
            }

            return(animal);
        }