The code I pasted is for a school assignment where we have to calculate the monthly payment for 3 different loans, with varying interest rates, and then display some information about each payment for each of the 3 loans.
My code compiles and runs fine (not finished commenting all the sections yet), but my question is how come the balance is $-0.00 at the end of the first loan, but not on the other two loans?
Any help is greatly appreciated.
Thanks.
Brandon
public class MonthlyPaymentWk5 {
public static void main(String[] args) {
// Declare Variables to be used
double principle = 200000.00; // The total balance owed is $200,000.00
double[] interest = {5.35, 5.5, 5.75}; // Create the interest array for the 3 different interest percentages
int[] term = {7, 15, 30}; // Create the term array for the 3 different terms
double numMonths = 0; // Variable to hold the # of months for each term
double monthlyInterest = 0; // Variable to hold the monthly interest for each term
double monthlyPayment= 0; // Variable to hold the monthly payment for each term
double principlePayment = 0; // Variable to hold portion of payment made to principle
double interestPayment = 0; // Variable to hold portion of payment made to interest
double balance = 0; // Variable to hold current loan balance
java.text.DecimalFormat dcm= new java.text.DecimalFormat("$,###.00"); // Formats dollar amount outputs to look like money
/* Determine the interest per month, and how many total months there are for each term
using the arrays that were previously declared for term and interest */
int i;
for (i=0; i<term.length; i++) {
monthlyInterest = (interest[i] / (12 * 100));
numMonths = (term[i] * 12);
//Calculate the amortized monthly payment for each loan
monthlyPayment = principle * ( monthlyInterest / (1 - Math.pow(1 + monthlyInterest, -numMonths)));
// Output the monthly payment amount for each loan
System.out.println(); // Print a blank line for a break in the visual output
System.out.println("Loan " + (i+1) + ":");
System.out.println("A " + dcm.format(principle) + " loan with an interest rate of " + interest[i] + "% over a period of " + term[i] + " years"); // Print the first line
System.out.println("has a monthly payment of " + dcm.format(monthlyPayment)); // Print the second line, rounding to two decimal places and 1 new line commands
balance = principle;
interestPayment = balance * (interest[i] / (12 * 100));
principlePayment = monthlyPayment - interestPayment;
System.out.println("Loan " + (i+1) + ":");
System.out.println("The interest and balance amounts for each payment are as follows:");
int j;
for (j=0; j < numMonths; j++){
System.out.printf("Payment " + (j+1) + ": Interest Paid - $%.2f", + interestPayment );
System.out.printf(" Remaining Balance - $%.2f\n", + (balance - principlePayment));
// These 3 lines make the necessary subtractions so that the loop does not repeat on the same numbers over and over
balance = balance - principlePayment;
interestPayment = balance * (interest[i] / (12 * 100));
principlePayment = monthlyPayment - interestPayment;
// This section is to display 20 lines at a time and then to pause for a few moments before continuing on
if (j % 20 == 0 && j != 0) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) { }
System.out.println();
}
}
}
System.out.println(); // Print a blank line for a break in the visual output
}
}