-2

I am trying to make a class for a pet name, age, weight and if it is a dog or cat and the have to methods to calculate the dosage for Aceptomazine and Carprofen. I want to return a different value for both the Aceptomazine and Carprofen method for a cat and a dog but when I do it says Pet.Acropromazine: not all code paths return a value and the same error for the Carprofen method. I am not sure why it's doing this. any help would be appericated

 class Pet
{
    private string mName;
    private int mAge;
    private double mWeight;
    private string mType;

    public string Name
    {
        get { return mName; }
        set
        {
            if (string.IsNullOrEmpty(value) )
            {
                mName = value;
            }
            else
            {
                throw new Exception("Name cannot be empty");
            }
        }
    }
    public int Age
    {
        get { return mAge; }
        set
        {
            if (value > 1)
            {
                mAge = value;
            }
            else
            {
                throw new Exception("Age must be greater than 1");
            }
        }
    }

    public double Weight
    {
        get { return mWeight; }
        set
        {
            if (value > 5)
            {
                mWeight = value;
            }
            else
            {
                throw new Exception("Age must be greater than 5");
            }
        }
    }



    public Pet()
    {
        mName = "Super Pet";
        mType = "Dog";
        mAge = 1;
        mWeight = 5;

        }
    public double Acropromazine()
    {
        if (mType == "Dog")
        {
            return (mWeight / 2.205) * (0.03 / 10);
                }
        else if(mType =="Cat")
        {
            return (mWeight / 2.205) * (0.002/ 10);
        }
    }
    public double Carprofen()
    {
        if (mType == "Dog")
        {
            return (mWeight / 2.205) * (0.5 / 10);
        }
        else if (mType == "Cat")
        {
            return (mWeight / 2.205) * (0.25 / 10);
        }
    }
}
David197
  • 21
  • 4
  • Take a look at those functions and see if you can figure out what they return when `mType` is something other than "Dog" or "Cat". – Herohtar Nov 30 '19 at 06:06
  • There is a third condition in which it's not Dog and Cat, You need to write a else statement to handle that case. – Anirudha Gupta Nov 30 '19 at 06:15

1 Answers1

0

Like Anirudha Gupta said in the comment: you must have a third 'else' to handle when the type is neither a dog or cat ... the result would be something like:

public double Acropromazine()
{
    if (mType == "Dog")
    {
        return (mWeight / 2.205) * (0.03 / 10);
    }
    else if (mType == "Cat")
    {
        return (mWeight / 2.205) * (0.002 / 10);
    }
    else throw new NotSupportedException("only cats and dogs support Acropromazine");
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
OMR
  • 11,736
  • 5
  • 20
  • 35