jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Mortgage Calculator -In Frames Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Mortgage Calculator -In Frames" Watch "Mortgage Calculator -In Frames" New topic
Author

Mortgage Calculator -In Frames

Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
I know this is a long shot but does anyone out there know a code for a Mortgage Calculator using Frames ? It would need to include things like Interest, Loan Amount,Property Value,Property Type eg. Town home,moblie home,family home,commercial property etc...,Loan Time in years thast sort of thing. So that you could work out the monthly payment. It must be using Frames aswell, any ideas ?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
search javaranch, sun, google for something you would expect to be common in a
console app, and a swing app.

pehaps the formula for monthly payment - remember to strip any 'variable' names
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
There are various ways of calculating monthly repayments of mortgages, it all depends on the type of loan structure. Before you worry about using frames, you should ensure your calculator gives correct results.
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Does anyone have any codes that work for this ?
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
This might be useful for you
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Thanks will the code on there work if I type it straigt in to Jbuilder 5 ?
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Does anyone know where I can find a code that is compatible with jBuilder, just to do a simple mortgage calculator in frames? Most of the codes are for html websites and I don't know how to convert them.
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
You haven't told us anything about your web site. Does it use JSP? Servlets? Something else? Do you want to make an applet?

Why not look at one of the many Javascript calculators for inspiration about how to make the calculations.

Writing the implementation in whatever framework you are using is really up to you.

The fact that there are frames involved should really have very little impact upon the main part of this task.
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
No, Its not even for a website. Its just supposed to be a simple programme which when run on jBuilder5 will produce a frame where you enter the Loan amount,Interest%, and Loan time in yrs into there own individual jTextFields. Then you press a jButton titled calculate, which then gives you the monthly payment in a jLabel.
I found this code but I think its for JavaScript and not Java:

MortgageCalculator mc = new MortgageCalculator();
double amount = 0;
double years = 0;
double interest = 0;
double annualTax = 0;
double annualInsurance = 0;
String temp = request.getParameter("txtAmount");
if(temp != null && temp.length() != 0)
amount = Double.parseDouble(temp);
temp = request.getParameter("txtYears");
if(temp != null && temp.length() != 0)
years = Double.parseDouble(temp);
temp = request.getParameter("txtInterest");
if(temp != null && temp.length() != 0)
interest = Double.parseDouble(temp);
temp = request.getParameter("txtTax");
if(temp != null && temp.length() != 0)
annualTax = Double.parseDouble(temp);
temp = request.getParameter("txtInsurance");
if(temp != null && temp.length() != 0)
annualInsurance = Double.parseDouble(temp);

mc.calculateMortgage(amount,
years,
interest,
annualTax,
annualInsurance);
pageContext.setAttribute("payments", mc);

Would anyone know how to make that run in jBuilder ?
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
OK, that sort of frame. Apologies.

The code you listed looks like Java (servlet code), but uses a class MortgageCalculator which you haven't listed the source for.

Basically you want to divide this task into two parts. The first part is to write a class MortgageCalculator which has a constructor which takes various arguments:



Then you need to add some methods to get access to the interesting information:



Now, the second part is to create your user interface. You will need input fields for the various arguments and a calculate button.

When the user clicks the calculate button, you need to check that they have filled out all of the fields correctly, then create a MortgageCalculator instance, passing the values.

Then you will need to get the monthly payment by calling the appropriate method on you MortgageCalculator, and set the value of the user interface element you want to display it in.

So, where to start?

First write down, on paper, the calculation you need to do to get the monthly payment given the total amount, number of years/months etc.

Now write the MortgageCalculator class, using the formula you wrote down to implement the required calculation. Also write a small driver (perhaps give MortgageCalculator a main method), so you can test that it is working from the command line.

I recommend that you read the Swing tutorial on the Sun site. This will help you create the user interface part.
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Thanks, how do you people know so much ? you must be an expert. I'll post my attempts in a while
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
I gave you a link to the FormattedTextDemo in the Java Tutorial, this is exactly what you want. Here is a link from where you can download the code to look at. Try to help yourslf by reading the information given to you.
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Originally posted by Nigel Browne:
I gave you a link to the FormattedTextDemo in the Java Tutorial, this is exactly what you want. Here is a link from where you can download the code to look at. Try to help yourslf by reading the information given to you.


So it does - that's handy!
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Ok I'm posting what I have done so far, this is how I need to go about doing it. Forgett the code I posted above.


package untitled3;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;

public class Frame1 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JTextArea jTextArea1 = new JTextArea();
JTextArea jTextArea2 = new JTextArea();
JTextArea jTextArea3 = new JTextArea();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JButton jButton1 = new JButton();
JTextArea jTextArea4 = new JTextArea();
JLabel jLabel1 = new JLabel();

