Week2.Java
Essay by 24 • November 7, 2010 • 1,627 Words (7 Pages) • 1,114 Views
/*
Week Two Individual Assignment - POS 407
Requirements: 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, the term of the
mortgage, and the interest rate of the mortgage. Allow the user
to loop back and enter new data or quit.
Please insert comments in the program to document the program.
Resulting functionality: Program executes with baseline values that the user can edit
multiple times. Tabbing or pressing Enter after user's entry recalculates payment.
Tool tips included to assist in user understanding of required inputs.
Incorrect entries (alpha characters) are ignored.
Exit via window close.
Date Development Start: November 7, 2006
Date Development End: November 11, 2006
Revision 4
*/
//Import Libraries
import java.awt.*;
import javax.swing.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.text.*;
public class week2mp extends JPanel
implements PropertyChangeListener {
//initial or default Values for the form fields
private double mortgage = 200000;//Default mortgage amount
private double interestrate = 5.75; //Default interest rate
private int mortgageTerm = 30; //Default mortgage term in years
//Set field labels
private JLabel mortgageLabel;
private JLabel interestrateLabel;
private JLabel mortgageTermLabel;
private JLabel paymentLabel;
//Set values for the labels
private static String mortgageString = "Mortgage Amount: $"; //Set label for mortgage amount
private static String interestrateString = "Interest Rate: "; //Set label for interest rate
private static String mortgageTermString = "Mortgage Term: "; //Set label for mortgage Term
private static String paymentString = "Your Monthly Payment: "; //Set label for mortgage payment
//JFormattedTextField supports formatting arbitrary values, as well as retrieving an object
//once the user has edited the text.
private JFormattedTextField mortgageField;//Mortgage Amount
private JFormattedTextField interestrateField;//interest rate
private JFormattedTextField mortgageTermField;// Mortgage term
private JFormattedTextField paymentField;//Monthly payment
//NumberFormat is the base class for all number formats. Numberformat provides the interface for formatting and parsing numbers.
//NumberFormat also provides methods for determining which locales have number formats, and what their names are.
private NumberFormat mortgageFormat;//Mortgage amount
private NumberFormat percentFormat;//Interest Rate
private NumberFormat paymentFormat;//MOnthyl payment
public week2mp() {
super(new BorderLayout());
setUpFormats();
double payment = computePayment(mortgage,
interestrate,
mortgageTerm);
//Create the labels.
mortgageLabel = new JLabel(mortgageString);
interestrateLabel = new JLabel(interestrateString);
mortgageTermLabel = new JLabel(mortgageTermString);
paymentLabel = new JLabel(paymentString);
//Create the text fields and set them up.
//Mortgage
mortgageField = new JFormattedTextField(mortgageFormat);
mortgageField.setValue(new Double(mortgage));
mortgageField.setColumns(10);
mortgageField.setToolTipText("Please enter the amount borrowed without a dollar sign");//tip appears on mouse over
mortgageField.addPropertyChangeListener("value", this);//checks for user input
//Interest Rate
interestrateField = new JFormattedTextField(percentFormat);
interestrateField.setValue(new Double(interestrate));
interestrateField.setColumns(5);
interestrateField.setToolTipText("Please enter the interest rate of the mortgage without the percentage symbol");//tip appears on mouse over
interestrateField.addPropertyChangeListener("value", this);//checks for user input
...
...