I am in
dire need of some expertise.
I am writing an applet that contains a mortgage calculator. My problem is that
I can't get my payments to amortize properly. Can someone please take a look
at this code and let me know where I'm going wrong? This was done in JCreator.
import java.awt.*;
import java.text.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Container;
import javax.swing.*;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MortgageWk31 extends JApplet implements ActionListener
{
//Declaring Labels, TexFields and Greeting
JLabel greetingLabel = new JLabel("Welcome to My Mortgage Calculator");
JLabel amountLabel = new JLabel("Amount");
JLabel termLabel = new JLabel("Select your Terms");
JLabel payLabel = new JLabel("You're Mortgage Payment is:");
JLabel scheduleLabel = new JLabel("Below is your Amortization Schedule:");
JButton calculateButton = new JButton("Calculate");
JTextField amountField = new JTextField("", 10);
JTextField paymentField = new JTextField("", 10);
JTextField tableField = new JTextField("", 40);
public void init() {
//Setting up the Layout Manager
SpringLayout layout = new SpringLayout();
getContentPane().setLayout(layout);
paymentField.setEditable(false);
//Adjusting constraints for labels
layout.putConstraint(SpringLayout.WEST, greetingLabel,
130,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, greetingLabel,
0,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, amountLabel,
25,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, amountLabel,
50,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, termLabel,
25,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, termLabel,
100,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, payLabel,
130,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, payLabel,
188,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, scheduleLabel,
135,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, scheduleLabel,
235,
SpringLayout.NORTH, getContentPane());
//Adjusting constraints for textfields
layout.putConstraint(SpringLayout.WEST, amountField,
75,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, amountField,
48,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, paymentField,
300,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, paymentField,
186,
SpringLayout.NORTH, getContentPane());
//Adjusting constraints for combo boxes
layout.putConstraint(SpringLayout.WEST, terms,
25,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, terms,
125,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, tableField,
25,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, tableField,
250,
SpringLayout.NORTH, getContentPane());
//Adjusting constraint for button
layout.putConstraint(SpringLayout.WEST, calculateButton,
25,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, calculateButton,
185,
SpringLayout.NORTH, getContentPane());
//Displays Labels, TextFields and Combo box in the Applet
getContentPane().add(greetingLabel);
getContentPane().add(amountLabel);
getContentPane().add(amountField);
getContentPane().add(termLabel);
getContentPane().add(terms);
getContentPane().add(tableField);
getContentPane().add(calculateButton);
getContentPane().add(payLabel);
getContentPane().add(paymentField);
getContentPane().add(scheduleLabel);
calculateButton.addActionListener(this);
}
//Terms for combo box menu
String[] termsArray = {"7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"};
JComboBox terms = new JComboBox(termsArray);
JComboBox amortizationCombo = new JComboBox();
public void actionPerformed(ActionEvent thisEvent) {
//Establishing local variables
double amt=0, rt=0, tm=0;
double monthlyInterest;
double monthlyPrinciple;
double balance = amt;
double payment=0;
try{
amt = Double.parseDouble(amountField.getText());
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null, "Non Numeric value entered for Amount", "Input Error", JOptionPane.WARNING_MESSAGE);
}
int menuNum = terms.getSelectedIndex();
switch(menuNum){
case 0:
rt = 5.35;
tm = 84;
break;
case 1:
rt = 5.5;
tm = 180;
break;
case 2:
rt = 5.75;
tm = 360;
break;
}
//Formulas for determining payment amount
double monthlyAPR = (rt /1200);
double exponentCalculation = (1 - Math.pow((1 + monthlyAPR), ( -tm)));
payment = (amt * monthlyAPR) / (exponentCalculation);
System.out.println("Monthly Payment is: "+payment);
monthlyInterest = (amt * monthlyAPR);
System.out.println("Monthly Interest is: "+monthlyInterest);
monthlyPrinciple = (payment - monthlyInterest);
System.out.println("Monthly Principle is: "+monthlyPrinciple);
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
String currencyOut = currencyFormatter.format(payment);
paymentField.setText(currencyOut);
for(int i=1;i<=tm;i++)
{
//Reduces Principle each month
double newBalance = (amt - monthlyPrinciple);
double remainingBalance = (newBalance);
monthlyPrinciple = (payment - monthlyInterest);
monthlyInterest = (newBalance * monthlyAPR);
//Determining monthly payment
payment = (amt * monthlyAPR) / (1 - Math.pow ((1 + monthlyAPR), - tm));
System.out.println("Payment "+i+"\tInterest Paid: "+currencyFormatter.format(monthlyInterest)+"\tRemaining Balance: "+currencyFormatter.format(remainingBalance));
}
}
}