示例#1
0
        public Insect CreateInsect(InsectSpecies Species)
        {
            Insect animalObj = null;

            switch (Species)
            {
            case InsectSpecies.Bee:
                animalObj = new Bee();
                break;

            //Continue with the rest
            case InsectSpecies.Butterfly:
                animalObj = new Butterfly();
                break;

            case InsectSpecies.Fly:
                animalObj = new Fly();
                break;

            default:
                Debug.Assert(false, "To be completed!");
                break;
            }


            animalObj.CategoryType = CategoryType.Insect;

            return(animalObj);
        }
示例#2
0
        /// <summary>
        /// Create an animal object of a species (Bee,Butterfly) of Insect cateogry.
        /// </summary>
        /// <param name="Species">Species: Bee, Butterfly, etc.</param>
        /// <returns>Object of the Species type.</returns>
        /// <remarks></remarks>
        public static Insect CreateInsect(InsectSpecies Species)
        {
            Insect animalObj = null;        //type not known at this time

            //type determined by late binding
            switch (Species)
            {
            case InsectSpecies.Bee:
                animalObj = new Bee();               //Late binding
                break;

            //Continue with the rest
            case InsectSpecies.Butterfly:
                animalObj = new Butterfly();        //Late binding
                break;

            default:
                Debug.Assert(false, "To be completed!");
                break;
            }

            //Set the category
            animalObj.Category = CategoryType.Insect;

            return(animalObj);     //return the created animal object.
        }
        public static Insects CreateInsect(InsectSpecies Species)
        {
            Insects animalObj = null; //type not known at this time

            //type determined by late binding
            switch(Species)
            {
                case InsectSpecies.Bee:
                    animalObj = new Bee();           //Late binding
                    break;
                //Continue with the rest
                case InsectSpecies.ButterFly:
                    animalObj = new ButterFly();    //Late binding
                    break;

                default:
                    Debug.Assert(false, "To be completed!");
                    break;
            }

            //set the category
            animalObj.Category = CategoryType.Insect;

            return animalObj; //return the crated animal Object
        }
示例#4
0
        private Animal castAnimal(int speciesIndex, CategoryType type)
        {
            //Checking which object to create:
            if (type == CategoryType.Bird)
            {
                BirdSpecies speciestype = (BirdSpecies)speciesIndex;
                animalis = BirdFactory.CreateBird(speciestype);
            }
            else if (type == CategoryType.Insect)
            {
                InsectSpecies insectSpec = (InsectSpecies)speciesIndex;
                animalis = InsectFactory.CreateInsect(insectSpec);
            }
            else if (type == CategoryType.Mammal)
            {
                MammalSpecies mammalSpec = (MammalSpecies)speciesIndex;
                animalis = MammalFactory.CreateMammal(mammalSpec);
            }
            else if (type == CategoryType.Marine)
            {
                MarineSpecies marineSpec = (MarineSpecies)speciesIndex;
                animalis = MarineFactory.CreateMarine(marineSpec);
            }
            else if (type == CategoryType.Reptile)
            {
                ReptileSpecies reptileSpec = (ReptileSpecies)speciesIndex;
                animalis = ReptileFactory.CreateReptile(reptileSpec);
            }

            return(animalis);
        }
示例#5
0
        //instance variables
        public static Insect CreateInsect(InsectSpecies species)
        {
            //type not known at this time
            Insect animalObj = null;

            try
            {
                //type determined by late binding - species is an enumeration
                switch (species)
                {
                    case InsectSpecies.Bee:

                        //ID, nickName, age, category, gender
                        animalObj = new Bee(); //late binding

                        if (animalObj == null)
                        {
                            NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                            throw (ex);
                        }
                        break;

                    //continue with the rest
                    case InsectSpecies.Butterfly:

                        animalObj = new Butterfly(); //late binding

                        if (animalObj == null)
                        {
                            NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                            throw (ex);
                        }
                        break;

                    default:
                        Debug.Assert(false, "To be completed!");
                        break;

                }
            }
            //custom exception
            catch (Exception e)
            {
                e.Message.ToString();
                NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                throw (ex);
            }
            finally
            {

                //Set the category
                animalObj.Category = CategoryType.Insect;
            }
            return animalObj;
        }
