Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Overloading method but the output isn't what I expected

Here is the question:

Create a program named TipCalculation that includes two overloaded methods—one that accepts a meal price and a tip as doubles (for example, 30.00 and 0.20, where 0.20 represents a 20 percent tip), and one that accepts a meal price as a double and a tip amount as an integer (for example, 30.00 and 5, where 5 represents a $5 tip). Each method displays the meal price, the tip as a percentage of the meal price, the tip in dollars, and the total of the meal plus the tip. Include a Main() method that demonstrates each method.

Here is the code I wrote. The problem is when I entered the correct format input it doesn’t do any calculation at all. I suspect that may be I created ambiguity. But I thought the if condition should have appropriately selected the right method for the right parameter.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class TipCalculation
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the meal price: ");
            double mealPrice = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Please enter the tip percentage: ");
            var tipPercentage = Console.ReadLine();
            Type tipType = tipPercentage.GetType();
            if(tipType.Equals(typeof(double)))
            {
                double doubleTypeTip = Convert.ToDouble(tipPercentage);
                double tipValD = doubleTypeTip / 10;
                GetBill(mealPrice, tipValD);
            }

            if (tipType.Equals(typeof(int)))
            {
                int intTypeTip = Convert.ToInt32(tipPercentage);
                GetBill(mealPrice, intTypeTip);
            }
        }

        public static void GetBill(double mealPrice, double tipPercentage)
        {
            double tip = mealPrice * tipPercentage;
            double bill = mealPrice + tip;
            Console.WriteLine("The meal price is {0:C3}\n"+
                              "The tip is {1:C3}\n" +
                              "The total bill is {3:C3}\n",mealPrice, tip, bill);
        }

        public static void GetBill(double mealPrice, int tipPercentage)
        {
            double tip = mealPrice * (tipPercentage/10);
            double bill = mealPrice + tip;
            Console.WriteLine("The meal price is {0:C3}\n" +
                              "The tip is {1:C3}\n" +
                              "The total bill is {3:C3}\n", mealPrice, tip, bill);
        }
    }
}

>Solution :

var is not a dynamic type

To use use var is a static inferred type meaning that while you as the coder don’t have to worry about it, under the hood it will have a proper, statically compiled type

The only way to do dynamic typing in all of the dotnet languages is to use dynamic, or classic overkill object type

What I mean to say is:

var tipPercentage = Console.ReadLine();
Type tipType = tipPercentage.GetType();

Really will always mean:

string tipPercentage = Console.ReadLine();
Type tipType = tipPercentage.GetType(); //tipType == typeof(string)

As such, neither of the arms at the end of your main() method will trigger, hence nothing happens

You can also make things a bit easier by leaning on double more heavily. The reason is because, int 10 would simply be 10.00 as a double. The reverse, say 10.50 would be a problem. C# also have the decimal type for monetary calculations but that would be a bit much here

Here is your Main() method with some fixes, it simplifies the logic but also includes some validation logic

//Note: it is ideal to use double.TryParse() to better handle exception throws from faulty inputs
Console.WriteLine("Please enter the meal price: ");
string mealPrice = Console.ReadLine();

Console.WriteLine("Please enter the tip percentage: ");
string tipPercentage = Console.ReadLine();

if (double.TryParse(mealPrice, out double meal) && double.TryParse(tipPercentage, out double tip))
{
    GetBill(meal, tip;)
}
else
{
    //print message, or throw an exception like in the line below
    Console.WriteLine("Could not parse inputs");
    //throw new Exception("Could not parse inputs");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading