I am trying to retrieve data from a text file and place it in a text field.
I am not sure were I am going.
Problem is around 522 to 531?
/**
*Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from
* user input of the amount of the mortgage and the user's selection from a menu of available mortgage loans:
- 7 years at 5.35%
- 15 years at 5.5%
- 30 years at 5.75%
Use an array for the mortgage data for the different loans. Read the interest rates to fill the array from a sequential file.
Display the mortgage payment amount followed by the loan balance and interest paid for each payment over the term of the loan.
Add graphics in the form of a chart. Allow the user to loop back and enter a new amount and make a new selection or quit.
Please insert comments in the program to document the program.
*
*/
import java.io.*;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.text.*;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
Calculater form = new Calculater();
form.setVisible(true);
}
}
public class Calculater extends javax.swing.JFrame {
// Variables
String termYearString="0", interestString="0", newLoan1 = "0",newLoan2="0",
newLoan3="0", newSelection1 = "0";
double principal = 0.0;
int termsYear = 0;
double term = 0;
double interest = 0.0;
double monthlyPayment = 0.0;
double rate =0;
double temp = 0;
double InterestPaid = 0;
double termMonths = 0;
double payment = 0;
double loanBalance = 0;
double monthlyInterest =0;
double payPrincipal =0;
double newPrincipal =0;
int month = 0;
int totalMonth =0;
/** Creates new form Calculater */
public Calculater() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane3 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
principalText = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
calButton = new javax.swing.JButton();
resetButon = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
termText = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
interestRateText = new javax.swing.JTextField();
exitButton = new javax.swing.JButton();
preMortComboBox = new javax.swing.JComboBox();
jLabel8 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel6 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
monthlyPaymentArea = new javax.swing.JTextArea();
jLabel7 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
amSchedArea = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
seveYearMort = new javax.swing.JMenuItem();
fifYearMort = new javax.swing.JMenuItem();
thirYearMort = new javax.swing.JMenuItem();
rateFromFile = new javax.swing.JMenuItem();
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane3.setViewportView(jList1);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Mortgage Calculator");
setBackground(new java.awt.Color(204, 204, 255));
jPanel1.setBackground(new java.awt.Color(153, 153, 255));
jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel1.setText("Principal of Mortgage");
principalText.setText("0.00");
principalText.setToolTipText("Enter Principal Here");
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel4.setText("Pick a Pre-defined Mortgage");
calButton.setText("Calculate");
calButton.setToolTipText("Click To Find Your Answer");
calButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calButtonActionPerformed(evt);
}
});
resetButon.setText("Reset");
resetButon.setToolTipText("Click To Reset The Mortgage Calculator");
resetButon.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resetButonActionPerformed(evt);
}
});
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel5.setText("Or Enter Your Own Information");
jLabel5.setToolTipText("You can also edit the information after picking a pre-defined mortgage.");
jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
jLabel2.setText("Term of Mortgage (years)");
termText.setText("0");
termText.setToolTipText("Enter Number of Years");
jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
jLabel3.setText("Intrest Rate of Mortgage");
interestRateText.setText("0.00");
interestRateText.setToolTipText("Enter Interest Rate.");
exitButton.setText("Exit");
exitButton.setToolTipText("Click to cloase window");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});
preMortComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "7 Years at 5.35%", "15 Years at 5.5%", "30 Years at 5.75%" }));
preMortComboBox.setToolTipText("If You Choose, You Can Pick A Pre-defined Mortgage From This List.");
preMortComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
preMortComboBoxActionPerformed(evt);
}
});
jLabel8.setFont(new java.awt.Font("Tahoma", 0, 10));
jLabel8.setText("Edit the boxes below to make the pre-defined mortgage information suite your needs.");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(205, 205, 205)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(principalText, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(preMortComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jLabel1))
.addGap(92, 92, 92))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4)
.addContainerGap(301, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(termText, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(22, 22, 22)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(interestRateText, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(46, 46, 46))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(calButton)
.addGap(109, 109, 109)
.addComponent(resetButon)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 145, Short.MAX_VALUE)
.addComponent(exitButton))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(29, 29, 29)))
.addGap(32, 32, 32))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(principalText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(preMortComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(33, 33, 33)
.addComponent(jLabel5)
.addGap(11, 11, 11)
.addComponent(jLabel8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(termText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(interestRateText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(calButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(resetButon)
.addComponent(exitButton))
.addContainerGap())
);
jPanel2.setBackground(new java.awt.Color(0, 204, 204));
jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18));
jLabel6.setText("Results");
monthlyPaymentArea.setColumns(20);
monthlyPaymentArea.setRows(5);
jScrollPane1.setViewportView(monthlyPaymentArea);
jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel7.setText("Amortization Schedule");
amSchedArea.setColumns(20);
amSchedArea.setRows(5);
jScrollPane2.setViewportView(amSchedArea);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE)
.addComponent(jLabel7)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(304, Short.MAX_VALUE))
);
jMenu1.setBackground(new java.awt.Color(255, 255, 255));
jMenu1.setText("Pick A Pre-defined Mortgage");
jMenu1.setToolTipText("Pick A Pre-defined Mortgage");
seveYearMort.setText("7 Years at 5.35%");
seveYearMort.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
seveYearMortActionPerformed(evt);
}
});
jMenu1.add(seveYearMort);
fifYearMort.setText("15 Years at 5.5%");
fifYearMort.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fifYearMortActionPerformed(evt);
}
});
jMenu1.add(fifYearMort);
thirYearMort.setText("30 Years at 5.75%");
thirYearMort.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
thirYearMortActionPerformed(evt);
}
});
jMenu1.add(thirYearMort);
rateFromFile.setText("Rate From File");
rateFromFile.setToolTipText("This is a special rate that may change at any time.");
rateFromFile.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rateFromFileActionPerformed(evt);
}
});
jMenu1.add(rateFromFile);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void calButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calButtonActionPerformed
DecimalFormat formatter = new DecimalFormat("#0.00"); // To display both cents columns
try {
String ir = principalText.getText();
principal = Double.valueOf(ir.trim()).doubleValue();
}
catch (Exception e) {
JOptionPane.showMessageDialog(this, "Bad Number Entered into principal Field",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
termsYear = Integer.parseInt(
this.termText.getText());
}
catch (Exception e) {
JOptionPane.showMessageDialog(this, "Bad Number Entered into Term Field",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
String ir = interestRateText.getText();
interest = Double.valueOf(ir.trim()).doubleValue();
}
catch (Exception e) {
JOptionPane.showMessageDialog(this, "Bad Number Entered into interest Rate Field",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
// This formula will calculate the payment information.
term = termsYear * 12;
rate = interest / 1200;
monthlyPayment = (rate + rate/(Math.pow(1+rate, term) -1)) * principal; // Formula provided by YI MI
monthlyPaymentArea.setText(
"\t The loan amount is $" +formatter.format(principal)+""
+ "\n\t The term of the loan is "+ termsYear +" years"
+ "\n\t The interest rate applied to the loan is " +formatter.format(interest)+" %"
+ "\n\t The monthly payment is $" +formatter.format(monthlyPayment) );
amSchedArea.append(" Month Principal Payment Interest Paid New Principal\n");
while (principal > 1) {
temp=(1-1/Math.pow((1+rate), month));
monthlyInterest = principal * rate;
payPrincipal = monthlyPayment - monthlyInterest;
newPrincipal = principal - payPrincipal;
principal = newPrincipal;
totalMonth = month + 1;
month = totalMonth;
amSchedArea.append(" "+(month ) + "\t"+ (formatter.format(payPrincipal)+ "\t " + (formatter.format(monthlyInterest)+ "\t " + (formatter.format(principal) + "\n"))));
}
}
// Reset Button
private void resetButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButonActionPerformed
principalText.setText("0.00");
termText.setText("0");
interestRateText.setText("0.0");
monthlyPaymentArea.setText ("");
amSchedArea.setText("");
preMortComboBox.setSelectedIndex(0);
}
// Exit Button
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
System.exit(0);
}
private void preMortComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_preMortComboBoxActionPerformed
String value = preMortComboBox.getSelectedItem().toString();
if (value.contentEquals("7 Years at 5.35%")) {
termYearString="7";
interestString="5.35";
termText.setText(termYearString);
interestRateText.setText(interestString);
} else {
if (value.contentEquals("15 Years at 5.5%")) {
termYearString="15";
interestString="5.5";
termText.setText(termYearString);
interestRateText.setText(interestString);
} else {
if (value.contentEquals("30 Years at 5.75%")) {
termYearString="30";
interestString="5.75";
termText.setText(termYearString);
interestRateText.setText(interestString);
}
}
}
}
private void seveYearMortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_seveYearMortActionPerformed
termYearString="7";
interestString="5.35";
termText.setText(termYearString);
interestRateText.setText(interestString);
}
private void fifYearMortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fifYearMortActionPerformed
termYearString="15";
interestString="5.5";
termText.setText(termYearString);
interestRateText.setText(interestString);
}
private void thirYearMortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_thirYearMortActionPerformed
termYearString="30";
interestString="5.75";
termText.setText(termYearString);
interestRateText.setText(interestString);
}
private void rateFromFileActionPerformed(java.awt.event.ActionEvent evt) {
//create file object
File file = new File("rate.txt");
int ch;
StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;
try
{
fin = new FileInputStream(file);
while( (ch = fin.read()) != -1)
strContent.append((char)ch);
fin.close();
}
catch(FileNotFoundException e)
{
JOptionPane.showMessageDialog(this, "File " + file.getAbsolutePath() + " could not be found on filesystem",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(this, "Exception while reading the file" + ioe,
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
/* ++++++++++++++++++++++++++++++++++++++++++++
* So How do I get teh data from the file into the interestRateText area?
*/
interestString=(strContent;
termYearString="30";
termText.setText(termYearString);
interestRateText.setText(interestString);
/* ++++++++++++++++++++++++++++++++++++++++++++++++*/
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculater().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextArea amSchedArea;
private javax.swing.JButton calButton;
private javax.swing.JButton exitButton;
private javax.swing.JMenuItem fifYearMort;
private javax.swing.JTextField interestRateText;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JList jList1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextArea monthlyPaymentArea;
private javax.swing.JComboBox preMortComboBox;
private javax.swing.JTextField principalText;
private javax.swing.JMenuItem rateFromFile;
private javax.swing.JButton resetButon;
private javax.swing.JMenuItem seveYearMort;
private javax.swing.JTextField termText;
private javax.swing.JMenuItem thirYearMort;
// End of variables declaration//GEN-END:variables
}
Attached File(s)
-
rate.TXT (4bytes)
Number of downloads: 90

New Topic/Question
Reply




MultiQuote




|