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:
- 5 years at 5% - 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.
I know I need to put something like this in there, but I just don't know where to start making the adjustments:
input = new DataInputStream(new FileInputStream(existingFilename));
Here is my code for last weeks assignment and it works very well......
/** Mortgage Calculator Program
* Week 4 PRG421
*Change request #6
*Write the program in Java (with a graphical user interface) so that it will
* allow the user to select which way they want to calculate a mortgage:
* by input of the amount of the mortgage, the term of the mortgage, and the
* interest rate of the mortgage payment or by input of the amount of a mortgage
* and then select from a menu of mortgage loans:
- 5 years at 5%
- 7 years at 5.35%
- 15 years at 5.5 %
- 30 years at 5.75%
In either case, display the mortgage payment amount and then,
list the loan balance and interest paid for each payment over
the term of the loan. Allow the user to loop back and enter a new
amount and make a new selection, or quit. Insert comments in the program to document the program.
*
*Author: Stacey Estey
*Due September 13, 2010*/
//import needed packages
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;
//declare calculator class
public class Calculator extends JFrame
{
private String [] terms = {"5","7", "15", "30"};
private double [] rates = {0.05, 0.0535, 0.55, 0.0575};
private JLabel amountLabel = new JLabel ("Mortgage Amount:");
private JTextField amount = new JTextField();
private JLabel termLabel = new JLabel ("Term of Loan:");
private JTextField term = new JTextField();
private JLabel rateLabel = new JLabel("Rate: ('0.0525')");
private JTextField rate = new JTextField();
private JComboBox termList = new JComboBox(terms);
private JLabel payLabel = new JLabel("Monthly Payment:");
private JLabel payment = new JLabel();
private JButton calculate = new JButton("Calculate");
private JButton clear = new JButton("Clear");
private JButton exit = new JButton("Exit");
private JTextArea paymentSchedule = new JTextArea();
private JScrollPane schedulePane = new JScrollPane(paymentSchedule);
private Container cp = getContentPane();
public Calculator(String title)
{
super.setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//handle term list
termList.setSelectedIndex(0);
termList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
JComboBox cb = (JComboBox)e.getSource();
String termYear = (String)cb.getSelectedItem();
term.setText(termYear);
int index = 0;
switch (Integer.parseInt(termYear)){
case 5: index = 0; break;
case 7: index = 1; break;
case 15: index = 2; break;
case 30: index = 3; break;
}
rate.setText(rates[index] + "");
}
});
//handle the button events
calculate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
//calculate the payments.
double p = Double.parseDouble(amount.getText());
double r = Double.parseDouble(rate.getText()) / 12;
double n = Integer.parseInt(term.getText()) * 12;
double monthlyPayment = p * Math.pow (1 + r, n) * r / (Math.pow(1 + r, n)- 1);
DecimalFormat df = new DecimalFormat("$###,###.00");
payment.setText(df.format(monthlyPayment));
//calculate the loan information
double principal = p;
int month;
StringBuffer buffer = new StringBuffer();
buffer.append("Month\tAmount\tInterest\tBalance\n");
for (int i = 0; i < n; i++){
month = i + 1;
double interest = principal * r;
double balance = principal + interest - monthlyPayment;
buffer.append(month + "\t");
buffer.append(new String(df.format(principal)) + "\t");
buffer.append(new String(df.format(interest)) + "\t");
buffer.append(new String(df.format(balance)) + "\n");
principal = balance;
}
paymentSchedule.setText(buffer.toString());
} catch (Exception ex) {
System.out.println(ex);
}
}
});
clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
term.setText("");
rate.setText("");
amount.setText("");
payment.setText("");
paymentSchedule.setText("");
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(1);
}
});
JPanel calcScreen = new JPanel();
calcScreen.setLayout(new GridLayout(10,4));
calcScreen.add(amountLabel);
calcScreen.add(amount);
calcScreen.add(new Label("Select Mortgage Terms from List or enter manually"));
calcScreen.add(termList);
calcScreen.add(termLabel);
calcScreen.add(term);
calcScreen.add(rateLabel);
calcScreen.add(rate);
calcScreen.add(payLabel);
calcScreen.add(payment);
JPanel buttons = new JPanel();
buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
buttons.add(calculate);
buttons.add(clear);
buttons.add(exit);
JPanel calc = new JPanel();
calc.setLayout(new BoxLayout(calc, BoxLayout.Y_AXIS));
calc.add(calcScreen);
calc.add(buttons);
cp.add(BorderLayout.NORTH, calc);
cp.add(BorderLayout.CENTER, schedulePane);
}
public static void main(String[] args)
{
Calculator frame = new Calculator("Mortgage Calculator");
frame.setSize(400,600);
Frame.setVisible(true);
}
}

New Topic/Question
Reply




MultiQuote







|