samedi 22 août 2015

How would you write Junit tests for this code?

Im new to java and practising my coding, how would I write some Junit tests for this code without changing it? I wanted to write some Junits to see if the output is correct. Could someone provide one such example? Any help is greatly appreciated.

package returnOnInvestment;

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 BestInvesment
{
    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 = 1;//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);       
    }
}

Aucun commentaire:

Enregistrer un commentaire