.NET Fiddle

CSharp · Console

/Program.cs

using System;
using System.Text.RegularExpressions;

namespace Cohen_Brett_HW1
{
    public class Program
    {
        //Named constants
        public const decimal TacoPrice = 2.00m;
        public const decimal SandwichPrice = 7.00m;
        public const decimal TaxRate = .0825m;


        public static void Main(string[] args)
        {
            //Input variables
            String strCustCodeInput = "";
            int intTacoCountInput = 0;
            String strTacoCountInput = "";
            int intSandwichCountInput = 0;
            String strSandwichCountInput = "";

            //Variable for customer code validity
            Boolean boolCodeValid = false;


            //Get the customer code
            do
            {
                //Request input
                Console.WriteLine("Please enter the customer code:");
                strCustCodeInput = Console.ReadLine();

                //Make sure the code is valid
                boolCodeValid = CheckCode(strCustCodeInput);

            } while (boolCodeValid == false); //loop if customer code is invalid


            do
            {
                //Get the taco order count
                do
                {
                    //Request user input
                    Console.WriteLine("Enter the number of tacos you would like to order:");

                    //Read the input
                    strTacoCountInput = Console.ReadLine();

                    //Pass the string to CheckItem method and assign the result to intTacoCountInput
                    intTacoCountInput = CheckItem(strTacoCountInput);
                } while (intTacoCountInput == -1); //Loop if they enter an invalid number


                //Get the sandwich order count
                do
                {   //Request user input
                    Console.WriteLine("Enter the number of sandwiches you would like to order:");

                    //Read the input
                    strSandwichCountInput = Console.ReadLine();

                    //Pass the string to CheckItem method and assign the result to intTacoCountInput
                    intSandwichCountInput = CheckItem(strSandwichCountInput);

                } while (intSandwichCountInput == -1); //Loop if they enter an invalid number

                if (intTacoCountInput + intSandwichCountInput == 0)
                {
                    Console.WriteLine("Total number of items ordered must be greater than 0." + "\n");

                }
            } while (intTacoCountInput + intSandwichCountInput < 1); //make sure that total items ordered is at least one


            //Output variables

            //Total item count
            int intTotalItemCount = intTacoCountInput + intSandwichCountInput;

            //Taco subtotal
            decimal TacoSubtotal = TacoPrice * intTacoCountInput;

            //Sandwich subtotal
            decimal SandwichSubtotal = SandwichPrice * intSandwichCountInput;

            //Combined Subtotal
            decimal CombinedSubtotal = TacoSubtotal + SandwichSubtotal;

            //Tax Amount
            decimal TaxAmount = CombinedSubtotal * TaxRate;

            //Grand Total
            decimal GrandTotal = CombinedSubtotal + TaxAmount;

            //Console output
            Console.WriteLine("Customer Code: " + strCustCodeInput.ToUpper());
            Console.WriteLine("Total Items: " + intTotalItemCount);
            Console.WriteLine("Taco Subtotal: " + (TacoSubtotal.ToString("C")));
            Console.WriteLine("Sandwich Subtotal: " + (SandwichSubtotal.ToString("C")));
            Console.WriteLine("Full Subtotal: " + (CombinedSubtotal.ToString("C")));
            Console.WriteLine("Sales Tax: " + (TaxAmount.ToString("C")));
            Console.WriteLine("Grand Total: " + (GrandTotal.ToString("C")));

            //Keep console open
            Console.WriteLine("Press any key to exit");
            Console.ReadLine();

        }


        //This method validates the customer code
        public static Boolean CheckCode(String strInput)
        {
            if (strInput.Length < 4 || strInput.Length > 6)
            {
                Console.Write("Please enter a valid code, 4 to 6 characters, letters only." + "\n");
                return false;
            }

            else
            {   //make sure that input is letters only
                return Regex.IsMatch(strInput, @"^[a-zA-Z]+$");
            }
        }


        //This method validates the customer selection
        static int CheckItem(String strInput)
        {
            //Establish integer variable
            int NowInt;

            //Convert the strInput to an integer
            NowInt = Convert.ToInt32(strInput);

            //Validate
            if (NowInt > -1)
            {
                return NowInt;
            }
            else
            {
                Console.Write("Please enter a valid number greater than or equal to 0." + "\n");
                return -1;
            }
        }
    }
}
;