Advertisement
Guest User

Untitled

a guest
Apr 26th, 2010
1,642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.77 KB | None | 0 0
  1. /*PRG 421 WEEK 2
  2.  * Write the program in Java (with a graphical user interface)
  3.  * and have it calculate and display the mortgage payment amount from
  4.  * user input of the amount of the mortgage, the term of the mortgage,
  5.  * and the interest rate of the mortgage.
  6.  * Allow the user to loop back and enter new data or quit.
  7.  * Please insert comments in the program to document the program
  8.  */
  9.  
  10. package ray8;
  11.  
  12. /**
  13.  *
  14.  * @author Ray Corry
  15.  */
  16. import java.awt.*;
  17. import java.awt.event.*;
  18. import javax.swing.*;
  19. import javax.swing.event.*;
  20. import javax.swing.text.*;
  21. import java.beans.PropertyChangeListener;
  22. import java.beans.PropertyChangeEvent;
  23. import java.text.*;
  24.  
  25.  
  26. public class Main extends JPanel implements PropertyChangeListener
  27. {
  28.     //sets initial values in the  field boxes
  29.     private double amount = 200000;
  30.     private double rate = 7.5;  
  31.     private int numPeriods = 30;
  32.  
  33.     // field names
  34.     private JLabel amountLabel;
  35.     private JLabel rateLabel;
  36.     private JLabel numPeriodsLabel;
  37.     private JLabel paymentLabel;
  38.  
  39.     //sets the names of the labels to strings
  40.     private static String amountString = "Enter Loan Amount: ";
  41.     private static String rateString = "Enter a number for the annual percentage rate : ";
  42.     private static String numPeriodsString = "Enter the term of the loan in years  ";
  43.     private static String paymentString = "Monthly Payment: ";
  44.  
  45.     //User input fields
  46.     private JFormattedTextField amountField;
  47.     private JFormattedTextField rateField;
  48.     private JFormattedTextField numPeriodsField;
  49.     private JFormattedTextField paymentField;
  50.  
  51.     //Formats to format and parse numbers
  52.     private NumberFormat amountFormat;
  53.     private NumberFormat percentFormat;
  54.     private NumberFormat paymentFormat;
  55.  
  56.    public void  main ()
  57.    {
  58.         new BorderLayout();
  59.         setUpFormats();
  60.         double payment = computePayment(amount,
  61.                                         rate,
  62.                                         numPeriods);
  63.  
  64.         //Create the labels.
  65.         amountLabel = new JLabel(amountString);
  66.         rateLabel = new JLabel(rateString);
  67.         numPeriodsLabel = new JLabel(numPeriodsString);
  68.         paymentLabel = new JLabel(paymentString);
  69.  
  70.         //Create the text fields and set them up.
  71.         amountField = new JFormattedTextField(amountFormat);
  72.         amountField.setValue(new Double(amount));
  73.         amountField.setColumns(10);
  74.         amountField.addPropertyChangeListener("value", this);
  75.  
  76.         rateField = new JFormattedTextField(percentFormat);
  77.         rateField.setValue(new Double(rate));
  78.         rateField.setColumns(10);
  79.         rateField.addPropertyChangeListener("value", this);
  80.  
  81.         numPeriodsField = new JFormattedTextField();
  82.         numPeriodsField.setValue(new Integer(numPeriods));
  83.         numPeriodsField.setColumns(10);
  84.         numPeriodsField.addPropertyChangeListener("value", this);
  85.  
  86.         paymentField = new JFormattedTextField(paymentFormat);
  87.         paymentField.setValue(new Double(payment));
  88.         paymentField.setColumns(10);
  89.         paymentField.setEditable(false);
  90.         paymentField.setForeground(Color.red);
  91.  
  92.         //Tell accessibility tools about label and textfield pairs.
  93.         amountLabel.setLabelFor(amountField);
  94.         rateLabel.setLabelFor(rateField);
  95.         numPeriodsLabel.setLabelFor(numPeriodsField);
  96.         paymentLabel.setLabelFor(paymentField);
  97.  
  98.         //Lay out the labels in a panel.
  99.         JPanel labelPane = new JPanel(new GridLayout(0,1));
  100.         labelPane.add(amountLabel);
  101.         labelPane.add(rateLabel);
  102.         labelPane.add(numPeriodsLabel);
  103.         labelPane.add(paymentLabel);
  104.  
  105.         //Layout the text fields in a panel.
  106.         JPanel fieldPane = new JPanel(new GridLayout(0,1));
  107.         fieldPane.add(amountField);
  108.         fieldPane.add(rateField);
  109.         fieldPane.add(numPeriodsField);
  110.         fieldPane.add(paymentField);
  111.  
  112.         //Put the panels in this panel, labels on left,
  113.         //text fields on right.
  114.         setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  115.         add(labelPane, BorderLayout.CENTER);
  116.         add(fieldPane, BorderLayout.LINE_END);
  117.     }
  118.  
  119.     /** Called when a field's "value" property changes. */
  120.     public void propertyChange(PropertyChangeEvent e) {
  121.         Object source = e.getSource();
  122.         if (source == amountField) {
  123.             amount = ((Number)amountField.getValue()).doubleValue();
  124.         } else if (source == rateField) {
  125.             rate = ((Number)rateField.getValue()).doubleValue();
  126.         } else if (source == numPeriodsField) {
  127.             numPeriods = ((Number)numPeriodsField.getValue()).intValue();
  128.         }
  129.  
  130.         double payment = computePayment(amount, rate, numPeriods);
  131.         paymentField.setValue(new Double(payment));
  132.     }
  133.  
  134.     /**
  135.      * Create the GUI and show it.  For thread safety,
  136.      * this method should be invoked from the
  137.      * event dispatch thread.
  138.      */
  139.     private static void createAndShowGUI() {
  140.         //Create and set up the window.
  141.         JFrame frame = new JFrame("Mortgage Calculator that killed Ray");
  142.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  143.  
  144.         //Add contents to the window.
  145.         frame.add (new FormattedTextFieldDemo());
  146.  
  147.         //Display the window.
  148.         frame.pack();
  149.         frame.setVisible(true);
  150.     }
  151.  
  152.     public static void main(String[] args) {
  153.         //Schedule a job for the event dispatch thread:
  154.         //creating and showing this application's GUI.
  155.         SwingUtilities.invokeLater(new Runnable() {
  156.             public void run() {
  157.                 //Turn off metal's use of bold fonts
  158.             UIManager.put("swing.boldMetal", Boolean.FALSE);
  159.                 createAndShowGUI();
  160.             }
  161.         });
  162.     }
  163.  
  164.     //Compute the monthly payment based on the loan amount,
  165.     //APR, and length of loan.
  166.     double computePayment(double loanAmt, double rate, int numPeriods) {
  167.         double I, partial1, denominator, answer;
  168.  
  169.         numPeriods *= 12;        //get number of months
  170.         if (rate > 0.01) {
  171.             I = rate / 100.0 / 12.0;         //get monthly rate from annual
  172.             partial1 = Math.pow((1 + I), (0.0 - numPeriods));
  173.             denominator = (1 - partial1) / I;
  174.         } else { //rate ~= 0
  175.             denominator = numPeriods;
  176.         }
  177.  
  178.         answer = (-1 * loanAmt) / denominator;
  179.         return answer;
  180.     }
  181.  
  182.     //Create and set up number formats. These objects also
  183.     //parse numbers input by user.
  184.     private void setUpFormats() {
  185.         amountFormat = NumberFormat.getNumberInstance();
  186.  
  187.         percentFormat = NumberFormat.getNumberInstance();
  188.         percentFormat.setMinimumFractionDigits(3);
  189.  
  190.         paymentFormat = NumberFormat.getCurrencyInstance();
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement