Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of try - catch for taking care of letters

I have written a program that will emulate how a cash register works.

I would need some help with how to make the program take care of, if for example the user enters letters instead of numbers.

Then i would like the letters that the user entered were lost and the user receives a new opportunity to start from scratch.

I have written some code for it using try and catch, but not sure how it should be written.

class Program
{
    static void Main(string[] args)
    {
        int cash = 0;                    
        double totalAmount = 0;                     
        uint subTotal;                  
        int exchange;                   
        double roundingOffAmount;               

        Console.Write("Please enter a total amount for the cash register    : ");
        totalAmount = double.Parse(Console.ReadLine());

        if (totalAmount < 1)
        {
            Console.BackgroundColor = ConsoleColor.Red;
            Console.WriteLine("\nTotalamount needs to be more\n");
            Console.ResetColor();
            Environment.Exit(0);
        }

        try
        {
            Console.Write("Please enter cash for the cash register: ");
            cash = int.Parse(Console.ReadLine());
            if (cash < totalAmount)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine("\nCash needs to be more than totalAmount\n");
                Console.ResetColor();
                Environment.Exit(0);
                Console.WriteLine();
            }
            else
            {
                // Do nothing
            }
        }
        catch (FormatException)
        {
           Console.Write("\nSorry you typed in a letter you need to type in a number");
            Console.WriteLine();
            Console.BackgroundColor = ConsoleColor.Red;
            Console.WriteLine("\nSomething went wrong, please try again");
            Console.ResetColor();
            Console.WriteLine();               
            Main(args);
        }

        subTotal = (uint)Math.Round(totalAmount);
        roundingOffAmount = subTotal - totalAmount;

        exchange = cash - (int)totalAmount;

        Console.WriteLine("\n Receipt"
                        + "\n ------------------------------------"
                        + "\n Totalt \t: \t {0:c}", totalAmount);
        Console.WriteLine(" RoundingOffAmount\t: \t {0:f2}", roundingOffAmount);
        Console.WriteLine(" To pay \t: \t {0:c}", subTotal);
        Console.WriteLine(" Cash \t: \t {0:c}", cash);
        Console.WriteLine(" Exchange \t:\t {0:c}", exchange
                        + "\n ------------------------------------");
        Console.WriteLine();
    }
}

Any help is warmly received.

like image 459
John Avatar asked Dec 07 '22 04:12

John


1 Answers

Firstly - and more importantly - for currency values, you should be using decimal rather than double. Decimal floating point numbers are more appropriate for monetary values, which are exactly represented in decimal - whereas binary floating point types (double and float) are more appropriate for "natural" values such as height and weight, which will never have an absolutely precise measured value anyway. See my articles on binary floating point and decimal floating point for more details.

Next, rather than using exception handling for this validation, I suggest you use decimal.TryParse - which returns whether or not it was successful. That way you don't have to use try/catch just to catch a pretty predictable exception which can easily be avoided. For example:

decimal value;
while (!decimal.TryParse(Console.ReadLine(), out value))
{
    Console.WriteLine("Sorry, that wasn't a valid number");
}
like image 161
Jon Skeet Avatar answered Dec 30 '22 00:12

Jon Skeet