Welcome to Dream.In.Code
Become a Java Expert!

Join 149,804 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,831 people online right now. Registration is fast and FREE... Join Now!




Graphing

 
Reply to this topicStart new topic

Graphing, Graphing the amortization in a mortgage program

retmsg21
15 Feb, 2007 - 07:21 PM
Post #1

New D.I.C Head
*

Joined: 10 Dec, 2006
Posts: 16


My Contributions
I am attempting to graph my amortization results and I am a little lost on how to accomplish this. any help is appreciated.

//Imports packages
import java.awt.*; //Used create to buttons
import java.awt.event.*; //Used create an event
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import java.util.*;
import java.io.*;
import javax.swing.*;

import java.text.DecimalFormat; //Used to import decimal format for money

//Action listener
public class MortgageCalculator extends JPanel implements ActionListener, ItemListener
{

//Variables
protected JComboBox MortIntRate;
protected JLabel lblprincipal, lblinterestRate, lblTerm, lblmonthlyPayment;
protected JButton calculate, Reset, exit;//New button exit


protected JTextField principal, interestRate, Term, monthlyPayment, mortgagecalculater;
protected TextArea MortPayResults;
protected Hashtable ht = null;

public MortgageCalculator()

{

//String[] comboItems = {"5.35", "5.50", "5.75", "Custom"};//Interest rates used in program creating a combo box
ht = new Hashtable();

try
{
RandomAccessFile bis = new RandomAccessFile(new File("c:\\rates.txt"), "r");
for(String str = bis.readLine(); str != null; str = bis.readLine())
{
String[] strArr = str.split(":");
ht.put(strArr[0], strArr[1]);
}

}catch(Exception ex)
{
System.out.println("Not able to read Rates.txt file" );
return;
}

String[] comboItems = new String[ht.size()];

Enumeration en = ht.keys();
for(int i = 0; en.hasMoreElements(); i++)
{
comboItems[i] = (String) en.nextElement();
}

//Label are defined here,used to creates labels and fields

lblprincipal = new JLabel("Principal Amount:");
principal = new JTextField("", 4);
lblinterestRate = new JLabel("Interest Rate:");
interestRate = new JTextField("", 3);
interestRate.setEditable(false);
lblTerm = new JLabel("Term:");
Term = new JTextField("", 3);
Term.setEditable(false);
lblmonthlyPayment = new JLabel("Monthly Payment:");
monthlyPayment = new JTextField("", 5);
MortIntRate = new JComboBox(comboItems);
MortIntRate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setTermValue();
}
} );
mortIntPrevSel = (String) MortIntRate.getSelectedItem();
interestRate.setText((String) MortIntRate.getSelectedItem());
MortIntRate.addItemListener(this);

MortPayResults = new TextArea(20, 65);

//Create action buttons
calculate = new JButton("Calculate"); //Create button used to calculate mortgage payment
calculate.setActionCommand("GO");

Reset = new JButton("Reset"); //Create button used to reset data from the fields
Reset.setActionCommand("Reset");

exit = new JButton("Exit");//new
exit.setActionCommand("Exit");//new



//Activate buttons
calculate.addActionListener(this);
Reset.addActionListener(this);
exit.addActionListener(this);//new

//Add labels and fields

add(lblprincipal);
add(principal);
add(lblinterestRate);
add(interestRate);
add(MortIntRate);
add(lblTerm);
add(Term);
add(lblmonthlyPayment);
add(monthlyPayment);
add(MortPayResults);
add(calculate);
add(Reset);
add(exit);//new

}
public void actionPerformed(ActionEvent e)// Action event occurs here
{
if ("GO".equals(e.getActionCommand()))
{
CalculateMortgage();
}
else
{
principal.setText("");
interestRate.setText("");
Term.setText("");
monthlyPayment.setText("");
MortPayResults.setText("");

}
}

public void setTermValue()
{
String termValue = (String) ht.get((String)interestRate.getText());
Term.setText(termValue);
}

public void CalculateMortgage()
{
// interestRate.setText("" + (String) MortIntRate.getSelectedItem());
double dblprincipal = Double.parseDouble(principal.getText());
double dblInterestRate = Double.parseDouble(interestRate.getText());

//Handle the condition.
/*if (dblInterestRate == 5.35)
Term.setText("7");
else if (dblInterestRate == 5.50)
Term.setText("15");
else if (dblInterestRate == 5.75)
Term.setText("30");*/

//Currency is set here
int intTerm = Integer.parseInt(Term.getText());
DecimalFormat money = new DecimalFormat("$0.00");

double MonthlyPayment;
double InterestPaid;
double PrincipalBal;
double temp;
int Months;
double MIntRate = dblInterestRate / 100;
int MTerms = intTerm * 12;
Months = MTerms;

//Formulas
MonthlyPayment = (dblprincipal * (MIntRate / 12)) / (1 - 1 / Math.pow((1 + MIntRate / 12), MTerms));

//Converts Monthly Payment to decimal format
monthlyPayment.setText("" + (money.format(MonthlyPayment)));

MortPayResults.append("Month No.\tMonthly Payment\t\tLoan Balance\t\tInterest Payment\n");

for (int counter = 0; counter < MTerms; counter++)
{
temp = (1 - 1 / Math.pow((1 + MIntRate / 12), Months));
InterestPaid = MonthlyPayment * temp;
dblprincipal = (dblprincipal - MonthlyPayment + InterestPaid);
MTerms = MTerms--;
Months--;

MortPayResults.append((counter + 1) + "\t\t" + (money.format(MonthlyPayment) + "\t\t" + (money.format(dblprincipal) + "\t\t" + (money.format(InterestPaid) + "\n"))));
MortPayResults.setCaretPosition(0);
}

}
//GUI is created in this section
private static void createAndShowGui()
{
JFrame frame = new JFrame("Multiple Mortgage Calculator");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Frame is setup

MortgageCalculator myCalculator = new MortgageCalculator();
myCalculator.setOpaque(true);
frame.setContentPane(myCalculator);

frame.pack();
frame.setVisible(true);


}

public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGui();
}
}

);

}

//Get interest rates here
public void itemStateChanged(ItemEvent e)
{
if(e.getSource() instanceof JComboBox)
{
JComboBox cb = (JComboBox) e.getSource();
String str = (String) cb.getSelectedItem();
if(str.equals("Custom"))
{
interestRate.setEditable(true);
interestRate.setText("");
}
else
{
interestRate.setEditable(false);
interestRate.setText(str);
}

}
}

private String mortIntPrevSel;

}



User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Graphing
20 Feb, 2007 - 01:26 PM
Post #2

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
It seems a lot for one class. Anyway, I would use a graphing API that is already made, and/or get involved in that project before I would write a graph by myself. Have a look at this thead
http://www.dreamincode.net/forums/showtopic23931.htm
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 07:55AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month