The program has a separate class. Both parts are attached.
GUI Portion of program:
/*
*
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
*
* @Programmer:
*/
public class GUICal extends JFrame implements ActionListener{
/**
* @param args the command line arguments
*/
//instance variables
private JTextField tf_mortAmount = new JTextField(9);
private JTextField tf_interRate = new JTextField(7);
private JTextField tf_term = new JTextField(3);
private JTextField tf_payment = new JTextField(9);
//Constructor to create and initalize submit button
public GUICal ()
{
JButton submit = new JButton("Submit");
submit.addActionListener(new SubmitListener());
tf_mortAmount.addActionListener(new SubmitListener());
tf_interRate.addActionListener(new SubmitListener());
tf_term.addActionListener(new SubmitListener());
tf_payment.setEditable(false);
//Create pane to organize information and set layout
JPanel info = new JPanel();
info.setLayout(new FlowLayout() );
//Add content to pane
info.add(new JLabel("Mortgage Amount:")); //Add label for Mortgage Amount
info.add (tf_mortAmount);//Text box to allow user to enter mortgage amount
info.add(new JLabel ("Interest Rate:"));
info.add(tf_interRate);
info.add(new JLabel ("Loan Term in Years:"));
info.add(tf_term);
info.add (submit);
info.add (new JLabel ("Monthly Mortgage Payment = $"));
info.add (tf_payment);//Text box to output payment to user
//Window attibutes
setContentPane(info);
pack();
setTitle("Mortgage Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
class SubmitListener implements ActionListener
{
private Object submit;
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if (source == submit)
{
try
{
double principal = Double.parseDouble(tf_mortAmount.getText());
double monthlyInt = Double.parseDouble(tf_interRate.getText());
int years = Integer.parseInt(tf_term.getText());
GUICalCalculation calc = new GUICalCalculation (principal, years, monthlyInt);
if(principal>0&&years>0&&monthlyInt>0){
tf_payment.setText("" + calc.payment);
}
}
catch(NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null, "Please make sure all your entries are numeric!", "USER INPUT ERROR", JOptionPane.ERROR_MESSAGE);
}
}
}
}
public static void main(String[] args) {
GUICal calculator = new GUICal();
calculator.setVisible(true);
}
}
Calculation portion
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.text.NumberFormat;
/**
*
* @author
*/
public class GUICalCalculation {
//member variables
private double principal = -1.0;
private double annualInterest = -1.0;
private int years = -1;
public double payment = -1;
private NumberFormat fmt = NumberFormat.getInstance();
private int monthlyInt;
GUICalCalculation(double principal, int years, double monthlyInt) {
this.principal = principal;
this.monthlyInt = (int) ((monthlyInt/100)/12);
this.years = years;
fmt.setMaximumFractionDigits(2);
fmt.setMinimumFractionDigits(2);
calculatePayment();
getPayment();
}
public void calculatePayment() {
payment = (principal * monthlyInt)/ (1 - Math.pow(1/ (1 + monthlyInt), years *12));
}
public double getPayment(){
return payment;
}
}

New Topic/Question
Reply



MultiQuote



|