vendredi 5 février 2016

Issue with method to calculate and display

So, I'm having an issue with a programming assignment (I'm not going to lie, this is homework). The teacher assigned us something like the following: Get input from the user about the total of a purchase and the amount they submitted, then call a single method from the main routine that calculates and displays the results (where the results are the change the user gets back in hundred dollar bills, fifty dollar bills, etc. I have workable code for this problem, but I know I'm not fulfilling the requirements. I also have a "gut feeling" that there's a better way to write the code so that it isn't so bulky. I'm looking for suggestions on how to improve the code I do have. Thanks in advance for your help.

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

namespace Change
{
    class Change
    {
        static void Main(string[] args)
        {
            //Initialize variables
            double costOfPurchase = 0;
            double amountSubmitted = 0;
            double hundredDollarBills = 0;
            double fiftyDollarBills = 0;
            double twentyDollarBills = 0;
            double fiveDollarBills = 0;
            double quarters = 0;
            double nickles = 0;
            double pennies = 0;

            //Set up try catch for errors from user input
            try
            {
                //Get the cost of a sale
                Console.Write("Enter the total cost of your purchase: ");
                costOfPurchase = Convert.ToDouble(Console.ReadLine());

                if (costOfPurchase < 0)
                {
                    Console.WriteLine("The cost of your purchase cannot be a negative value.");
                }

                //Get the amount submitted to pay for the sale
                Console.Write("Enter the amount submitted: ");
                amountSubmitted = Convert.ToDouble(Console.ReadLine());

               if (costOfPurchase > amountSubmitted)
                {
                    Console.WriteLine("You have not submitted enough money.");
                }

                //Call a method to calculate and display the change due
                calculateChangeDue(costOfPurchase, amountSubmitted, out hundredDollarBills, out fiftyDollarBills, out twentyDollarBills, out fiveDollarBills, out quarters, out nickles, out pennies);

                Console.WriteLine(hundredDollarBills + " Hundred(s)");
                Console.WriteLine(fiftyDollarBills + " Fifty");
                Console.WriteLine(twentyDollarBills + " Twenty(s)");
                Console.WriteLine(fiveDollarBills + " Five");
                Console.WriteLine(quarters + " Quarter(s)");
                Console.WriteLine(nickles + " Nickle");
                Console.WriteLine(pennies + " Penny(s)");

                //Keep the console open in debug mode
                Console.Write("Press any key to exit.");
                Console.ReadKey();
            }
            catch (System.Exception e)
            {
                Console.WriteLine("An error has occurred.");
                Console.WriteLine("The eror was: {0}", e.Message);
                Console.ReadLine();
            }
        }

        //Method to calculate and display the change due
        private static void calculateChangeDue(double costOfPurchase, double amountSubmitted, out double hundredDollarBills, out double fiftyDollarBills, out double twentyDollarBills, out double fiveDollarBills, out double quarters, out double nickles, out double pennies)
        {
            //Initialize variables
            double changeDue = 0;
            double remainingChange = 0;

            //Calculate the change due
            changeDue = amountSubmitted - costOfPurchase;

            remainingChange = changeDue % 100;
            hundredDollarBills = (changeDue / 100) - (remainingChange / 100);

            fiftyDollarBills = (remainingChange / 50) - ((remainingChange % 50) / 50);
            remainingChange = remainingChange % 50;

            twentyDollarBills = (remainingChange / 20) - ((remainingChange % 20) / 20);
            remainingChange = remainingChange % 20;

            fiveDollarBills = (remainingChange / 5) - ((remainingChange % 5) / 5);
            remainingChange = remainingChange % 5;

            quarters = (remainingChange / 0.25) - ((remainingChange % 0.25) / 0.25);
            remainingChange = remainingChange % 0.25;

            nickles = (remainingChange / 0.05) - ((remainingChange % 0.05) / 0.05);
            remainingChange = remainingChange % 0.05;

            pennies = (remainingChange / 0.01) - ((remainingChange % 0.01) / 0.01);
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire