Can someone point me in the right directions. I have to build on an existing mortgage program. I have to pull interest rates in from an external txt file and have them populate my array. The array will then be used in a combobox pull down. I don't know which class to use. File, File inFile?? below is the code I have to work with.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.text.DecimalFormat;
public class joeweek4 extends JPanel{
//global variable
private JComboBox IntRates;
private JLabel prinlb, rateLb, termLb, paymtLb;
private JButton calBtn, resetBtn;
private JTextField prinTxFld, rateTxFld, termTxFld, monthlyPayment;
private TextArea payAmortTA;
String currentInt;
public joeweek4()
{
String[] comboItems={"5.35","5.50","5.75"};
//creates labels
prinlb = new JLabel("Principal Amount:");
rateLb = new JLabel("Interest Rate:");
termLb = new JLabel("Term in years:");
paymtLb = new JLabel("Monthly Payment:");
//creates textfields
prinTxFld = new JTextField("",10);
rateTxFld = new JTextField("",5);
termTxFld = new JTextField("",5);
monthlyPayment = new JTextField("",10);
//combobox pulldown
IntRates = new JComboBox(comboItems);
IntRates.setEditable(true);
payAmortTA = new TextArea(20,50);
//create buttons
calBtn = new JButton("Calculate");
calBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
calculateMortgage();
}
});
resetBtn = new JButton("Reset");
resetBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
prinTxFld.setText("");
rateTxFld.setText("");
termTxFld.setText("");
monthlyPayment.setText("");
payAmortTA.setText ("");
}
});
//add labels and fields
add(prinlb);
add(prinTxFld);
add(rateLb);
add(IntRates);
add(rateTxFld);
add(termLb);
add(termTxFld);
add(paymtLb);
add(monthlyPayment);
add(payAmortTA);
add(calBtn);
add(resetBtn);
}
public void calculateMortgage()
{
//string array to accept which terms to use
rateTxFld.setText(IntRates.getSelectedItem().toString());
double principle=Double.parseDouble (prinTxFld.getText());
double dblrateTxFld=Double.parseDouble(rateTxFld.getText());
if (dblrateTxFld == 5.35)
termTxFld.setText("7");
else if (dblrateTxFld == 5.50)
termTxFld.setText("15");
else if (dblrateTxFld == 5.75)
termTxFld.setText("30");
int inttermTxFld=Integer.parseInt(termTxFld.getText());
// Formats the output of dollar figures
DecimalFormat two_decimal=new DecimalFormat("$0,000.00");
double Payment;
double InterestPaid;
double prinTxFldBal;
double temp;
int Months;
double intMonthlyRate = dblrateTxFld/100;
int MonthTerm = inttermTxFld * 12;
Months=MonthTerm;
//formulas
Payment=(principle * (intMonthlyRate/12)) / (1-1/Math.pow((1+intMonthlyRate/12), MonthTerm));
//converts monthly payment to decimal format
monthlyPayment.setText("" + (two_decimal.format(Payment)));
payAmortTA.append("Month No.\tMonthly Payment\t\tLoan Balance\t\tInterest Payment\n");
for (int counter=0; counter < MonthTerm; counter++)
{
temp=(1-1/Math.pow((1+intMonthlyRate/12), Months));
InterestPaid = Payment * temp;
principle = (principle - Payment + InterestPaid);
MonthTerm = MonthTerm --;
Months--;
payAmortTA.append((counter + 1) + "\t\t" + (two_decimal.format(Payment) + "\t\t" + (two_decimal.format(principle)
+ "\t\t" + (two_decimal.format(InterestPaid) + "\n"))));
}
}
private static void createAndShowGui()
{
JFrame frame = new JFrame("Mortgage Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
joeweek4 calculator = new joeweek4();
calculator.setOpaque(true);
frame.setContentPane(calculator);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
createAndShowGui();
}
}
);
}
}