Thanks again. I'm really sorry I'm not understanding this. There is just too much on my plate. I'm having a hard time concentrating. I'm praying and it looks like at least I have my Java helpers. Thanks again.
I will work on this all day and all day tomorrow to get it right.
Well I did think the term and termMonths variables were not declared so I added them. But it still is not calculating the monthly payments and putting it in the window.
I cleaned up the code.
/* Mortgage Calculator Change Request #6 in Service Request SR-mf-003. Insert comments in the program to
document the program. Attach a design flow chart to the source code of the program.
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: 7 year at 5.35%, 15 year at 5.5 %, and 30 year 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.
University of Phoenix
PRG421 Java Programming II
Course Date 2/24/09 End Date 3/30/09
Instructor Yining Li */
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.math.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class MortgageCalculatorCR62 {
double amount;
double term;
double payments;
double amortize;
double interestRateMonths;
double monthlyInterestPayment;
double monthlyPrincipalPayment;
int termMonths;
double balance;
int Labels;
// Instance Variables
JLabel amountLbl, termLbl, IntrateLbl, paymentsLbl, amortizeLb1;
JTextField amountTf, termTf, intrateTf, paymentsTf, amortizeTf;
JButton calcBtn;
JButton clearButton;
JButton exitButton;
JFrame frame;
JComboBox cOption;
public MortgageCalculatorCR62() {
System.out.println("Mortgage Calculator Change Request #6.");
String Labels[] = { "7 Years at 5.35%", "15 years at 5.5%", "30 years at 5.75%",};
amountLbl = new JLabel(" Loan Amount In Whole Dollars");
termLbl = new JLabel(" Terms In Years");
IntrateLbl = new JLabel(" Interest Rate - Decimal Format (0.000)");
paymentsLbl = new JLabel(" Monthly Payment Amount");
amountTf = new JTextField();
JComboBox comboBox1 = new JComboBox(Labels);
cOption = new JComboBox(Labels);
termTf = new JTextField();
intrateTf = new JTextField();
amortizeTf = new JTextField();
amortizeLb1 = new JLabel("Amortization");
paymentsTf = new JTextField();
calcBtn = new JButton("Calculate");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");
frame = new JFrame("Mortgage Calculator CR#6");
frame.setLayout(new GridLayout(16,16));
frame.add(amountLbl);
frame.add(amountTf);
frame.add(comboBox1);
/* Remove these frames when action listener is working
frame.add(termLbl);
frame.add(termTf);
frame.add(IntrateLbl);
frame.add(intrateTf);
*/
frame.add(paymentsLbl);
frame.add(paymentsTf);
// Added amortize textfield and label
frame.add(amortizeLb1);
frame.add(amortizeTf);
frame.add(calcBtn);
frame.add(clearButton);
frame.add(exitButton);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clearButton.addActionListener(new Handler());
calcBtn.addActionListener(new Handler());
exitButton.addActionListener(new Handler());
frame.setLocation(300, 300);
frame.pack();
frame.setVisible(true);
}
private class Handler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Object command = event.getSource();
if(command == calcBtn)
{
calculate();
}
if(command == clearButton)
{
amountTf.setText(null);
termTf.setText(null);
intrateTf.setText(null);
paymentsTf.setText(null);
amortizeTf.setText(null);
}
if(command == exitButton)
{
System.exit(0);
}
}
}
public static void main(String[] args) {
new MortgageCalculatorCR62();
}
private void calculate() {
DecimalFormat decimalPlaces=new DecimalFormat("0,000.00");
String Labels = (String) cOption.getSelectedItem();
if (Labels.equals("7 years at 5.35%"))
{
double amount = Double.parseDouble(amountTf.getText());
int years = 7;
int months = (years * 12);
double interest = .0535;
double termMonths = months;
double term = 7;
double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
paymentsTf.setText("" + decimalPlaces.format(payments));
}
if (Labels.equals("15 years at 5.5%"))
{
double amount = Double.parseDouble(amountTf.getText());
int years = 15;
int months = (years * 12);
double interest = .055;
double termMonths = months;
double term = 15;
double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
paymentsTf.setText("" + decimalPlaces.format(payments));
}
if (Labels.equals("30 years at 5.75%"))
{
double amount = Double.parseDouble(amountTf.getText());
int years = 30;
int months = (years * 12);
double interest = .0575;
double termMonths = months;
double term = 15;
double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
paymentsTf.setText("" + decimalPlaces.format(payments));
}
/* This is the area for the amortization process */
// displays monthly mortgage payment resulting from above calculation
System.out.println("\n\nBased on the above criteria, " +
"your monthly payment will be: " +
"$" + decimalPlaces.format(payments));
// formula(s) to calculate monthly interest and principal payments
monthlyInterestPayment = (amount * interestRateMonths);
monthlyPrincipalPayment = (payments - monthlyInterestPayment);
// start while loop
while (termMonths > 0);
// {
// information to display
System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
"\t\t$" + decimalPlaces.format(monthlyInterestPayment) +
"\t\t$" + decimalPlaces.format(amount));
// decrement months
termMonths--;
// calculate interest and principal payments
monthlyInterestPayment = (amount * interestRateMonths);
monthlyPrincipalPayment = (payments - monthlyInterestPayment);
balance = (payments - monthlyPrincipalPayment);
System.out.println(balance);
/**/
/* Put amortization into a Jtextarea */
//payments - principal show each payment
}}
This post has been edited by AdamSpeight2008: 08 June 2010 - 12:55 PM
Reason for edit:: Removed personal information

New Topic/Question
Reply




MultiQuote





|