I have done pretty good with out any assistance but I am done to my last night and I am stumped.. The good news is that I have it working... however, I need to be able to input a decimal for the annual interest input, if that is input it freaks out and quits, if a whole number is entered it works fine.
The only other issue I need to fix, it that I need to print the Monthly Payment. I can make it print but I don't want it in the loop, because it prints over and over obviously. I can make it print to the screen anywhere else! Help!
CODE
import java.io.*;
import java.text.*;
public class MortgageCalculatorWK4{
private static BufferedReader stdin = new BufferedReader(
new InputStreamReader (System.in));
public static void main(String[] args) throws IOException{
//local objects
MortgageCalculatorWK4 mortgageCalc = new MortgageCalculatorWK4();
DecimalFormat df = new DecimalFormat("###,##0.00"); // to format the amount.
DecimalFormat numDf = new DecimalFormat("000"); // to format the sequence number.
double principal = 0;
double annualInterest = 0.0;
int numOfYears = 0;
System.out.print("Please enter the amount borrowed:"); //prompt user input
String input = stdin.readLine(); //get user input
principal= Integer.parseInt(input); //convert string to int
System.out.print("Please enter the interest rate:");
String input2 = stdin.readLine();
annualInterest = Integer.parseInt(input2);
System.out.print("Please enter the term of the loan in years:");
String input3 = stdin.readLine();
numOfYears = Integer.parseInt(input3);
//variables
double monthlyInterest = annualInterest/(12*100);
int totalNumOfMonths = numOfYears*12;
System.out.println("Payment# LoanBalance InterestPaid");
int monthCounter=1;
for(; totalNumOfMonths>0; totalNumOfMonths--){
//first get the monthly payment...
double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths);
//now get the monthly principal in the payment...
double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal);
principal-=monthlyPrincipal;
System.out.println(numDf.format(monthCounter) + " " +df.format(principal)+ " " +df.format((monthlyPayment-monthlyPrincipal)));
monthCounter++;
if(monthCounter%25==0)sleep(1000); // this will hesitate...
}
}
public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){
// Monthly payment formula: M = P x (J/(1-(1+J)^-N));
// M = P * ( J / (1 - (1 + J) ** -N));
// source: http://www.hughchou.org/calc/formula.html
double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
return monthlyPayment;
}
//calculate the monthly interst using simple interest.
public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){
return monthlyPayment - (remainingPrincipal * monthlyInterest);
}
// method to make the program sleep (hesitate) for a give time (in milliseconds)..
public static void sleep(long milliseconds){
try{
Thread.sleep(milliseconds);
}catch(Exception e){
//do nothing... for now...
}
}
}
Any help would be greatly appreciated