MizziPizzi
MizziPizzi

Reputation: 103

Handle specific input

Ok I'm doing a univerity C# assignment that involves an animal motel...

I'll skip the details and cut to the chase..I'm havin problems with the input..I'm not sure how to handle the specific animal input like wingspan for birds, number of teeths for mammals? this is a chunk of the code from my mainform class:

private void AddAnimal(){

    Animal animalObj;

    Category animalCategory = (Category)CategoryList.SelectedIndex;

    switch (animalCategory)
    {
        case Category.Mammal:
            MammalSpecies animalSpecies = (MammalSpecies)Enum.Parse(typeof (MammalSpecies), AnimalList.Text);
            animalObj = MammalFactory.CreateMammal(animalSpecies);
         break;
    }
}

I was thinking of something like this:

animalObj.name = txtName.Text;

but realised I cant handle the specific animal input like this only the general like name age etc..

Upvotes: 0

Views: 115

Answers (1)

Karstan
Karstan

Reputation: 11

I'm not 100% sure if I understand your question, but here goes. I asume that you have problems setting up the stats for each animal, so that a non flying animal won't have wings. I suggest, that you create an animal super class, with all the general data/functionality and then create a sub class for each animal.

Here is an example of the animal class. This is the super class of all animals:

/// <summary>
/// This is an enum, that defines the different species of animals
/// </summary>
enum Species {Mammal, Fish, Insect, Bird}

/// <summary>
/// This is the base class for all animals
/// The class is abstract, so that we cant instantiated (Animal a = new Animal() is no longer possible)
/// It wouldn't make any sense to instantiate an animal (what color does and animal have, how many legs etc....) thats why its abstract
/// </summary>
abstract class Animal
{
    /// <summary>
    /// All animals has teeth, so we add this field to the animal
    /// </summary>
    private int teeth;

    /// <summary>
    /// All animals have legs, so we add this field in the animal as well
    /// </summary>
    private int legs;

    /// <summary>
    /// I'm not 100% sure about how the species should work
    /// I just implemented and enum for 3 species
    /// This can be adapted to suit your needs
    /// </summary>
    private Species species;

    /// <summary>
    /// This is the constructor of the animal. this constructor takes in 
    /// all the base values of the animal
    /// </summary>
    /// <param name="species">The current animals species</param>
    /// <param name="legs">The current animal's amount of legs</param>
    /// <param name="teeth">The current animal's amount of theeth</param>
    public Animal(Species species, int legs, int teeth)
    {
        //Sets the number of teeth on the current animal
        this.teeth = teeth;

        //Sets the number of legs on the current animal
        this.legs = legs;

        //Sets the species of the current animal
        this.species = species;
    }
}

This is an example of a sub class. In this case its a duck, as you can see it adds a wingspand to the animal:

/// <summary>
/// This is a duck class, it inherits from the Animal class
/// Inheritance is done in C# by using Class A : Class B
/// The point of inheritance is to pass on members and functionality from a superclass to a sub class
/// In this case we give the duck legs, teeth and a species
/// </summary>
class Duck : Animal
{
    private int wingSpan;

    /// <summary>
    /// This is the constructor of the Duck subclass
    /// It takes in all the parameters that the super class needs +wingSpan
    /// It passes on the superclass parameters to the Animal class by calling the super class constructer with the
    /// Keyword base()
    /// </summary>
    /// <param name="wingSpan">The duck's wingspan</param>
    /// <param name="species">The species of the duck</param>
    /// <param name="legs">The amount of legs of the duck</param>
    /// <param name="teeth">The duck's amount of teeth</param>
    public Duck(int wingSpan, Species species, int legs, int teeth) : base(species,legs,teeth)
    {
        //sets the ducks number of wings
        this.wingSpan = wingSpan;
    }
}

This is how you can create a duck inside add animal:

 //Instantiates a duck
 Duck duck = new Duck(2, Species.Bird, 2, 0);

Upvotes: 1

Related Questions