Search code examples
c#methodsoverloadingoverload-resolution

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.

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);
        }
    }
}

Hi everyone, thanks y'all for the swift responds! I figured it out! Below is the change I made in the Main()

 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();

            double doubleTypeTip;
            int intTypeTip;
            if (Double.TryParse(tipPercentage, out doubleTypeTip))
            {  
                GetBill(mealPrice, doubleTypeTip);
            }
            
            else if(Int32.TryParse(tipPercentage, out intTypeTip))
            { 
                GetBill(mealPrice, intTypeTip); 
            }
            
    }

Solution

  • Because Console.ReadLine always returns a string, you need to convert the string to a numeric value, or more accurately attempt to convert. Since an integer can be converted to a double, you will need to attempt to convert to an integer first.

    There were a few other mistakes in your code:

    1. Total Bill value was using the wrong argument index, should be 2 not 3.
    2. The function that takes an integer should use the value divided by 100, not
    3. Also since you wouldn't want integer division for this, making the denominator a double will give you the value you are looking for.
    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();
    
                if (int.TryParse(tipPercentage, out var intTypeTip))
                {
                    GetBill(mealPrice, intTypeTip);
                }
                else if(double.TryParse(tipPercentage, out var doubleTypeTip))
                {
                    GetBill(mealPrice, doubleTypeTip);
                }
                else
                {
                    Console.WriteLine($"Invalid tip value of: {tipPercentage}");
                }
            }
    
            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 {2:C3}\n", mealPrice, tip, bill);
            }
    
            public static void GetBill(double mealPrice, int tipPercentage)
            {
                double tip = mealPrice * (tipPercentage / 100.0);
                double bill = mealPrice + tip;
                Console.WriteLine("The meal price is {0:C3}\n" +
                                "The tip is {1:C3}\n" +
                                "The total bill is {2:C3}\n", mealPrice, tip, bill);
            }
        }
    }
    

    Good luck and happy coding!