Search code examples
c#polymorphismvisual-studio-2019

Expressing dynamic polymorphism


Based on the animals danger rating that is inputted by the user, the calculate_insurance method will select the correct case and run the calculation to determine the cost of the animals insurance. The danger rating is set in the parent class The insurance calculation is done in the 2 child classes with dynamic polymorphism by overriding the parent class. Whats the best way of solving this? explanation with your code would be greatly appreciated

Base class -

public abstract class Animal
{
        protected int danger_rating = 0;

          //get danger rating from user (1-5)
        while (true)
        {
            Console.WriteLine("What is the animals danger rating? \nSelect a number between 1 - 5: "); //Prompt user to type in animals danger rating
            string input = Console.ReadLine(); //reads string input 

            if (int.TryParse(input, out danger_rating)) //convert user string input to int danger_rating
            {
                if (danger_rating == 1) //loop until danger_rating is between 1 and 5
                {
                    break;
                }
                else if (danger_rating == 2)
                {
                    break;
                }
                else if (danger_rating == 3)
                {
                    break;
                }
                else if (danger_rating == 4)
                {
                    break;
                }
                else if (danger_rating == 5)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("Invalid Number. \nNumber must be between 1 - 5");
                }
            }
            else
            {
                Console.WriteLine("This is not a number");
            }

        public virtual double calculate_insurance(int danger_rating)
        {
            return 0;
        } 
}

Mammal class -

public class Mammal : Animal //Derived class
{
        public virtual double calculate_insurance(int danger_rating)
        {
            int ins_base = 5000; //base for mammals
            
            double Insurance; //this will contain the output

            //Select case based on danger rating 1 - 5
            switch (danger_rating)
            {
                case 1:
                    Insurance = (ins_base * 10) * 0.02;
                    break;

                case 2:
                    Insurance = (ins_base * 20) * 0.05;
                    break;

                case 3:
                    Insurance = (ins_base * 30) * 0.05;
                    break;

                case 4:
                    Insurance = (ins_base * 40) * 0.05;
                    break;

                case 5:

                    Insurance = (ins_base * 50) * 0.10;
                    break;

                default:
                    Console.WriteLine("Invalid danger rating");
                    break;

            } //end of switch

            return base.calculate_insurance(danger_rating);

        }
}

Reptile class -

public class Reptile : Animal //Derived class
{
        public virtual double calculate_insurance(int danger_rating)
        {
            int ins_base = 1000; //base for reptiles
            
            double Insurance; //this will contain the output

            //Select case based on danger rating 1 - 5
            switch (danger_rating)
            {
                case 1:
                    Insurance = (ins_base * 10) * 0.02;
                    break;

                case 2:
                    Insurance = (ins_base * 20) * 0.05;
                    break;

                case 3:
                    Insurance = (ins_base * 30) * 0.05;
                    break;

                case 4:
                    Insurance = (ins_base * 40) * 0.05;
                    break;

                case 5:
                    Insurance = (ins_base * 50) * 0.10;
                    break;

                default:
                    Console.WriteLine("Invalid danger rating");
                    break;

            } //end of switch

            return base.calculate_insurance(danger_rating);

        }
}

Solution

  • I'm not really sure there is a problem to "solve", other than a syntax error in the base class of a while loop floating round at method level and some wonkiness where you calculate some insurance value for a derived type but then return the base type's insurance calc (which is 0).

    Your calculate_insurance methods in Mammal and Reptile should be declared to override the base method, and should end with return Insurance (note; it should be called insurance; we do not name local variables in PascalCase) not returning the result of calling the base..

    If you're then asking how to use what you have created. The idea behind polymorphism of this nature is that you can have a variable of the base type and not know or care what actual type is stored inside it, because you're only using functionality the base type claims exists

    Animal a = new Mammal();
    a.calculate_insurance(1);  //returns 1000, the rating for a Mammal of type 1
    
    Animal b = new Reptile();
    b.calculate_insurance(1);  //returns 200, the rating for a Reptile of type 1