Java - Mortgage Calculator - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Java - Mortgage Calculator

TBRown12TBRown12 Posts: 2Member
Hello All!

I've been racking my brain for days trying to figure out how to get a Mortgage Calculator to run (getting input from user for interest rate, loan term and loan amount). The code I have is the basis on the rest of the program that I have to write. I am probably over thinking it but ANY help would be great!



import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.*;



public class Tasha2Week1{


//Creates Fields
JTextField AnnualPercentageRate = new JTextField();
JTextField TermInYears = new JTextField();
JTextField LoanAmount = new JTextField();
JTextArea Output = new JTextArea();

public static void main(String[] args) {
Tasha2Week1 Tasha2Week1 = new Tasha2Week1();
}
//Code for the Exit Button
private void jButtonQuitActionPerformed(java.awt.event.ActionEvent evt){// Code entered here will be run when the Quit button is clicked
System.exit(0);
}
//Code for the Clear Button
private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt){
//Clear ALL fields
AnnualPercentageRate.setText("");
TermInYears.setText("");
LoanAmount.setText("");
Output.setText("");
}
//Code for the Enter Button
public void PaymentCalculator(){
}

private void jButtonCalculateActionPerformed(java.awt.event.ActionEvent evt){
//Enter Calculator

DecimalFormat formatNumber = new DecimalFormat("$#,###.##");

double LoanAMT = Double.parseDouble(LoanAmount.getText());
double LoanTERM = Double.parseDouble(TermInYears.getText());
double LoanRATE = Double.parseDouble(AnnualPercentageRate.getText());
double Payment = LoanAMT*Math.pow(1+LoanRATE, LoanTERM)*LoanRATE/(Math.pow(1+LoanRATE, LoanTERM)-1);


Output.setText(formatNumber.format(Payment));

}


public Tasha2Week1(){
//GUI LAYOUT
JFrame jf = new JFrame();
jf.setTitle("Mortgage Calculator"); //Sets Title
jf.setSize(800, 500); //Set Size
jf.setVisible(true);

//GUI Objects
Container pane=jf.getContentPane();
JLabel l1 = new JLabel ("Please insert Interest Rate here: ");
JLabel l2 = new JLabel ("Please insert Loan Amount here: ");
JLabel l3 = new JLabel ("Please insert Loan Term in years here: ");

JButton Calculate = new JButton("Calculate");
JButton Quit = new JButton("Quit");
JButton Clear = new JButton("Clear");

JScrollPane scrollPane = new JScrollPane(Output);

pane.setLayout(null);
l1.setBounds(5, 20, 295, 15);
l2.setBounds(5, 120, 295, 15);
l3.setBounds(5, 220, 295, 15);

Calculate.setBounds(650,25,100, 20);
Clear.setBounds(650, 50,100, 20);
Quit.setBounds(650, 75,100, 20);
Output.setBounds(5, 300, 775,150);
scrollPane.setBounds(5,300,775,150);

pane.add(l1);
pane.add(AnnualPercentageRate);
pane.add(l2);
pane.add(LoanAmount);
pane.add(l3);
pane.add(TermInYears);

AnnualPercentageRate.setBounds(300, 20, 300, 20); // Sets loanfield, termfield, interestfield position on the panel
LoanAmount.setBounds(300, 120, 300, 20);
TermInYears.setBounds(300, 220, 300, 20);

pane.add(Calculate);
pane.add(Quit);
pane.add(Clear);
pane.add(Output);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);

Quit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(java.awt.event.ActionEvent evt){
jButtonQuitActionPerformed(evt);
}
});
Calculate.addActionListener(new ActionListener(){
@Override
public void actionPerformed(java.awt.event.ActionEvent evt){
jButtonCalculateActionPerformed(evt);
}
});
Clear.addActionListener(new ActionListener(){
@Override
public void actionPerformed(java.awt.event.ActionEvent evt){
jButtonClearActionPerformed(evt);
}
});





}


}
Sign In or Register to comment.