Joseph

Joseph

  • NA
  • 14
  • 17.3k

using method

Apr 5 2011 2:19 PM
Instruction:

GetInvestment()
This method will input a double value for the princple, and a double value for the interest rate. All Values are input without error checking. All Values are returned to main as out parameters.

GetYears()
This method inputs the number of years for the investment as an integer value. The value will be checked to ensure that it is valid integer, and within the range of 5 to 15 years(inclusive). A loop will be used to ensure that the value meets these specifications. Return the value to the main program.

ShowGrowth()
This method calculates the value of the investment for each year, and displays the results as shown below. A For loop will be used to display the values. The method will be passed all values that are required. Note that the new principle for each year is old principle plus the interest times the old principle.

The Output should be.

Investment Calculator

Enter principle value: 1000
Enter interest rate: 10
Number of years: 4
Number of years is out of range.
Number of years: 16
Number of years is out of range.
Number of years: yuck
An invalid number was entered.
Number of years: 10

Year 1, Value = $1,100.00
Year 2, Value = $1,210.00
Year 3, Value = $1,331.00
Year 4, Value = $1,464.10
Year 5, Value = $1,610.51
Year 6, Value = $1,771.56
Year 7, Value = $1,948.72
Year 8, Value = $2,143.59
Year 9, Value = $2,357.95
Year 10, Value = $2,593.74

This is the code i have.
        static void Main(string[] args)
        {
            Console.Title = "Investment Calculator";

            Console.WriteLine("{0, 40}", "Investment Calculator");

            string sPlay = "no";

            double dvalue;
           
            do
            {
                GetInvestment(out dvalue, "Enter principle value: ");
               
                GetInvestment(out dvalue, "Enter interest rate: ");

                int iyears = GetYears("Number of years: ", 5, 15);

                double dResult = ShowGrowth(dvalue, iyears);

                Console.Write("\nRun again? \"yes\" to run again: ");
                sPlay = Console.ReadLine();
                Console.Clear();
            }
            while ((sPlay.ToLower() == "yes") || (sPlay.ToLower() == "Y") || (sPlay.ToLower() == "y"));
        }
        static public void GetInvestment(out double dvalue, string sPrompt)
        {
            bool bError;
            dvalue = 0.0;

            do
            {
                try
                {
                    bError = false;
                    Console.Write("\n{0}", sPrompt);
                    dvalue = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.Write("An invalid number was entered. Please try again.");
                    bError = true;
                }
            }
            while (bError);
            return;
        }
        static int GetYears(string sPrompt, int imin, int imax)
        {
            int iValue = 0;
            bool bError;

            do
            {
                try
                {
                    bError = false;
                    Console.WriteLine(sPrompt);
                    iValue = int.Parse(Console.ReadLine());

                    if ((iValue < imin) || (iValue > imax))
                    {
                        Console.WriteLine("The Value entered is out of range");
                        bError = true;
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("Number of years is out of range.");
                    bError = true;
                }
                finally
                {
                    Console.WriteLine("");
                }
            }
            while (bError);
            return iValue;
        }
        static double ShowGrowth(double dn, int iy)
        {
            double dAnswer = 0.0;

            for (int i = 1; i < iy; i++)
            {
                dAnswer = (dn * (dn / 100)) + dn;
                Console.WriteLine("Year {0}, Value = {1:C}", i, dAnswer);
            }
            return dAnswer;
        }

Answers (4)