/**Construct the frame*/
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
jTextArea1.setBackground(SystemColor.control);
jTextArea1.setBorder(BorderFactory.createEtchedBorder());
jTextArea1.setText("Enter Your Loan Amount Here:");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jTextArea2.setBackground(SystemColor.control);
jTextArea2.setBorder(BorderFactory.createEtchedBorder());
jTextArea2.setText("The Amount of Interest(APR%)");
jTextArea3.setBackground(SystemColor.control);
jTextArea3.setBorder(BorderFactory.createEtchedBorder());
jTextArea3.setText("Enter The Loan Time (years)");
jTextField1.setText("jTextField1");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField1_actionPerformed(e);
}
});
jTextField2.setText("jTextField2");
jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField2_actionPerformed(e);
}
});
jTextField3.setText("jTextField3");
jTextField3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField3_actionPerformed(e);
}
});
jButton1.setText("Calculate");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jTextArea4.setBackground(SystemColor.control);
jTextArea4.setBorder(BorderFactory.createEtchedBorder());
jTextArea4.setText("The Monthly Payment Is");
jLabel1.setBorder(BorderFactory.createEtchedBorder());
jLabel1.setText("jLabel1");
contentPane.add(jTextArea1, new XYConstraints(11, 17, 213, 42));
contentPane.add(jTextField1, new XYConstraints(226, 17, 153, 41));
contentPane.add(jTextArea2, new XYConstraints(11, 74, 214, 45));
contentPane.add(jTextField2, new XYConstraints(229, 74, 153, 43));
contentPane.add(jTextArea3, new XYConstraints(10, 139, 213, 47));
contentPane.add(jTextField3, new XYConstraints(229, 140, 154, 45));
contentPane.add(jButton1, new XYConstraints(100, 197, 171, 40));
contentPane.add(jTextArea4, new XYConstraints(8, 249, 199, 41));
contentPane.add(jLabel1, new XYConstraints(217, 247, 172, 42));
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void jTextField1_actionPerformed(ActionEvent e) {

}

void jTextField2_actionPerformed(ActionEvent e) {

}

void jTextField3_actionPerformed(ActionEvent e) {

}

void jButton1_actionPerformed(ActionEvent e) {

}

}
[ March 25, 2005: Message edited by: Michael Munro ]
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
I basically need the coding for the above 'voids':So it does this
jTextField1=Loan amount - �10,000
jTextField2 = Interest rate(APR%)- we'll say 10% to make it easy
jTextField3 = Loan Time in years, 2 years
jButton1 = (calculate) when pressed uses all the infomation in jTextFields1,2,and3 and gives the monthly payment in jLabel1.
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Any Ideas ?
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
Ok this is the code I have so far.
I just havn't got a clue what the code should be for the calculate button(jButton1). I'm sure there are some clever people out there who do.

package untitled3;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;

public class Frame1 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JTextArea jTextArea1 = new JTextArea();
JTextArea jTextArea2 = new JTextArea();
JTextArea jTextArea3 = new JTextArea();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JButton jButton1 = new JButton();
JTextArea jTextArea4 = new JTextArea();
JLabel jLabel1 = new JLabel();

/**Construct the frame*/
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
jTextArea1.setBackground(SystemColor.control);
jTextArea1.setBorder(BorderFactory.createEtchedBorder());
jTextArea1.setText("Enter Your Loan Amount Here:");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(450, 400));
this.setTitle("Frame Title");
jTextArea2.setBackground(SystemColor.control);
jTextArea2.setBorder(BorderFactory.createEtchedBorder());
jTextArea2.setText("The Amount of Interest(APR%)");
jTextArea3.setBackground(SystemColor.control);
jTextArea3.setBorder(BorderFactory.createEtchedBorder());
jTextArea3.setText("Enter The Loan Time (years)");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField1_actionPerformed(e);
}
});
jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField2_actionPerformed(e);
}
});
jTextField3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField3_actionPerformed(e);
}
});
jButton1.setText("Calculate");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jTextArea4.setBackground(SystemColor.control);
jTextArea4.setBorder(BorderFactory.createEtchedBorder());
jTextArea4.setText("The Monthly Payment Is");
jLabel1.setBorder(BorderFactory.createEtchedBorder());
jLabel1.setText("jLabel1");
contentPane.add(jTextArea1, new XYConstraints(11, 17, 213, 42));
contentPane.add(jTextField1, new XYConstraints(226, 17, 153, 41));
contentPane.add(jTextArea2, new XYConstraints(11, 74, 214, 45));
contentPane.add(jTextArea3, new XYConstraints(10, 139, 213, 47));
contentPane.add(jTextField3, new XYConstraints(229, 140, 154, 45));
contentPane.add(jButton1, new XYConstraints(100, 197, 171, 40));
contentPane.add(jTextArea4, new XYConstraints(8, 249, 199, 41));
contentPane.add(jLabel1, new XYConstraints(217, 247, 172, 42));
contentPane.add(jTextField2, new XYConstraints(230, 74, 153, 43));
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void jButton1_actionPerformed(ActionEvent e) {
String numberString = jTextField1 .getText();
double number = Double .parseDouble (numberString);

jLabel1.setText("");
}

}
Mike Meakin
Ranch Hand

