Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Mortgage Calculator Issue

1. ## Mortgage Calculator Issue

HI all,

I have my code working only problem is that on the first loan its supposed to stop after 84 months, but it goes all the way to 100.
This of course causes a negative ammount to be displayed. I need to figure out how to make only this first load (the 7 year one) stop at 84 months, but the others need to continue past that because they are 180 months and 360 months respectively.

Here is my code:
```// Dwight Welsh
/Mortgage Calculator

//Principal = The initial amount of the loan
//InterestYearly = The annual interest rate (from 1 to 100 percent)
//InterestMonthly = monthly interest in decimal form = interestYealy / (12 * 100)
//LengthInYears = the length (in years) of the loan
//NumberOfPayments = number of months over which loan is amortized = L * 12
//InterestMonthly = InterestYearly / (12.0*100.0)
//NumberOfPayments = LengthInYears *12
//MonthlyPayment = (Principal * InterestMonthly) / (1.0 - (Math.pow(1.0 + InterestMonthly, - NumberOfPayments)))

//This Imports the various classes

import java.text.NumberFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.Formatter;

class Loan {
public final double principal;
public final double interestYearly;
public final int lengthInYears;

public Loan(double principal, double interestYearly, int lengthInYears) {
this.principal = principal;
this.interestYearly = interestYearly;
this.lengthInYears = lengthInYears;
}

public double ComputeMonthlyInterest() {
return interestYearly / 12.0;
}

public double ComputeMonthlyPayment() {
double interestMonthly = ComputeMonthlyInterest();
double numberOfPayments = lengthInYears * 12.0;
return (principal * interestMonthly)
/ (1.0 - (Math.pow((1.0 + interestMonthly), -numberOfPayments)));
}
}

// File name is Mortgage.java
public class Mortgage {
private Loan loan;
private NumberFormat nf;

// Calculate and Return the Monthly Payment

public Mortgage(double principal, double interestYearly, int lengthInYears) {
this(new Loan(principal, interestYearly, lengthInYears));
}

public Mortgage(Loan loan) {
this.loan = loan;
nf = NumberFormat.getCurrencyInstance();
}

// Displays the Monthly Payment

public void DisplayMonthlyPayment() {
double monthlyPayment = loan.ComputeMonthlyPayment();
double total = loan.principal;
double mrate = loan.ComputeMonthlyInterest();

//tells the number of lines to show on a screen before asking the user to press enter
int linesPerScreen = 20;

//counts the number of lines (months) printed to the console
int monthsCount = 0;

//This will read input from the console
Scanner input = new Scanner(System.in);

System.out.println ("Loan amount " + nf.format(loan.principal)
+ " over " + loan.lengthInYears + " years term at "
+ (loan.interestYearly * 100) + "% interest rate");

System.out.println("Month\t\tPayment Amount\t\tInterest\tPrincipal");

//Start of Loop code

while (monthsCount < loan.lengthInYears * 12){
for( int k = 0 ; k < linesPerScreen ; k++ ){
double interest = total * mrate;
total -= (monthlyPayment - interest);
System.out.printf ("%2d\t\t%s\t\t%s\t\t%s\n",
monthsCount+1, nf.format(monthlyPayment), nf.format(interest), nf.format(total));;

//This will cause the month to increase
monthsCount = monthsCount+1;
}

System.out.println ("Press Enter to continue...");
input.nextLine();
}
}

public static void main(String[] args) {

System.out.println("Dwight Welsh: " + new Date());
Mortgage pgm = new Mortgage(200000, 0.0535, 7);
pgm.DisplayMonthlyPayment();
Mortgage pgm2 = new Mortgage(200000, 0.0550, 15);
pgm2.DisplayMonthlyPayment();
Mortgage pgm3 = new Mortgage(200000, 0.0575, 30);
pgm3.DisplayMonthlyPayment();

}

}```

Thanks in advance

COY