Sunday, January 24, 2016
Loan Monthly Payment Calculator
package myMortgagePackage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
/**
*
* @author Zau
*/
public class MortgageCalculatorFrm extends javax.swing.JFrame {
/**
* Creates new form MortgageCalculatorFrm
*/
public MortgageCalculatorFrm() {
initComponents();
//Step 1: Register button with anonymous actionListener class
jbtnComputeMonthlyPayment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
//Read data from user
double principle = Double.parseDouble(jtxtLoanAmount.getText());
double interestRate = Double.parseDouble(jtxtAPR.getText());
//int numberOfYear = Int.parseInt(jtxtYear.getText());
int numberOfYear = Integer.parseInt(jtxtYear.getText());
//Convert interest rate into decimal
interestRate /= 100.0;
//Monthly interest rate is the yearly rate divided by 12
double monthlyRate = interestRate/12.0;
//The lenght of the term in months is
//the number of years times 12
int termInMonths = numberOfYear * 12;
//Calculate the monthly payment
double monthlyPayment = (principle*monthlyRate) / (1-Math.pow(1+monthlyRate, -termInMonths));
//Total interest
double totalInterest = ((monthlyPayment * termInMonths) - principle);
NumberFormat formatter = NumberFormat.getCurrencyInstance();
jtxtMonthlyPayment.setText(formatter.format(monthlyPayment));
jtxtTotalInterest.setText(formatter.format(totalInterest));
}
});
//add action event to clear the button
jbtnClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
jtxtLoanAmount.setText("");
jtxtAPR.setText("");
jtxtYear.setText("");
jtxtMonthlyPayment.setText("");
jtxtTotalInterest.setText("");
}
});
}
Labels:
Java
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment