Search code examples
javafinance

Using for-loop to compare multiple investment options


The multiplier in the for-loop is changed with each passing year, however for some reason it is not being applied when the yearly calculation is being computed. Right now the only multiplier that is working is when the user inputs values 1-12 when prompted how many times the interest is compounded per year. Typically interest is compounded either daily, monthly, quarterly, or yearly. It is not working for daily.

    import java.util.Scanner;

    /**
       This program compares CD /Investment plans input by the year
       broken down by the requirements below:

       This program creates a table of compound interest investment growth over time
       Broken down by: a) year b) balance at end of year
       Finance formula of A= P(1+ r/n)^n*t is used:
       A = Future Value         | P = Initial Investment
       r = annual interest rate |n = times interest is compounded/year
       t = years invested
    */ 

     public class InvestmentTableFirstTest
     {
        public static void main(String[] args)
        {
             Scanner in = new Scanner(System.in);

             String bestBankName = "";
             double bestGrowth = 0;
             boolean done = false;

             while(!done)
             {
                 System.out.print("Plan name (one word, Q to quit): ");
                 String bankName = in.next();
                 if (bankName.equals("Q"))
                 {
                       done = true;
                 }
                 else
                 {
                      System.out.print("Please enter your principal investment: ");
                      final double PRINCIPAL_INVESTMENT = in.nextDouble();
                      System.out.print("Please enter the annual interest rate: ");
                      double iRate = in.nextDouble();
                      System.out.print("Please enter number of times interest is compounded per year:  ");
                      final double INCREMENT = in.nextDouble();      
                      System.out.print("Enter number of years: ");
                      int nyears = in.nextInt();

                      iRate = iRate/100; System.out.println("iRate:" + iRate);

                      //Print the table of balances for each year

                       for (int year = 1; year <= nyears; year++)
                       {
                        double MULTIPLIER = INCREMENT * year;
                        System.out.println("Multiplier: " + MULTIPLIER); // I've included this print statement to show that the multiplier changes with each passing year
                        double interest = 1 + (iRate/INCREMENT);
                        double balance = PRINCIPAL_INVESTMENT;
                        double growth =  balance * Math.pow(interest, MULTIPLIER);
                        growth = growth - PRINCIPAL_INVESTMENT;                      
                        balance = balance + growth;                                  
                        System.out.printf("Year: %2d  Interest Earned:   $%.2f\t   Ending Balance:   $%.2f\n", year, growth, balance);

                       if (bestBankName.equals("") || bestGrowth > growth) // || bestBankName > growth
                       {
                            bestBankName = bankName;  // bestBankName = bankName
                            bestGrowth = growth; // mostGrow = growth
                       }
                          System.out.println("Earning with this option: " + growth);        
                  }
             }

         } 
            System.out.println("Best Growth: " + bestBankName);
            System.out.println("Amount Earned: " + bestGrowth);       
       }
    }

Solution

  • This is just a simple error. You're comparing (bestBankName.equals("") || bestGrowth > growth) , which is if your current growth (so the first bank set) has a growth higher than the second bank. In other words, you flipped the arrow. You need to do if (bestBankName.equals("") || bestGrowth < growth) instead.