Here is code for a GUI Mortgage program I am working on for a class assignment. There are two things I need help with.

(1) I need to populate the array with data from a sequential file. This means I would place the first three elements of my current array into a text file, but I need to keep my fourth element intact because that is driven by user input via the interface.

(2) I need to be able to save the amortization schedule, that gets displayed in the TexArea, to a text file. I have added a save button to the GUI interface that I would like to use for saving this information.

Any assistance is greatly appreciated.

//Import java packages
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.text.NumberFormat;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;


public class GUIMortgageCalculatorWk5 extends JFrame implements ActionListener
{
// Labels
JLabel AmountLabel = new JLabel( "Mortgage Amount:$ " ); // Declares Mortgage Amount Label
JLabel PaymentLabel = new JLabel( "Monthly Payment: " ); // Declares Monthly Payment Label
JLabel InterestLabel = new JLabel( "Interest Rate %: " ); // Declares Interest Rate Label
JLabel TermofLoanLabel = new JLabel( "TermofLoan of Loan: " ); // Declares TermofLoan of Loan Label

// Text Fields
JTextField mortgageAmount = new JTextField(7); // Declares Mortgage Amount Text Field
JTextField MonthlyPayment = new JTextField(7); // Declares Monthly Payment Text Field
JTextField InterestRate = new JTextField(7); // Declares Interest Rate Text Field
JTextField TermofLoan = new JTextField(7); // Declares TermofLoan of Loan Text Field


// Buttons
JButton Loan1 = new JButton( "7 years at 5.35%" ); // Declares 1st Mortgage TermofLoan and Interest Rate
JButton Loan2 = new JButton( "15 years at 5.50%" ); // Declares 2nd Mortgage TermofLoan and Interest Rate
JButton Loan3 = new JButton( "30 years at 5.75%" ); // Declares 3rd Mortgage TermofLoan and Interest Rate
JButton ExitButton = new JButton( "Exit" ); // Declares Exit Button
JButton ClearButton = new JButton( "Clear" ); // Declares New Calculation Button
JButton CalculateButton = new JButton( "Calculate" ); // Declares New Calculation Button
JButton SaveButton = new JButton( "Save" ); // Declares New Save Button
JButton ChartButton = new JButton( "Show Chart" ); // Declares New Chart Button

// Text Area and Scroll
JTextArea MortgageTable = new JTextArea(25,40); // Declares Mortgage Table Area
JScrollPane scroll = new JScrollPane(MortgageTable); // Declares ScrollPane and puts the Mortgage Table inside


GUIMortgageCalculatorWk5()//Method
{
//Frame, Panel, and Layout set up
super("GUI Mortgage Calculator");
setSize(600, 400);
setLocation(0, 0);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

//Setup container and contents
Container grid = getContentPane();
grid.setLayout(new GridLayout(4,0,5,5));
pane.add(grid);
pane.add(scroll);
grid.add(AmountLabel);
grid.add(mortgageAmount);
grid.add(InterestLabel);
grid.add(InterestRate);
grid.add(TermofLoanLabel);
grid.add(TermofLoan);
grid.add(PaymentLabel);
grid.add(MonthlyPayment);
grid.add(Loan1);
grid.add(Loan2);
grid.add(Loan3);
grid.add(SaveButton);
grid.add(CalculateButton);
grid.add(ClearButton);
grid.add(ExitButton);
grid.add(ChartButton);
MonthlyPayment.setEditable(false);
setContentPane(pane);
setVisible(true);

//Adds Action Listeners
ExitButton.addActionListener(this);
ClearButton.addActionListener(this);
Loan1.addActionListener(this);
Loan2.addActionListener(this);
Loan3.addActionListener(this);
mortgageAmount.addActionListener(this);
InterestRate.addActionListener(this);
TermofLoan.addActionListener(this);
MonthlyPayment.addActionListener(this);
CalculateButton.addActionListener(this);


}//End GUIMortgageCalculatorWk5 method


public void actionPerformed(ActionEvent e)
{
Object command = e.getSource();
if
(
command == ExitButton) //sets ExitButton
System.exit(0); // Exits from exit button

int loanTermofLoan = 0; // Declares loanTermofLoan

if (command == Loan1) //Activates the 1st Loan Button
{
loanTermofLoan = 0; //Sets 1st value of Array
TermofLoan.setText("0");
InterestRate.setText("0");
}

if (command == Loan2) //Activates the 2nd Loan Button
{
loanTermofLoan = 1; //Sets 2nd value of Array
TermofLoan.setText("0");
InterestRate.setText("0");
}

if (command == Loan3) //Activates the 3rd Loan Button
{
loanTermofLoan = 2; // Sets 3rd value of Array
TermofLoan.setText("0");
InterestRate.setText("0");
}

if (command == CalculateButton ) //Activates the Calculate Button for manual entries
{
loanTermofLoan = 3; // Sets 4rd value of Array
}

double t1 = Double.parseDouble(TermofLoan.getText());
double r1 = Double.parseDouble(InterestRate.getText());
double [][] loans = { {7, 5.35}, {15, 5.50}, {30, 5.75}, {t1, r1} }; // Array Data for Calculation

double mortgage = 0; // Declares and Initializes mortgage
mortgage = Double.parseDouble(mortgageAmount.getText()); //Parse text to double data type for calculation
double interestRate = loans [loanTermofLoan][1]; // Sets interestRate amount
double intRate = (interestRate / 100) / 12; // Calculates Interst Rate
double loanTermofLoanMonths = loans [loanTermofLoan] [0]; // Calculates Loan TermofLoan in Months
int months = (int)loanTermofLoanMonths * 12; // Converts Loan TermofLoan to Months
double interestRateMonthly = (intRate / 12); // Converts Annual interest rate to monthly interst rate
double payment = mortgage * intRate / (1 - (Math.pow(1/(1 + intRate), months))); // Calculation for Monthly payment
double remainingLoanBalance = mortgage; // Sets Reamaining Balance
double MonthlyPaymentInterest = 0; // holds current interest payment
double MonthlyPaymentPrincipal = 0; // holds current principal payment

// Number formatter to format output in table
NumberFormat CurrencyFormatter = NumberFormat.getCurrencyInstance(Locale.US); //Display amounts in US Dollars
MonthlyPayment.setText(CurrencyFormatter.format(payment));
MortgageTable.setText("Month\tPrincipal\tInterest\tEnding Balance\n" + // Formats TextArea Header
"----------\t------------\t-------------\t----------------------\n"); // Formats TextArea Header cont.

for (;months > 0 ; months -- )
{
//Append loop for mortgage detail in the text area
MonthlyPaymentInterest = (remainingLoanBalance * intRate);//Monthly Payment Toward Interest
MonthlyPaymentPrincipal = (payment - MonthlyPaymentInterest);//Monthly Payment Toward Principal
remainingLoanBalance = (remainingLoanBalance - MonthlyPaymentPrincipal);//Remaining loan Balance

MortgageTable.setCaret (new DefaultCaret()); // Sets Scroll position to the top left corner
MortgageTable.append(String.valueOf(months) + "\t" +
CurrencyFormatter.format(MonthlyPaymentPrincipal) + "\t" +
CurrencyFormatter.format(MonthlyPaymentInterest) + "\t" +
CurrencyFormatter.format(remainingLoanBalance) + "\n");//
}

//New calculation button
if(command == ClearButton)
{
mortgageAmount.setText(null); //clears mortgage amount fields
MonthlyPayment.setText(null); //clears monthly payment fields
InterestRate.setText(null); //clears interest field
TermofLoan.setText(null); //clears rate field
MortgageTable.setText(null); //clears mortgage table
}

} // End actionPerformed method


public static void main(String[] args)
{
new GUIMortgageCalculatorWk5();
}

}//End Class