-2

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); 
            }
            
    }
  • 2
    Have you stepped through it in the debugger and looked at which type `tipType` actually is? (Hint: what's the return type of `Console.ReadLine`?) – madreflection May 02 '23 at 17:36
  • 1
    Another hint: the assignment doesn't say you need to accept user input. You can demonstrate each method with hard-coded input - such as the values given in the assignment. – Jon Skeet May 02 '23 at 17:38

3 Answers3

0

in your code:

    var tipPercentage = Console.ReadLine();

Console.ReadLine() returns a String, so tipPercentage will be type String, and neither of your if statements testing the run-time type of tipPercentage will evaluate to true, and thus no calculation is performed.

Paul Dempsey
  • 639
  • 3
  • 19
0

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. But being that C# is a statically typed language you should avoid using it. Even in cases where you think you need it, there may be other options like use of generics and generic constraints

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");
}
Narish
  • 607
  • 4
  • 18
0

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!

Larry Dukek
  • 2,179
  • 15
  • 16