Joined: Dec 30, 2004
Posts: 88
If anyone ever needs a source code for a Mortgage Calculator,done in an Application and Frame here its. Obviously you will need to complete the design in design view first to make it work but you should be able to work that out from the source code:


package untitled3;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.text.NumberFormat;

public class Frame1 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JTextArea jTextArea1 = new JTextArea();
JTextArea jTextArea2 = new JTextArea();
JTextArea jTextArea3 = new JTextArea();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JButton jButton1 = new JButton();
JTextArea jTextArea4 = new JTextArea();
JLabel jLabel1 = new JLabel();
NumberFormat nf;
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();

/**Construct the frame*/
public Frame1() {
nf = NumberFormat.getCurrencyInstance();
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
jTextArea1.setBackground(SystemColor.control);
jTextArea1.setBorder(BorderFactory.createEtchedBorder());
jTextArea1.setText("Enter Your Loan Amount Here:");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(450, 450));
this.setTitle("Frame Title");
jTextArea2.setBackground(SystemColor.control);
jTextArea2.setBorder(BorderFactory.createEtchedBorder());
jTextArea2.setText("The Amount of Interest(APR%)");
jTextArea3.setBackground(SystemColor.control);
jTextArea3.setBorder(BorderFactory.createEtchedBorder());
jTextArea3.setText("Enter The Loan Time (years)");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
jTextField3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
jButton1.setText("Calculate");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jTextArea4.setBackground(SystemColor.control);
jTextArea4.setBorder(BorderFactory.createEtchedBorder());
jTextArea4.setText("The Monthly Payment Is");
jLabel1.setBorder(BorderFactory.createEtchedBorder());
jLabel1.setText("jLabel1");
jButton2.setText("Close");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton3.setText("Again");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
});
contentPane.add(jTextArea1, new XYConstraints(11, 17, 213, 42));
contentPane.add(jTextField1, new XYConstraints(226, 17, 153, 41));
contentPane.add(jTextArea2, new XYConstraints(11, 74, 214, 45));
contentPane.add(jTextArea3, new XYConstraints(10, 139, 213, 47));
contentPane.add(jTextField3, new XYConstraints(229, 140, 154, 45));
contentPane.add(jButton1, new XYConstraints(100, 197, 171, 40));
contentPane.add(jTextArea4, new XYConstraints(8, 249, 199, 41));
contentPane.add(jLabel1, new XYConstraints(217, 247, 172, 42));
contentPane.add(jTextField2, new XYConstraints(230, 74, 153, 43));
contentPane.add(jButton2, new XYConstraints(221, 365, 183, 45));
contentPane.add(jButton3, new XYConstraints(16, 366, 179, 46));
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void jButton1_actionPerformed(ActionEvent e) {
double monthlyPayment = getMonthlyPayment();
jLabel1.setText(nf .format(monthlyPayment));

}
private double getMonthlyPayment() {
// read entries from each of the three JTextFields
String entry1 = jTextField1 .getText();
double loanAmount = Double.parseDouble(entry1);
String entry2 = jTextField2 .getText();
double interestRate = Double.parseDouble(entry2)/100;
String entry3 = jTextField3 .getText();
int term = Integer.parseInt(entry3);

int paymentsPerYear = 12;
int totalPayments = term * paymentsPerYear;
double x = 1.0 - Math.pow(1.0 + (interestRate/paymentsPerYear), -
totalPayments);
double divisor = paymentsPerYear * x;
double monthlyPayment = (loanAmount * interestRate) / divisor;
return monthlyPayment;

}

void jButton2_actionPerformed(ActionEvent e) {
this .dispose();
}

void jButton3_actionPerformed(ActionEvent e) {
jLabel1 .setText("");
jTextField1 .setText("");
jTextField2 .setText("");
jTextField3 .setText("");

}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mortgage Calculator -In Frames