import java.text.*; //To use text formatting
import java.util.Scanner; //To use the scanner
public class Wk1MortgagePaymentCalculator {
NumberFormat currency = NumberFormat.getCurrencyInstance(); //Currancy Format
DecimalFormat intRate = new DecimalFormat("##.00"); //Interest Format
/**
* The following loads the static variables for the program.
*/
double payment = 0.0;
double rate = 0.0;
double principal = 200000.0;
double[] annualInterest = {0.0535,0.0550,0.0575};
int[] years = {7,15,30};
int months = 0;
int lineCount = 20;
public static void main(String[] args) {
Wk1MortgagePaymentCalculator MP = new
Wk1MortgagePaymentCalculator(args);
}
public Wk1MortgagePaymentCalculator(String[] args) {
if (args.length == 3) {
principal = Double.parseDouble(args[0]);
for (int i=0; i< annualInterest.length; i++)
annualInterest[i] = Double.parseDouble(args[1])/100;
for (int i=0; i< years.length; i++)
years[i] = Integer.parseInt(args[2]);
print();
}
else
{
showUsage();
}
}
/**
* The following calculates the monthly payment.
*/
public void calculatePayment(){
for (int i=0; i< annualInterest.length; i++)
rate = annualInterest[i] / 12;
for (int i=0; i< years.length; i++)
months = years[i] * 12;
payment = (principal * rate)
/ (1 - Math.pow(1/ (1 + rate), months));
}
/**
* The following prints out information about the program, the static
* fields, and calls the amortization.
*/
public void print(){
System.out.println("The principal is " + currency.format(principal));
for (int i=0; i< annualInterest.length; i++)
System.out.println("The annual interest rate is " +
intRate.format(annualInterest[i] * 100) +"%");
for (int i=0; i< years.length; i++)
System.out.println("The term is " + years[i] + " years");
System.out.println("Your monthly payment is " + currency.format(payment));
//showAmortization(); // Calls the amortization method
}
public void showUsage() {
calculatePayment(); // Calls the calculate payment method
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 ");
System.out.println("\nThe Following is your output based on the "
+ "hardcoded amount: \n");
print();
System.exit(0); // Controlled exit
}
/**
* Prints out the amortization table for the loan.
*/
public void showAmortization() {
double pmtInterest = 0.0;
double pmtPrincipl = 0.0;
double pmtBalance = 0.0;
System.out.println("\nPmt#:\tPrincipalPmt\tInterestPmt\tBalance");
System.out.println("====\t===========\t===========\t=======");
Scanner s = new Scanner(System.in);
for ( int i = 1; i <= months; ++i )
{
pmtInterest = principal * rate; // Payment to intrest
pmtPrincipl = payment - pmtInterest; // Payment to princapl
pmtBalance = principal - pmtPrincipl;
System.out.println( "#" + i
+ "\tP=" + currency.format(pmtPrincipl)
+ "\tI=" + currency.format(pmtInterest)
+ (pmtInterest < 10 ? "\t\tB=" : "\tB=") // Tab adjustment
+ currency.format(pmtBalance) );
principal -= pmtPrincipl; // Reduce the payment-to-principal
if ( (i % lineCount) == 0 ) {
String input = s.nextLine(); // Pauses for an enter
}
}
s.close(); // Closes the scanner call
}
}