Hi,
I am trying to create a mortgage calculator per an assignment and have it working ALMOST perfectly. I just need to add a loop to make it pause when the screen is filled and wait for an ENTER until the loan balance is 0. Can someone help me?
Here is what I have:
import java.text.*; // FOR THE FORMATTERS
public class MortgageProgram {
// FORMATTERS: see use in functions print(), and showAmortization().
NumberFormat currency = NumberFormat.getCurrencyInstance(); // currency format.
DecimalFormat intRate = new DecimalFormat("##.00"); // interest rate format.
// ADDED: rate, months
double payment = 0.0, rate = 0.0;
double principal = 200000.0; //*Principle amount of loan is $200,000
double annualInterest = 0.0575; //*Interest rate is currently 5.75%
int years = 30; //*Term of the loan is 30 years
int months = 0; // years expressed in months
public static void main (String[] args){
MortgageProgram MP = new MortgageProgram(args);
}
public MortgageProgram(String[] args) {
if (args.length == 3) {
principal = Double.parseDouble(args[0]);
annualInterest = Double.parseDouble(args[1])/100; // AUTO-CONVERT -- this assumes
// the user enters as 5.75, etc.
// as your statement in showUsage()
// implies.
years = Integer.parseInt(args[2]);
print();
}
else
{
showUsage();
}
}
public void calculatePayment(){ // ELIMINATED ARGUMENTS and, RETURN TYPE CHANGED TO VOID.
rate = annualInterest / 12;
months = years * 12; // USE NEW VARS.
payment = (principal * rate)
/ (1 - Math.pow(1/ (1 + rate), months)); //*Shows 1 monthly payment multiplied by 12 to make one complete year.
}
public void print(){ // ELIMINATED ARGUMENTS
calculatePayment();
System.out.println("The principal is " + currency.format(principal)); //*Shows the principle amount in $ value.
System.out.println("The annual interest rate is " + intRate.format(annualInterest * 100) +"%");
System.out.println("The term is " + years + " years"); //*Term is normally in years.
System.out.println("Your monthly payment is " + currency.format(payment)); //*Shows output of monthly payment.
showAmortization();
}
public void showUsage() {
System.out.println("Usage: Calculate Mortgage Payment Amount ");
System.out.println("\nFor example: $200,000 Mortgage loan amount, 5.75% interest for a 30 year term "); //*States that Loan Amount is $200,000, interest is 5.75% and it is 30 year term.
System.out.println("\nThe Following is your output based on the hardcoded amount: \n"); //*Will show output in the following format
print();
System.exit(0); //*System exits nice and quietly
}
public void showAmortization() {
double pmt2interest = 0.0; // payment-to-interest portion.
double pmt2principl = 0.0; // payment-to-principal portion.
double pmtBalance = 0.0;
System.out.println("\nPmt#:\tPrincipalPmt\tInterestPmt\tBalance");
System.out.println("====\t===========\t===========\t=======");
for ( int i = 1; i <= months; ++i )
{
pmt2interest = principal * rate;
pmt2principl = payment - pmt2interest;
pmtBalance = principal - pmt2principl;
System.out.println( "#" + i
+ "\tP=" + currency.format(pmt2principl)
+ "\tI=" + currency.format(pmt2interest)
+ (pmt2interest < 10 ? "\t\tB=" : "\tB=") // tab adjustment.
+ currency.format(pmtBalance) );
principal -= pmt2principl; // reduce by our payment-to-principal
}
}
}