示例#6
0
        //instance variables

        public static Insect CreateInsect(InsectSpecies species)
        {
            //type not known at this time
            Insect animalObj = null;

            try
            {
                //type determined by late binding - species is an enumeration
                switch (species)
                {
                case InsectSpecies.Bee:

                    //ID, nickName, age, category, gender
                    animalObj = new Bee();     //late binding

                    if (animalObj == null)
                    {
                        NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                        throw (ex);
                    }
                    break;

                //continue with the rest
                case InsectSpecies.Butterfly:

                    animalObj = new Butterfly();     //late binding

                    if (animalObj == null)
                    {
                        NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                        throw (ex);
                    }
                    break;

                default:
                    Debug.Assert(false, "To be completed!");
                    break;
                }
            }
            //custom exception
            catch (Exception e)
            {
                e.Message.ToString();
                NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                throw (ex);
            }
            finally
            {
                //Set the category
                animalObj.Category = CategoryType.Insect;
            }
            return(animalObj);
        }
示例#7
0
 public static Insect CreateInsect(InsectSpecies Species)
 {
     /// <summary
     ///     Static method that creates choosen Animal, determed on the parameter Species.
     ///     Insect animalObj set to null;
     ///     <returns>animalObj with created animal object</returns>
     /// </summary
     Insect animalObj = null;
     switch (Species)
     {
         case InsectSpecies.Bee:
             animalObj = new Bee();
             break;
         case InsectSpecies.Butterfly:
             animalObj = new Butterfly();
             break;
     }
     animalObj.Category = CategoryType.Insect;
     return animalObj;
 }
        private Animal SetAnimalType()
        {
            Animal newAnimal        = new Animal();
            var    selectedCategori = listBxCategori.SelectedIndex;

            if (string.IsNullOrEmpty(listbxAnimalSpecies.Text))
            {
                showValidationMessage.ShowNewMessageBox("Du måste välja djurarten från listan!");
                return(newAnimal = null);
            }
            else
            {
                switch (selectedCategori)
                {
                case 0:
                    BirdSpecies birdSpecies = (BirdSpecies)Enum.Parse(typeof(BirdSpecies), listbxAnimalSpecies.Text);
                    BirdFactory birdFactory = new BirdFactory();
                    newAnimal = birdFactory.Createbird(birdSpecies);
                    break;

                case 1:
                    InsectSpecies insectSpecies = (InsectSpecies)Enum.Parse(typeof(InsectSpecies), listbxAnimalSpecies.Text);
                    InsectFactory insectFactory = new InsectFactory();
                    newAnimal = insectFactory.CreateInsect(insectSpecies);
                    break;

                case 2:
                    MammalSpecies mammalSpecies = (MammalSpecies)Enum.Parse(typeof(MammalSpecies), listbxAnimalSpecies.Text);
                    MammalFactory mammalFactory = new MammalFactory();
                    int           daysOfQuarantine;
                    int           numberOfteeth;
                    bool          underQuarantine = chBoxUnderQuarantine.Checked;
                    if (!helper.CheckInteger(texBoxDaysInQuarantine.Text, out daysOfQuarantine) && underQuarantine == true)
                    {
                        string messageDayOfQuarantine = "You have entered the error value in days of quarantine";
                        errorDayInQuarantiner.SetError(texBoxDaysInQuarantine, messageDayOfQuarantine);
                        showValidationMessage.ShowNewMessageBox(messageDayOfQuarantine);
                        newAnimal = null;
                        break;
                    }
                    else
                    {
                        errorDayInQuarantiner.Clear();
                    }
                    if (!int.TryParse(tboxNoOfTeeth.Text, out numberOfteeth))
                    {
                        errorNoOfTeeth.SetError(tboxNoOfTeeth, "You have entered the error value");
                        showValidationMessage.TeethException(tboxNoOfTeeth.Text);
                        newAnimal = null;
                        break;
                    }
                    else
                    {
                        errorNoOfTeeth.Clear();
                    }


                    newAnimal = mammalFactory.CreateMammal(mammalSpecies, daysOfQuarantine, underQuarantine, numberOfteeth);

                    break;

                case 3:
                    MarineSpecies marineSpecies = (MarineSpecies)Enum.Parse(typeof(MarineSpecies), listbxAnimalSpecies.Text);
                    MarineFactory marineFactory = new MarineFactory();
                    newAnimal = marineFactory.CreateMarine(marineSpecies);
                    break;

                case 4:
                    ReptileSpecies reptileSpecies = (ReptileSpecies)Enum.Parse(typeof(ReptileSpecies), listbxAnimalSpecies.Text);
                    ReptileFactory reptileFactory = new ReptileFactory();
                    newAnimal = reptileFactory.CreateReptile(reptileSpecies);
                    break;
                }
            }

            return(newAnimal);
        }