By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,800 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,800 IT Pros & Developers. It's quick & easy.

Buttons not performing correctly

P: 5
My Mortgage Calculator program worked fine, until I added more functions. I added new fields for the new functions and changed my calculations for a calculate button, but it does not work. The rest of the program is fine. I commented out my else if exit command, because that would close my program when I hit the calculate button.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2.     import java.awt.event.*;
  3.     import javax.swing.*;
  4.     import java.text.DecimalFormat;
  5.     import java.text.NumberFormat;
  6.     public class MortgageCalculatorWeek3 {
  7.  
  8.         public static void main(String[] args) { // GUI JFrame Functions for Visibility and exit tool bar.
  9.             JFrame window = new Calculator ();
  10.             window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11.             window.setVisible(true);
  12.         }
  13.     }
  14.         class Calculator extends JFrame implements ActionListener{ // Shows that the class Calculator extends the JFrame.        
  15.  
  16.             private static final long serialVersionUID = 1L; // Added to get rid of an error
  17.             private JButton Calculate, Calculate7, Calculate15, Calculate30, Clear, Exit; // Declares the buttons 
  18.             private JLabel Payment, Principle, labelTitle, labelInstructions,AmortizationTable, Interest, Duration, LabelOr; // declares the JLables
  19.             private JTextField fieldPayment, fieldLoanAmount, fieldInterest, fieldDuration; // Declares the JTextfields 
  20.             private JTextArea areaAmortTable; // Declares the Amortization table text area. The area supports more lines than the JTextfield.         
  21.  
  22.             //Declare variables and the loan array and assigns values. 
  23.             double InterestPaid,PrinciplePaid,Balance,monthlyPay,loanAmount;
  24.             int[] loanTerm = {7,15,30}; // Declares Loan terms
  25.             double[] intRate = {5.35,5.50,5.75}; // Declares interest rates
  26.             double loanAmt = loanAmount; // Declares area for user input for principle amount.
  27.             private DecimalFormat decimalPlaces; // sets decimal places for first calculate button
  28.             private DecimalFormat decimalPlaces2; // sets decimal places for second calculate button.
  29.             private DecimalFormat decimalPlaces3; // sets decimal places for third calculate button.
  30.             private DecimalFormat decimalPlaces4; 
  31.             private Dimension screenSize; // sets screen size.             
  32.             int Months;
  33.             double Inter;
  34.  
  35.  
  36.             public void actionPerformed (ActionEvent e)
  37.             {
  38.                 //Provides looping functions for the buttons pressed.
  39.                 if ("Calculate".equals(e.getActionCommand()))
  40.                 {
  41.                     setDecimalPlaces4(new DecimalFormat("0.00"));
  42.                     NumberFormat currency = NumberFormat.getCurrencyInstance();
  43.                     double paymentAmount = Calculate();
  44.  
  45.  
  46.                     fieldPayment.setText("" + (currency.format(paymentAmount)));         
  47.                     areaAmortTable.append ("Payment  \t Balance After Payment \t  Interest Paid\n");
  48.  
  49.                     loanAmt = Double.parseDouble(fieldLoanAmount.getText());
  50.                     for (int x = 1; x <=(Inter*12); x++){
  51.                         Balance=loanAmt;
  52.                         InterestPaid=(((Inter/100.0)/12.0)*Balance);
  53.                         PrinciplePaid=(paymentAmount-InterestPaid);
  54.                         loanAmt=(Balance-PrinciplePaid);
  55.                         areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
  56.                     }
  57.                     if (Balance <= 0) //ending statement, balance at $0.00
  58.                         areaAmortTable.append ("\tRemaining balance = $0.00");                        
  59.                 }
  60.                     {
  61.                 }
  62.  
  63.                    if ("Calculate7".equals(e.getActionCommand()))
  64.                 {
  65.                     setDecimalPlaces(new DecimalFormat("0.00"));
  66.                     NumberFormat currency = NumberFormat.getCurrencyInstance();                     
  67.                     double paymentAmount = CalculatePayment7();            
  68.  
  69.                     fieldPayment.setText("" + (currency.format(paymentAmount)));         
  70.                     areaAmortTable.append ("Payment  \t Balance After Payment \t  Interest Paid\n"); // sets fields for loan amortization table 
  71.  
  72.                     loanAmt = Double.parseDouble(fieldLoanAmount.getText()); // allows user to input principle amount.                             
  73.                     for (int x = 1; x <=(loanTerm[0]*12); x++) //Loop through all monthly payments for each loan
  74.                     {                                   
  75.                             Balance=loanAmt; //Set Balance to Loan Amount                
  76.  
  77.                             //Calculations on monthly payment                             
  78.                             InterestPaid=(((intRate[0]/100.0)/12.0)*Balance); //Monthly Interest paid
  79.                             PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle
  80.                             loanAmt=(Balance-PrinciplePaid); //loan balance after payment                 
  81.  
  82.                             areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
  83.                             }                 
  84.  
  85.                     if (Balance <= 0) //ending statement, balance at $0.00
  86.                     {
  87.                         areaAmortTable.append ("\tRemaining balance = $0.00");                        
  88.                     }
  89.                }         
  90.                 else if ("Calculate15".equals(e.getActionCommand())) // tells the first button what actions to perform.
  91.                 {
  92.                     setDecimalPlaces2(new DecimalFormat("0.00"));
  93.                     NumberFormat currency = NumberFormat.getCurrencyInstance();                     
  94.                     double paymentAmount = CalculatePayment15 ();                     
  95.                     fieldPayment.setText("" + (currency.format(paymentAmount))); 
  96.  
  97.                     areaAmortTable.append ("Payment  \t Balance After Payment \t  Interest Paid\n");
  98.                     loanAmt = Double.parseDouble(fieldLoanAmount.getText());                     
  99.                     for (int x = 1; x <=(loanTerm[1]*12); x++) 
  100.                     {         
  101.                             Balance = loanAmt;                         
  102.                             //Calculations on monthly payment
  103.                             InterestPaid=((intRate[1]/100/12)*Balance); //Monthly Interest paid
  104.                             PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle
  105.                             loanAmt=((Balance-PrinciplePaid)); //loan balance after payment         
  106.                             areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
  107.                     }         
  108.                     if (loanAmt <= 0) //ending statement, balance at $0.00
  109.                     {
  110.                         areaAmortTable.append ("\tRemaining balance = $0.00");                        
  111.                     }         
  112.                 }         
  113.                 else if ("Calculate30".equals(e.getActionCommand()))
  114.                 {
  115.                     setDecimalPlaces3(new DecimalFormat("0.00"));
  116.                     NumberFormat currency = NumberFormat.getCurrencyInstance();                     
  117.                     double paymentAmount = CalculatePayment30 ();                     
  118.                     fieldPayment.setText("" + (currency.format(paymentAmount)));         
  119.                     areaAmortTable.append ("Payment \t Balance After Payment \t  Interest Paid\n");
  120.                     loanAmt = Double.parseDouble(fieldLoanAmount.getText());             
  121.  
  122.                     for (int x = 1; x <=(loanTerm[2]*12); x++) 
  123.                     {
  124.                         Balance = loanAmt;         
  125.                         //Calculations on monthly payment
  126.                         InterestPaid=((intRate[2]/100/12)*Balance); //Monthly Interest paid
  127.                         PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle
  128.                         loanAmt=((Balance-PrinciplePaid)); //loan balance after payment         
  129.                         areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
  130.                     }                     
  131.                     if (loanAmt <= 0) //ending statement, balance at $0.00
  132.                     {
  133.                         areaAmortTable.append ("\tRemaining balance = $0.00");                     
  134.  
  135.                     }         
  136.                 }
  137.  
  138.                 else if ("Clear".equals(e.getActionCommand()))
  139.                 {
  140.                     ClearFields ();
  141.                 }
  142.  
  143.                 else
  144.                 {                    
  145.                     //System.exit(0); //End and close application.
  146.                }
  147.             }
  148.  
  149.             public double CalculatePayment7 ()
  150.             {    
  151.  
  152.                 try //Check for number entry
  153.                 {
  154.                  //Perform payment calculations if input is valid
  155.  
  156.                     fieldPayment.setText ("");
  157.                     double Payment =(loanAmount() * (intRate[0]/100/12)) / (1 - Math.pow(1/(1 + (intRate[0]/100/12)),loanTerm[0]*12));
  158.                     return Payment;
  159.                 }
  160.  
  161.                 catch (NumberFormatException event)
  162.                 {
  163.                     //Display error message for invalid characters and beep at user.
  164.                      // beeps when there is an error
  165.                     JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "ERROR", JOptionPane.ERROR_MESSAGE);
  166.                     System.out.print ( "\007" );
  167.                     return 0;
  168.                 }
  169.             }
  170.  
  171.             public double CalculatePayment15 ()
  172.             {
  173.  
  174.                 try //Check for valid numeric input
  175.                 {
  176.                      //Display error message for invalid characters and beep at user.
  177.                     fieldPayment.setText ("");
  178.                     double Payment =(loanAmount() * (intRate[1]/100/12)) / (1 - Math.pow(1/(1 + (intRate[1]/100/12)),loanTerm[1]*12));
  179.                     return Payment;
  180.                 }
  181.  
  182.                 catch (NumberFormatException event)
  183.                 {
  184.                      // Display error message for invalid characters and beep at user.
  185.                     System.out.print ( "\007" ); // beeps when there is an error
  186.                     JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "ERROR", JOptionPane.ERROR_MESSAGE);
  187.                     return 0;
  188.                 }
  189.             }
  190.  
  191.             public double CalculatePayment30 ()
  192.             {
  193.                 //Check for valid numeric input
  194.                 try
  195.                 {
  196.                     //Perform payment calculations if input is valid
  197.                     fieldPayment.setText ("");
  198.                     double Payment =(loanAmount() * (intRate[2]/100/12)) / (1 - Math.pow(1/(1 + (intRate[2]/100/12)),loanTerm[2]*12));
  199.                     return Payment;
  200.                 }
  201.  
  202.                 catch (NumberFormatException event)
  203.                 {
  204.                     // Display error message for invalid characters and beep at user.
  205.                     System.out.print ( "\007" ); // beeps when there is an error
  206.                     JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "Error", JOptionPane.ERROR_MESSAGE);
  207.                     return 0;
  208.                 }
  209.  
  210.             }
  211.             public double Calculate(){
  212.                 try{
  213.  
  214.                 fieldPayment.setText("");
  215.  
  216.                 double Payment =(loanAmount() * (Inter/100/12)) / (1 - Math.pow(1/(1 + (Inter/100/12)),Months*12));
  217.                 return Payment;
  218.  
  219.                 }
  220.  
  221.                 catch (NumberFormatException event){
  222.                     JOptionPane.showMessageDialog(null, "Invalid entry, please enter numbers only", "Error", JOptionPane.ERROR_MESSAGE);
  223.                     return 0;
  224.                 }
  225.             }
  226.  
  227.  
  228.             public void ClearFields ()
  229.             {
  230.                 //clears all fields for new input.
  231.                 fieldPayment.setText ("");
  232.                 fieldLoanAmount.setText ("");
  233.                 areaAmortTable.setText ("");
  234.                 fieldInterest.setText ("");
  235.                 fieldDuration.setText (""); 
  236.  
  237.             }
  238.  
  239.             public Calculator ()
  240.             {
  241.                 //Sets up buttons for GUI
  242.                 Calculate = new JButton ("Calculate");
  243.                 Calculate.setActionCommand ("Calculate");
  244.                 Calculate7 = new JButton ("7 Year, 5.35%");
  245.                 Calculate7.setActionCommand ("Calculate7");
  246.                 Calculate15 = new JButton ("15 Year, 5.50%");
  247.                 Calculate15.setActionCommand ("Calculate15");
  248.                 Calculate30 = new JButton ("30 Year, 5.75%");
  249.                 Calculate30.setActionCommand ("Calculate30");
  250.                 Clear = new JButton ("Clear ");
  251.                 Clear.setActionCommand ("Clear");
  252.                 Exit = new JButton ("Exit");
  253.                 Exit.setActionCommand ("Exit");
  254.  
  255.                 //Set up labels and field sizes for GUI
  256.                 labelTitle = new JLabel ("Mortgage Amortization Calculator");
  257.                 labelInstructions = new JLabel ("Enter the Principle, Interest rate, and Loan Duration.");
  258.                 LabelOr = new JLabel (" Or Pick From These Loan Options After Entering Loan Amount");
  259.                 Payment = new JLabel ("Monthly Payment:");
  260.                 Principle = new JLabel ("Amount of Loan:");
  261.                 fieldPayment = new JTextField ("", 12);
  262.                 fieldLoanAmount = new JTextField ("", 10);
  263.                 AmortizationTable = new JLabel ("Amortization Table");
  264.                 areaAmortTable = new JTextArea (10, 300);
  265.                 Interest = new JLabel ("Interest Rate:");
  266.                 Duration = new JLabel ("Duration:");
  267.                 fieldInterest = new JTextField ("", 12);
  268.                 fieldDuration = new JTextField ("", 10);
  269.  
  270.                 JScrollPane scrollPane = new JScrollPane (areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  271.                 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  272.  
  273.                 //Set up listeners for each button.
  274.                 Calculate.addActionListener(this);
  275.                 Calculate7.addActionListener(this);
  276.                 Calculate15.addActionListener(this);
  277.                 Calculate30.addActionListener(this);
  278.                 Clear.addActionListener(this);
  279.                 Exit.addActionListener(this);
  280.  
  281.                 //Constructs GUI. 
  282.                 JPanel mine = new JPanel();
  283.                 mine.setLayout (null);
  284.  
  285.                 mine.add (labelTitle);
  286.                 labelTitle.setBounds (110, 30, 700, 15);
  287.  
  288.                 mine.add (labelInstructions);
  289.                 labelInstructions.setBounds (80, 70, 450, 15);
  290.  
  291.                 mine.add (Principle);
  292.                 Principle.setBounds (130, 110, 100, 25);
  293.  
  294.                 mine.add(Interest);
  295.                 Interest.setBounds (130 , 140, 120, 25);
  296.  
  297.                 mine.add(Calculate);
  298.                 Calculate.setBounds(350, 145, 115, 25);
  299.  
  300.                 mine.add(Duration);
  301.                 Duration.setBounds(130, 170, 120, 25);
  302.  
  303.                 mine.add (fieldLoanAmount);
  304.                 fieldLoanAmount.setBounds (240, 110, 100, 25);
  305.  
  306.                 mine.add(fieldInterest);
  307.                 fieldInterest.setBounds (240, 140, 100, 25);
  308.  
  309.                 mine.add(fieldDuration);
  310.                 fieldDuration.setBounds (240, 170, 100, 25);
  311.  
  312.                 mine.add(LabelOr);
  313.                 LabelOr.setBounds(80, 220, 400, 25);
  314.  
  315.                 mine.add (Calculate7);
  316.                 Calculate7.setBounds (40, 260, 125, 30);
  317.  
  318.                 mine.add (Calculate15);
  319.                 Calculate15.setBounds (180, 260, 125, 30);
  320.  
  321.                 mine.add (Calculate30);
  322.                 Calculate30.setBounds (320, 260, 125, 30);
  323.  
  324.                 mine.add (Payment);
  325.                 Payment.setBounds (130, 300, 100, 25);
  326.  
  327.                 mine.add (fieldPayment);
  328.                 fieldPayment.setBounds (240, 300, 100, 25);
  329.                 fieldPayment.setEditable (false);
  330.  
  331.                 mine.add (AmortizationTable);
  332.                 AmortizationTable.setBounds (180, 330, 300, 25);
  333.  
  334.                  mine.add (scrollPane);
  335.                  scrollPane.setBounds (50, 350, 380, 260);
  336.                  areaAmortTable.setEditable(false);
  337.  
  338.                  mine.add (Clear);
  339.                  Clear.setBounds (110, 620, 125, 30);
  340.  
  341.                 mine.add (Exit);
  342.                 Exit.setBounds (250, 620, 125, 30);
  343.  
  344.                 this.setContentPane(mine);
  345.                 this.pack();
  346.                 this.setTitle("Mortgage Amortization Calculator");
  347.  
  348.                 setScreenSize(Toolkit.getDefaultToolkit().getScreenSize());
  349.                 setSize(500, 700);
  350.             }
  351.             public double loanAmount ()
  352.             {
  353.                 double loanAmount = Double.parseDouble(fieldLoanAmount.getText());
  354.                 return loanAmount;
  355.             }
  356.  
  357.             public DecimalFormat getDecimalPlaces() {
  358.                 return decimalPlaces;
  359.             }
  360.  
  361.             public void setDecimalPlaces(DecimalFormat decimalPlaces) {
  362.                 this.decimalPlaces = decimalPlaces;
  363.             }
  364.  
  365.             public DecimalFormat getDecimalPlaces2() {
  366.                 return decimalPlaces2;
  367.             }
  368.  
  369.             public void setDecimalPlaces2(DecimalFormat decimalPlaces2) {
  370.                 this.decimalPlaces2 = decimalPlaces2;
  371.             }
  372.  
  373.             public DecimalFormat getDecimalPlaces4() {
  374.                 return decimalPlaces4;
  375.             }
  376.  
  377.             public void setDecimalPlaces4(DecimalFormat decimalPlaces4) {
  378.                 this.decimalPlaces4 = decimalPlaces4;
  379.             }
  380.  
  381.             public Dimension getScreenSize() {
  382.                 return screenSize;
  383.             }
  384.  
  385.             public void setScreenSize(Dimension screenSize) {
  386.                 this.screenSize = screenSize;
  387.             }
  388.  
  389.             public DecimalFormat getDecimalPlaces3() {
  390.                 return decimalPlaces3;
  391.             }
  392.  
  393.             public void setDecimalPlaces3(DecimalFormat decimalPlaces3) {
  394.                 this.decimalPlaces3 = decimalPlaces3;
  395.             }
  396.         }
  397. // end of program
  398.  
Feb 11 '12 #1

✓ answered by neeraj0708

Hi Eric,

Replace the for loop
for (int x = 1; x <=(Inter*12); x++){
inside Calculate event(at line number 51) as
for (int x = 1; x <=(Months*12); x++){
it will work fine.

Share this Question
Share on Google+
9 Replies


P: 21
can u show us the code.. it will be more helpfull to understand.
Feb 13 '12 #2

P: 5
Here is the code, so far. Most of the program works, but I added in 2 new fields (Interest, and Duration) and a calculate button. The program is supposed to give the user a choice on how to calculate the mortgage.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2.     import java.awt.event.*;
  3.     import javax.swing.*;
  4.  
  5.     import java.text.DecimalFormat;
  6.     import java.text.NumberFormat;
  7.  
  8.     public class MortgageCalculatorWeek3 {
  9.  
  10.         public static void main(String[] args) { // GUI JFrame Functions for Visibility and exit tool bar.
  11.             JFrame window = new Calculator ();
  12.             window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.             window.setVisible(true);
  14.         }
  15.     }
  16.         class Calculator extends JFrame implements ActionListener{ // Shows that the class Calculator extends the JFrame.        
  17.  
  18.             private static final long serialVersionUID = 1L; // Added to get rid of an error
  19.             private JButton Calculate, Calculate7, Calculate15, Calculate30, Clear, Exit; // Declares the buttons 
  20.             private JLabel Payment, Principle, labelTitle, labelInstructions,AmortizationTable, Interest, Duration, LabelOr; // declares the JLables
  21.             private JTextField fieldPayment, fieldLoanAmount, fieldInterest, fieldDuration; // Declares the JTextfields 
  22.             private JTextArea areaAmortTable; // Declares the Amortization table text area. The area supports more lines than the JTextfield.         
  23.  
  24.             //Declare variables and the loan array and assigns values. 
  25.             double InterestPaid,PrinciplePaid,Balance,monthlyPay,loanAmount;
  26.             int[] loanTerm = {7,15,30}; // Declares Loan terms
  27.             double[] intRate = {5.35,5.50,5.75}; // Declares interest rates
  28.             double loanAmt = loanAmount; // Declares area for user input for principle amount.
  29.             private DecimalFormat decimalPlaces; // sets decimal places for first calculate button
  30.             private DecimalFormat decimalPlaces2; // sets decimal places for second calculate button.
  31.             private DecimalFormat decimalPlaces3; // sets decimal places for third calculate button.
  32.             private DecimalFormat decimalPlaces4; 
  33.             private Dimension screenSize; // sets screen size.             
  34.             double Inter, Months;
  35.  
  36.  
  37.             @Override
  38.             public void actionPerformed (ActionEvent e)
  39.             {
  40.  
  41.                     if ("Calculate".equals(e.getActionCommand()))
  42.                 {
  43.                     setDecimalPlaces4(new DecimalFormat("0.00"));
  44.                     NumberFormat currency = NumberFormat.getCurrencyInstance();
  45.                     double paymentAmount = CalculatePayment();
  46.  
  47.  
  48.                     fieldPayment.setText("" + (currency.format(paymentAmount)));         
  49.                     areaAmortTable.append ("Payment  \t Balance After Payment \t  Interest Paid\n");
  50.  
  51.                     loanAmt = Double.parseDouble(fieldLoanAmount.getText());
  52.                     Months = Double.parseDouble(fieldDuration.getText());
  53.                     Inter = Double.parseDouble(fieldInterest.getText());
  54.  
  55.  
  56.                     for (int x = 1; x <=(Inter*12); x++){
  57.                         Balance=loanAmt;
  58.                         InterestPaid=(((Inter/100.0)/12.0)*Balance);
  59.                         PrinciplePaid=(paymentAmount-InterestPaid);
  60.                         loanAmt=(Balance-PrinciplePaid);
  61.                         areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
  62.                     }
  63.                     if (Balance <= 0) //ending statement, balance at $0.00
  64.                         areaAmortTable.append ("\tRemaining balance = $0.00");
  65.  
  66.                 }
  67.                   //Provides looping functions for the buttons pressed.
  68.                    if ("Calculate7".equals(e.getActionCommand()))
  69.                 {
  70.                     setDecimalPlaces(new DecimalFormat("0.00"));
  71.                     NumberFormat currency = NumberFormat.getCurrencyInstance();                     
  72.                     double paymentAmount = CalculatePayment7();            
  73.  
  74.                     fieldPayment.setText("" + (currency.format(paymentAmount)));         
  75.                     areaAmortTable.append ("Payment  \t Balance After Payment \t  Interest Paid\n"); // sets fields for loan amortization table 
  76.  
  77.                     loanAmt = Double.parseDouble(fieldLoanAmount.getText()); // allows user to input principle amount.                             
  78.                     for (int x = 1; x <=(loanTerm[0]*12); x++) //Loop through all monthly payments for each loan
  79.                     {                                   
  80.                             Balance=loanAmt; //Set Balance to Loan Amount                
  81.  
  82.                             //Calculations on monthly payment                             
  83.                             InterestPaid=(((intRate[0]/100.0)/12.0)*Balance); //Monthly Interest paid
  84.                             PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle
  85.                             loanAmt=(Balance-PrinciplePaid); //loan balance after payment                 
  86.  
  87.                             areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
  88.                             }                 
  89.  
  90.                     if (Balance <= 0) //ending statement, balance at $0.00
  91.                     {
  92.                         areaAmortTable.append ("\tRemaining balance = $0.00");                        
  93.                     }
  94.                }         
  95.                 else if ("Calculate15".equals(e.getActionCommand())) // tells the first button what actions to perform.
  96.                 {
  97.                     setDecimalPlaces2(new DecimalFormat("0.00"));
  98.                     NumberFormat currency = NumberFormat.getCurrencyInstance();                     
  99.                     double paymentAmount = CalculatePayment15 ();                     
  100.                     fieldPayment.setText("" + (currency.format(paymentAmount))); 
  101.  
  102.                     areaAmortTable.append ("Payment  \t Balance After Payment \t  Interest Paid\n");
  103.                     loanAmt = Double.parseDouble(fieldLoanAmount.getText());                     
  104.                     for (int x = 1; x <=(loanTerm[1]*12); x++) 
  105.                     {         
  106.                             Balance = loanAmt;                         
  107.                             //Calculations on monthly payment
  108.                             InterestPaid=((intRate[1]/100/12)*Balance); //Monthly Interest paid
  109.                             PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle
  110.                             loanAmt=((Balance-PrinciplePaid)); //loan balance after payment         
  111.                             areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
  112.                     }         
  113.                     if (loanAmt <= 0) //ending statement, balance at $0.00
  114.                     {
  115.                         areaAmortTable.append ("\tRemaining balance = $0.00");                        
  116.                     }         
  117.                 }         
  118.                 else if ("Calculate30".equals(e.getActionCommand()))
  119.                 {
  120.                     setDecimalPlaces3(new DecimalFormat("0.00"));
  121.                     NumberFormat currency = NumberFormat.getCurrencyInstance();                     
  122.                     double paymentAmount = CalculatePayment30 ();                     
  123.                     fieldPayment.setText("" + (currency.format(paymentAmount)));         
  124.                     areaAmortTable.append ("Payment \t Balance After Payment \t  Interest Paid\n");
  125.                     loanAmt = Double.parseDouble(fieldLoanAmount.getText());             
  126.  
  127.                     for (int x = 1; x <=(loanTerm[2]*12); x++) 
  128.                     {
  129.                         Balance = loanAmt;         
  130.                         //Calculations on monthly payment
  131.                         InterestPaid=((intRate[2]/100/12)*Balance); //Monthly Interest paid
  132.                         PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle
  133.                         loanAmt=((Balance-PrinciplePaid)); //loan balance after payment         
  134.                         areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
  135.                     }                     
  136.                     if (loanAmt <= 0) //ending statement, balance at $0.00
  137.                     {
  138.                         areaAmortTable.append ("\tRemaining balance = $0.00");                     
  139.  
  140.                     }         
  141.                 }
  142.  
  143.                 else if ("Clear".equals(e.getActionCommand()))
  144.                 {
  145.                     ClearFields ();
  146.                 }
  147.  
  148.                 else
  149.                 {                    
  150.                     System.exit(0); //End and close application.
  151.                }
  152.             }
  153.  
  154.             public double CalculatePayment7 ()
  155.             {    
  156.  
  157.                 try //Check for number entry
  158.                 {
  159.                  //Perform payment calculations if input is valid
  160.  
  161.                     fieldPayment.setText ("");
  162.                     double Payment =(loanAmount() * (intRate[0]/100/12)) / (1 - Math.pow(1/(1 + (intRate[0]/100/12)),loanTerm[0]*12));
  163.                     return Payment;
  164.                 }
  165.  
  166.                 catch (NumberFormatException event)
  167.                 {
  168.                     //Display error message for invalid characters and beep at user.
  169.                      // beeps when there is an error
  170.                     JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "ERROR", JOptionPane.ERROR_MESSAGE);
  171.                     System.out.print ( "\007" );
  172.                     return 0;
  173.                 }
  174.             }
  175.  
  176.             public double CalculatePayment15 ()
  177.             {
  178.  
  179.                 try //Check for valid numeric input
  180.                 {
  181.                      //Display error message for invalid characters and beep at user.
  182.                     fieldPayment.setText ("");
  183.                     double Payment =(loanAmount() * (intRate[1]/100/12)) / (1 - Math.pow(1/(1 + (intRate[1]/100/12)),loanTerm[1]*12));
  184.                     return Payment;
  185.                 }
  186.  
  187.                 catch (NumberFormatException event)
  188.                 {
  189.                      // Display error message for invalid characters and beep at user.
  190.                     System.out.print ( "\007" ); // beeps when there is an error
  191.                     JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "ERROR", JOptionPane.ERROR_MESSAGE);
  192.                     return 0;
  193.                 }
  194.             }
  195.  
  196.             public double CalculatePayment30 ()
  197.             {
  198.                 //Check for valid numeric input
  199.                 try
  200.                 {
  201.                     //Perform payment calculations if input is valid
  202.                     fieldPayment.setText ("");
  203.                     double Payment =(loanAmount() * (intRate[2]/100/12)) / (1 - Math.pow(1/(1 + (intRate[2]/100/12)),loanTerm[2]*12));
  204.                     return Payment;
  205.                 }
  206.  
  207.                 catch (NumberFormatException event)
  208.                 {
  209.                     // Display error message for invalid characters and beep at user.
  210.                     System.out.print ( "\007" ); // beeps when there is an error
  211.                     JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "Error", JOptionPane.ERROR_MESSAGE);
  212.                     return 0;
  213.                 }
  214.  
  215.             }
  216.             public double CalculatePayment()
  217.             {
  218.                 try
  219.                 {
  220.  
  221.                 fieldPayment.setText("");
  222.  
  223.                 double PaymentAmount =(loanAmount() * (Inter/100/12)) / (1 - Math.pow(1/(1 + (Inter/100/12)), Months * 12));
  224.                  return PaymentAmount;
  225.  
  226.                 }
  227.  
  228.                 catch (NumberFormatException event)
  229.             {
  230.                     JOptionPane.showMessageDialog(null, "Invalid entry, please enter numbers only", "Error", JOptionPane.ERROR_MESSAGE);
  231.                        return 0;
  232.                 }
  233.             }
  234.  
  235.  
  236.             public void ClearFields ()
  237.             {
  238.                 //clears all fields for new input.
  239.                 fieldPayment.setText ("");
  240.                 fieldLoanAmount.setText ("");
  241.                 areaAmortTable.setText ("");
  242.                 fieldInterest.setText ("");
  243.                 fieldDuration.setText (""); 
  244.  
  245.             }
  246.  
  247.             public Calculator ()
  248.             {
  249.                 //Sets up buttons for GUI
  250.                 Calculate = new JButton ("Calculate");
  251.                 Calculate.setActionCommand ("CalculatePayment");
  252.                 Calculate7 = new JButton ("7 Year, 5.35%");
  253.                 Calculate7.setActionCommand ("Calculate7");
  254.                 Calculate15 = new JButton ("15 Year, 5.50%");
  255.                 Calculate15.setActionCommand ("Calculate15");
  256.                 Calculate30 = new JButton ("30 Year, 5.75%");
  257.                 Calculate30.setActionCommand ("Calculate30");
  258.                 Clear = new JButton ("Clear ");
  259.                 Clear.setActionCommand ("Clear");
  260.                 Exit = new JButton ("Exit");
  261.                 Exit.setActionCommand ("Exit");
  262.  
  263.                 //Set up labels and field sizes for GUI
  264.                 labelTitle = new JLabel ("Mortgage Amortization Calculator");
  265.                 labelInstructions = new JLabel ("Enter the Principle, Interest rate, and Loan Duration.");
  266.                 LabelOr = new JLabel (" Or Pick From These Loan Options After Entering Loan Amount");
  267.                 Payment = new JLabel ("Monthly Payment:");
  268.                 Principle = new JLabel ("Amount of Loan:");
  269.                 fieldPayment = new JTextField ("", 12);
  270.                 fieldLoanAmount = new JTextField ("", 10);
  271.                 AmortizationTable = new JLabel ("Amortization Table");
  272.                 areaAmortTable = new JTextArea (10, 300);
  273.                 Interest = new JLabel ("Interest Rate:");
  274.                 Duration = new JLabel ("Duration:");
  275.                 fieldInterest = new JTextField ("", 12);
  276.                 fieldDuration = new JTextField ("", 10);
  277.  
  278.                 JScrollPane scrollPane = new JScrollPane (areaAmortTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
  279.                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  280.  
  281.                 //Set up listeners for each button.
  282.                 Calculate.addActionListener(this);
  283.                 Calculate7.addActionListener(this);
  284.                 Calculate15.addActionListener(this);
  285.                 Calculate30.addActionListener(this);
  286.                 Clear.addActionListener(this);
  287.                 Exit.addActionListener(this);
  288.  
  289.                 //Constructs GUI. 
  290.                 JPanel mine = new JPanel();
  291.                 mine.setLayout (null);
  292.  
  293.                 mine.add (labelTitle);
  294.                 labelTitle.setBounds (110, 30, 700, 15);
  295.  
  296.                 mine.add (labelInstructions);
  297.                 labelInstructions.setBounds (80, 70, 450, 15);
  298.  
  299.                 mine.add (Principle);
  300.                 Principle.setBounds (130, 110, 100, 25);
  301.  
  302.                 mine.add(Interest);
  303.                 Interest.setBounds (130 , 140, 120, 25);
  304.  
  305.                 mine.add(Calculate);
  306.                 Calculate.setBounds(350, 145, 115, 25);
  307.  
  308.                 mine.add(Duration);
  309.                 Duration.setBounds(130, 170, 120, 25);
  310.  
  311.                 mine.add (fieldLoanAmount);
  312.                 fieldLoanAmount.setBounds (240, 110, 100, 25);
  313.  
  314.                 mine.add(fieldInterest);
  315.                 fieldInterest.setBounds (240, 140, 100, 25);
  316.  
  317.                 mine.add(fieldDuration);
  318.                 fieldDuration.setBounds (240, 170, 100, 25);
  319.  
  320.                 mine.add(LabelOr);
  321.                 LabelOr.setBounds(80, 220, 400, 25);
  322.  
  323.                 mine.add (Calculate7);
  324.                 Calculate7.setBounds (40, 260, 125, 30);
  325.  
  326.                 mine.add (Calculate15);
  327.                 Calculate15.setBounds (180, 260, 125, 30);
  328.  
  329.                 mine.add (Calculate30);
  330.                 Calculate30.setBounds (320, 260, 125, 30);
  331.  
  332.                 mine.add (Payment);
  333.                 Payment.setBounds (130, 300, 100, 25);
  334.  
  335.                 mine.add (fieldPayment);
  336.                 fieldPayment.setBounds (240, 300, 100, 25);
  337.                 fieldPayment.setEditable (false);
  338.  
  339.                 mine.add (AmortizationTable);
  340.                 AmortizationTable.setBounds (180, 330, 300, 25);
  341.  
  342.                  mine.add (scrollPane);
  343.                  scrollPane.setBounds (50, 350, 380, 260);
  344.                  areaAmortTable.setEditable(false);
  345.  
  346.                  mine.add (Clear);
  347.                  Clear.setBounds (110, 620, 125, 30);
  348.  
  349.                 mine.add (Exit);
  350.                 Exit.setBounds (250, 620, 125, 30);
  351.  
  352.                 this.setContentPane(mine);
  353.                 this.pack();
  354.                 this.setTitle("Mortgage Amortization Calculator");
  355.  
  356.                 setScreenSize(Toolkit.getDefaultToolkit().getScreenSize());
  357.                 setSize(500, 700);
  358.             }
  359.             public double loanAmount ()
  360.             {
  361.                 double loanAmount = Double.parseDouble(fieldLoanAmount.getText());
  362.                 return loanAmount;
  363.             }
  364.  
  365.             public double Interest ()
  366.             {
  367.                 double Interest = Double.parseDouble(fieldInterest.getText());
  368.                 return Interest;
  369.             }
  370.  
  371.             public double Duration ()
  372.             {
  373.                 double Duration = Double.parseDouble(fieldDuration.getText());
  374.                 return Duration;
  375.             }
  376.  
  377.             public DecimalFormat getDecimalPlaces() {
  378.                 return decimalPlaces;
  379.             }
  380.  
  381.             public void setDecimalPlaces(DecimalFormat decimalPlaces) {
  382.                 this.decimalPlaces = decimalPlaces;
  383.             }
  384.  
  385.             public DecimalFormat getDecimalPlaces2() {
  386.                 return decimalPlaces2;
  387.             }
  388.  
  389.             public void setDecimalPlaces2(DecimalFormat decimalPlaces2) {
  390.                 this.decimalPlaces2 = decimalPlaces2;
  391.             }
  392.  
  393.             public DecimalFormat getDecimalPlaces4() {
  394.                 return decimalPlaces4;
  395.             }
  396.  
  397.             public void setDecimalPlaces4(DecimalFormat decimalPlaces4) {
  398.                 this.decimalPlaces4 = decimalPlaces4;
  399.             }
  400.  
  401.             public Dimension getScreenSize() {
  402.                 return screenSize;
  403.             }
  404.  
  405.             public void setScreenSize(Dimension screenSize) {
  406.                 this.screenSize = screenSize;
  407.             }
  408.  
  409.             public DecimalFormat getDecimalPlaces3() {
  410.                 return decimalPlaces3;
  411.             }
  412.  
  413.             public void setDecimalPlaces3(DecimalFormat decimalPlaces3) {
  414.                 this.decimalPlaces3 = decimalPlaces3;
  415.             }
  416.         }
  417. // end of program 
Feb 13 '12 #3

P: 21
Hi Eric
In your code you have'nt write the code for amount and intrest, you need to get values for these to field and pass in calculate method.


Inter= Double.parseDouble(fieldInterest.getText());
Months= Integer.parseInt(fieldDuration.getText());
you can run this code. your program will work fine

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class MortgageCalculatorWeek3 {

public static void main(String[] args) { // GUI JFrame Functions for Visibility and exit tool bar.
JFrame window = new Calculator ();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
window.setVisible(true);
}
}
class Calculator extends JFrame implements ActionListener{ // Shows that the class Calculator extends the JFrame.

private static final long serialVersionUID = 1L; // Added to get rid of an error
private JButton Calculate, Calculate7, Calculate15, Calculate30, Clear, Exit; // Declares the buttons
private JLabel Payment, Principle, labelTitle, labelInstructions,AmortizationTable, Interest, Duration, LabelOr; // declares the JLables
private JTextField fieldPayment, fieldLoanAmount, fieldInterest, fieldDuration; // Declares the JTextfields
private JTextArea areaAmortTable; // Declares the Amortization table text area. The area supports more lines than the JTextfield.

//Declare variables and the loan array and assigns values.
double InterestPaid,PrinciplePaid,Balance,monthlyPay,loan Amount;
int[] loanTerm = {7,15,30}; // Declares Loan terms
double[] intRate = {5.35,5.50,5.75}; // Declares interest rates
double loanAmt = loanAmount; // Declares area for user input for principle amount.
private DecimalFormat decimalPlaces; // sets decimal places for first calculate button
private DecimalFormat decimalPlaces2; // sets decimal places for second calculate button.
private DecimalFormat decimalPlaces3; // sets decimal places for third calculate button.
private DecimalFormat decimalPlaces4;
private Dimension screenSize; // sets screen size.
int Months;
double Inter;


public void actionPerformed (ActionEvent e)
{
//Provides looping functions for the buttons pressed.
if ("Calculate".equals(e.getActionCommand()))
{
setDecimalPlaces4(new DecimalFormat("0.00"));
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = Calculate();


fieldPayment.setText("" + (currency.format(paymentAmount)));
areaAmortTable.append ("Payment \t Balance After Payment \t Interest Paid\n");

loanAmt = Double.parseDouble(fieldLoanAmount.getText());
for (int x = 1; x <=(Inter*12); x++){
Balance=loanAmt;
InterestPaid=(((Inter/100.0)/12.0)*Balance);
PrinciplePaid=(paymentAmount-InterestPaid);
loanAmt=(Balance-PrinciplePaid);
areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
}
if (Balance <= 0) //ending statement, balance at $0.00
areaAmortTable.append ("\tRemaining balance = $0.00");
}
{
}

if ("Calculate7".equals(e.getActionCommand()))
{
setDecimalPlaces(new DecimalFormat("0.00"));
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = CalculatePayment7();

fieldPayment.setText("" + (currency.format(paymentAmount)));
areaAmortTable.append ("Payment \t Balance After Payment \t Interest Paid\n"); // sets fields for loan amortization table

loanAmt = Double.parseDouble(fieldLoanAmount.getText()); // allows user to input principle amount.
for (int x = 1; x <=(loanTerm[0]*12); x++) //Loop through all monthly payments for each loan
{
Balance=loanAmt; //Set Balance to Loan Amount

//Calculations on monthly payment
InterestPaid=(((intRate[0]/100.0)/12.0)*Balance); //Monthly Interest paid
PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle
loanAmt=(Balance-PrinciplePaid); //loan balance after payment

areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
}

if (Balance <= 0) //ending statement, balance at $0.00
{
areaAmortTable.append ("\tRemaining balance = $0.00");
}
}
else if ("Calculate15".equals(e.getActionCommand())) // tells the first button what actions to perform.
{
setDecimalPlaces2(new DecimalFormat("0.00"));
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = CalculatePayment15 ();
fieldPayment.setText("" + (currency.format(paymentAmount)));

areaAmortTable.append ("Payment \t Balance After Payment \t Interest Paid\n");
loanAmt = Double.parseDouble(fieldLoanAmount.getText());
for (int x = 1; x <=(loanTerm[1]*12); x++)
{
Balance = loanAmt;
//Calculations on monthly payment
InterestPaid=((intRate[1]/100/12)*Balance); //Monthly Interest paid
PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle
loanAmt=((Balance-PrinciplePaid)); //loan balance after payment
areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
}
if (loanAmt <= 0) //ending statement, balance at $0.00
{
areaAmortTable.append ("\tRemaining balance = $0.00");
}
}
else if ("Calculate30".equals(e.getActionCommand()))
{
setDecimalPlaces3(new DecimalFormat("0.00"));
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = CalculatePayment30 ();
fieldPayment.setText("" + (currency.format(paymentAmount)));
areaAmortTable.append ("Payment \t Balance After Payment \t Interest Paid\n");
loanAmt = Double.parseDouble(fieldLoanAmount.getText());

for (int x = 1; x <=(loanTerm[2]*12); x++)
{
Balance = loanAmt;
//Calculations on monthly payment
InterestPaid=((intRate[2]/100/12)*Balance); //Monthly Interest paid
PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle
loanAmt=((Balance-PrinciplePaid)); //loan balance after payment
areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
}
if (loanAmt <= 0) //ending statement, balance at $0.00
{
areaAmortTable.append ("\tRemaining balance = $0.00");

}
}

else if ("Clear".equals(e.getActionCommand()))
{
ClearFields ();
}

else
{
//System.exit(0); //End and close application.
}
}

public double CalculatePayment7 ()
{

try //Check for number entry
{
//Perform payment calculations if input is valid

fieldPayment.setText ("");
double Payment =(loanAmount() * (intRate[0]/100/12)) / (1 - Math.pow(1/(1 + (intRate[0]/100/12)),loanTerm[0]*12));
return Payment;
}

catch (NumberFormatException event)
{
//Display error message for invalid characters and beep at user.
// beeps when there is an error
JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "ERROR", JOptionPane.ERROR_MESSAGE);
System.out.print ( "\007" );
return 0;
}
}

public double CalculatePayment15 ()
{

try //Check for valid numeric input
{
//Display error message for invalid characters and beep at user.
fieldPayment.setText ("");
double Payment =(loanAmount() * (intRate[1]/100/12)) / (1 - Math.pow(1/(1 + (intRate[1]/100/12)),loanTerm[1]*12));
return Payment;
}

catch (NumberFormatException event)
{
// Display error message for invalid characters and beep at user.
System.out.print ( "\007" ); // beeps when there is an error
JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "ERROR", JOptionPane.ERROR_MESSAGE);
return 0;
}
}

public double CalculatePayment30 ()
{
//Check for valid numeric input
try
{
//Perform payment calculations if input is valid
fieldPayment.setText ("");
double Payment =(loanAmount() * (intRate[2]/100/12)) / (1 - Math.pow(1/(1 + (intRate[2]/100/12)),loanTerm[2]*12));
return Payment;
}

catch (NumberFormatException event)
{
// Display error message for invalid characters and beep at user.
System.out.print ( "\007" ); // beeps when there is an error
JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "Error", JOptionPane.ERROR_MESSAGE);
return 0;
}

}
public double Calculate(){
try{

fieldPayment.setText("");
//Modified by neeraj **************************//////////
Inter= Double.parseDouble(fieldInterest.getText());
Months= Integer.parseInt(fieldDuration.getText());
//Modified by neeraj **************************//////////
double Payment =(loanAmount() * (Inter/100/12)) / (1 - Math.pow(1/(1 + (Inter/100/12)),Months*12));
return Payment;

}

catch (NumberFormatException event){
JOptionPane.showMessageDialog(null, "Invalid entry, please enter numbers only", "Error", JOptionPane.ERROR_MESSAGE);
return 0;
}
}


public void ClearFields ()
{
//clears all fields for new input.
fieldPayment.setText ("");
fieldLoanAmount.setText ("");
areaAmortTable.setText ("");
fieldInterest.setText ("");
fieldDuration.setText ("");

}

public Calculator ()
{
//Sets up buttons for GUI
Calculate = new JButton ("Calculate");
Calculate.setActionCommand ("Calculate");
Calculate7 = new JButton ("7 Year, 5.35%");
Calculate7.setActionCommand ("Calculate7");
Calculate15 = new JButton ("15 Year, 5.50%");
Calculate15.setActionCommand ("Calculate15");
Calculate30 = new JButton ("30 Year, 5.75%");
Calculate30.setActionCommand ("Calculate30");
Clear = new JButton ("Clear ");
Clear.setActionCommand ("Clear");
Exit = new JButton ("Exit");
Exit.setActionCommand ("Exit");

//Set up labels and field sizes for GUI
labelTitle = new JLabel ("Mortgage Amortization Calculator");
labelInstructions = new JLabel ("Enter the Principle, Interest rate, and Loan Duration.");
LabelOr = new JLabel (" Or Pick From These Loan Options After Entering Loan Amount");
Payment = new JLabel ("Monthly Payment:");
Principle = new JLabel ("Amount of Loan:");
fieldPayment = new JTextField ("", 12);
fieldLoanAmount = new JTextField ("", 10);
AmortizationTable = new JLabel ("Amortization Table");
areaAmortTable = new JTextArea (10, 300);
Interest = new JLabel ("Interest Rate:");
Duration = new JLabel ("Duration:");
fieldInterest = new JTextField ("", 12);
fieldDuration = new JTextField ("", 10);

JScrollPane scrollPane = new JScrollPane (areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

//Set up listeners for each button.
Calculate.addActionListener(this);
Calculate7.addActionListener(this);
Calculate15.addActionListener(this);
Calculate30.addActionListener(this);
Clear.addActionListener(this);
Exit.addActionListener(this);

//Constructs GUI.
JPanel mine = new JPanel();
mine.setLayout (null);

mine.add (labelTitle);
labelTitle.setBounds (110, 30, 700, 15);

mine.add (labelInstructions);
labelInstructions.setBounds (80, 70, 450, 15);

mine.add (Principle);
Principle.setBounds (130, 110, 100, 25);

mine.add(Interest);
Interest.setBounds (130 , 140, 120, 25);

mine.add(Calculate);
Calculate.setBounds(350, 145, 115, 25);

mine.add(Duration);
Duration.setBounds(130, 170, 120, 25);

mine.add (fieldLoanAmount);
fieldLoanAmount.setBounds (240, 110, 100, 25);

mine.add(fieldInterest);
fieldInterest.setBounds (240, 140, 100, 25);

mine.add(fieldDuration);
fieldDuration.setBounds (240, 170, 100, 25);

mine.add(LabelOr);
LabelOr.setBounds(80, 220, 400, 25);

mine.add (Calculate7);
Calculate7.setBounds (40, 260, 125, 30);

mine.add (Calculate15);
Calculate15.setBounds (180, 260, 125, 30);

mine.add (Calculate30);
Calculate30.setBounds (320, 260, 125, 30);

mine.add (Payment);
Payment.setBounds (130, 300, 100, 25);

mine.add (fieldPayment);
fieldPayment.setBounds (240, 300, 100, 25);
fieldPayment.setEditable (false);

mine.add (AmortizationTable);
AmortizationTable.setBounds (180, 330, 300, 25);

mine.add (scrollPane);
scrollPane.setBounds (50, 350, 380, 260);
areaAmortTable.setEditable(false);

mine.add (Clear);
Clear.setBounds (110, 620, 125, 30);

mine.add (Exit);
Exit.setBounds (250, 620, 125, 30);

this.setContentPane(mine);
this.pack();
this.setTitle("Mortgage Amortization Calculator");

setScreenSize(Toolkit.getDefaultToolkit().getScree nSize());
setSize(500, 700);
}
public double loanAmount ()
{
double loanAmount = Double.parseDouble(fieldLoanAmount.getText());
System.out.println("loanAmount = "+loanAmount+" fieldLoanAmount.getText()= "+fieldLoanAmount.getText());
return loanAmount;
}

public DecimalFormat getDecimalPlaces() {
return decimalPlaces;
}

public void setDecimalPlaces(DecimalFormat decimalPlaces) {
this.decimalPlaces = decimalPlaces;
}

public DecimalFormat getDecimalPlaces2() {
return decimalPlaces2;
}

public void setDecimalPlaces2(DecimalFormat decimalPlaces2) {
this.decimalPlaces2 = decimalPlaces2;
}

public DecimalFormat getDecimalPlaces4() {
return decimalPlaces4;
}

public void setDecimalPlaces4(DecimalFormat decimalPlaces4) {
this.decimalPlaces4 = decimalPlaces4;
}

public Dimension getScreenSize() {
return screenSize;
}

public void setScreenSize(Dimension screenSize) {
this.screenSize = screenSize;
}

public DecimalFormat getDecimalPlaces3() {
return decimalPlaces3;
}

public void setDecimalPlaces3(DecimalFormat decimalPlaces3) {
this.decimalPlaces3 = decimalPlaces3;
}
}
// end of program


And i hv some suggestion for your program.
" specify the duration as month or year" I think It is taking as a year so kindly check your code again.

Please run the code and tell me is it working fine or not.
Feb 13 '12 #4

P: 21
Hi Eric
In your code you have'nt write the code for amount and intrest, you need to get values for these to field and pass in calculate method.
Inter= Double.parseDouble(fieldInterest.getText());
Months= Integer.parseInt(fieldDuration.getText());
you can run this code. your program will work fine

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class MortgageCalculatorWeek3 {

public static void main(String[] args) { // GUI JFrame Functions for Visibility and exit tool bar.
JFrame window = new Calculator ();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
window.setVisible(true);
}
}
class Calculator extends JFrame implements ActionListener{ // Shows that the class Calculator extends the JFrame.

private static final long serialVersionUID = 1L; // Added to get rid of an error
private JButton Calculate, Calculate7, Calculate15, Calculate30, Clear, Exit; // Declares the buttons
private JLabel Payment, Principle, labelTitle, labelInstructions,AmortizationTable, Interest, Duration, LabelOr; // declares the JLables
private JTextField fieldPayment, fieldLoanAmount, fieldInterest, fieldDuration; // Declares the JTextfields
private JTextArea areaAmortTable; // Declares the Amortization table text area. The area supports more lines than the JTextfield.

//Declare variables and the loan array and assigns values.
double InterestPaid,PrinciplePaid,Balance,monthlyPay,loan Amount;
int[] loanTerm = {7,15,30}; // Declares Loan terms
double[] intRate = {5.35,5.50,5.75}; // Declares interest rates
double loanAmt = loanAmount; // Declares area for user input for principle amount.
private DecimalFormat decimalPlaces; // sets decimal places for first calculate button
private DecimalFormat decimalPlaces2; // sets decimal places for second calculate button.
private DecimalFormat decimalPlaces3; // sets decimal places for third calculate button.
private DecimalFormat decimalPlaces4;
private Dimension screenSize; // sets screen size.
int Months;
double Inter;


public void actionPerformed (ActionEvent e)
{
//Provides looping functions for the buttons pressed.
if ("Calculate".equals(e.getActionCommand()))
{
setDecimalPlaces4(new DecimalFormat("0.00"));
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = Calculate();


fieldPayment.setText("" + (currency.format(paymentAmount)));
areaAmortTable.append ("Payment \t Balance After Payment \t Interest Paid\n");

loanAmt = Double.parseDouble(fieldLoanAmount.getText());
for (int x = 1; x <=(Inter*12); x++){
Balance=loanAmt;
InterestPaid=(((Inter/100.0)/12.0)*Balance);
PrinciplePaid=(paymentAmount-InterestPaid);
loanAmt=(Balance-PrinciplePaid);
areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
}
if (Balance <= 0) //ending statement, balance at $0.00
areaAmortTable.append ("\tRemaining balance = $0.00");
}
{
}

if ("Calculate7".equals(e.getActionCommand()))
{
setDecimalPlaces(new DecimalFormat("0.00"));
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = CalculatePayment7();

fieldPayment.setText("" + (currency.format(paymentAmount)));
areaAmortTable.append ("Payment \t Balance After Payment \t Interest Paid\n"); // sets fields for loan amortization table

loanAmt = Double.parseDouble(fieldLoanAmount.getText()); // allows user to input principle amount.
for (int x = 1; x <=(loanTerm[0]*12); x++) //Loop through all monthly payments for each loan
{
Balance=loanAmt; //Set Balance to Loan Amount

//Calculations on monthly payment
InterestPaid=(((intRate[0]/100.0)/12.0)*Balance); //Monthly Interest paid
PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle
loanAmt=(Balance-PrinciplePaid); //loan balance after payment

areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
}

if (Balance <= 0) //ending statement, balance at $0.00
{
areaAmortTable.append ("\tRemaining balance = $0.00");
}
}
else if ("Calculate15".equals(e.getActionCommand())) // tells the first button what actions to perform.
{
setDecimalPlaces2(new DecimalFormat("0.00"));
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = CalculatePayment15 ();
fieldPayment.setText("" + (currency.format(paymentAmount)));

areaAmortTable.append ("Payment \t Balance After Payment \t Interest Paid\n");
loanAmt = Double.parseDouble(fieldLoanAmount.getText());
for (int x = 1; x <=(loanTerm[1]*12); x++)
{
Balance = loanAmt;
//Calculations on monthly payment
InterestPaid=((intRate[1]/100/12)*Balance); //Monthly Interest paid
PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle
loanAmt=((Balance-PrinciplePaid)); //loan balance after payment
areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
}
if (loanAmt <= 0) //ending statement, balance at $0.00
{
areaAmortTable.append ("\tRemaining balance = $0.00");
}
}
else if ("Calculate30".equals(e.getActionCommand()))
{
setDecimalPlaces3(new DecimalFormat("0.00"));
NumberFormat currency = NumberFormat.getCurrencyInstance();
double paymentAmount = CalculatePayment30 ();
fieldPayment.setText("" + (currency.format(paymentAmount)));
areaAmortTable.append ("Payment \t Balance After Payment \t Interest Paid\n");
loanAmt = Double.parseDouble(fieldLoanAmount.getText());

for (int x = 1; x <=(loanTerm[2]*12); x++)
{
Balance = loanAmt;
//Calculations on monthly payment
InterestPaid=((intRate[2]/100/12)*Balance); //Monthly Interest paid
PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle
loanAmt=((Balance-PrinciplePaid)); //loan balance after payment
areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n");
}
if (loanAmt <= 0) //ending statement, balance at $0.00
{
areaAmortTable.append ("\tRemaining balance = $0.00");

}
}

else if ("Clear".equals(e.getActionCommand()))
{
ClearFields ();
}

else
{
//System.exit(0); //End and close application.
}
}

public double CalculatePayment7 ()
{

try //Check for number entry
{
//Perform payment calculations if input is valid

fieldPayment.setText ("");
double Payment =(loanAmount() * (intRate[0]/100/12)) / (1 - Math.pow(1/(1 + (intRate[0]/100/12)),loanTerm[0]*12));
return Payment;
}

catch (NumberFormatException event)
{
//Display error message for invalid characters and beep at user.
// beeps when there is an error
JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "ERROR", JOptionPane.ERROR_MESSAGE);
System.out.print ( "\007" );
return 0;
}
}

public double CalculatePayment15 ()
{

try //Check for valid numeric input
{
//Display error message for invalid characters and beep at user.
fieldPayment.setText ("");
double Payment =(loanAmount() * (intRate[1]/100/12)) / (1 - Math.pow(1/(1 + (intRate[1]/100/12)),loanTerm[1]*12));
return Payment;
}

catch (NumberFormatException event)
{
// Display error message for invalid characters and beep at user.
System.out.print ( "\007" ); // beeps when there is an error
JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "ERROR", JOptionPane.ERROR_MESSAGE);
return 0;
}
}

public double CalculatePayment30 ()
{
//Check for valid numeric input
try
{
//Perform payment calculations if input is valid
fieldPayment.setText ("");
double Payment =(loanAmount() * (intRate[2]/100/12)) / (1 - Math.pow(1/(1 + (intRate[2]/100/12)),loanTerm[2]*12));
return Payment;
}

catch (NumberFormatException event)
{
// Display error message for invalid characters and beep at user.
System.out.print ( "\007" ); // beeps when there is an error
JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "Error", JOptionPane.ERROR_MESSAGE);
return 0;
}

}
public double Calculate(){
try{

fieldPayment.setText("");
//Modified by neeraj **************************//////////
Inter= Double.parseDouble(fieldInterest.getText());
Months= Integer.parseInt(fieldDuration.getText());
//Modified by neeraj **************************//////////
double Payment =(loanAmount() * (Inter/100/12)) / (1 - Math.pow(1/(1 + (Inter/100/12)),Months*12));
return Payment;

}

catch (NumberFormatException event){
JOptionPane.showMessageDialog(null, "Invalid entry, please enter numbers only", "Error", JOptionPane.ERROR_MESSAGE);
return 0;
}
}


public void ClearFields ()
{
//clears all fields for new input.
fieldPayment.setText ("");
fieldLoanAmount.setText ("");
areaAmortTable.setText ("");
fieldInterest.setText ("");
fieldDuration.setText ("");

}

public Calculator ()
{
//Sets up buttons for GUI
Calculate = new JButton ("Calculate");
Calculate.setActionCommand ("Calculate");
Calculate7 = new JButton ("7 Year, 5.35%");
Calculate7.setActionCommand ("Calculate7");
Calculate15 = new JButton ("15 Year, 5.50%");
Calculate15.setActionCommand ("Calculate15");
Calculate30 = new JButton ("30 Year, 5.75%");
Calculate30.setActionCommand ("Calculate30");
Clear = new JButton ("Clear ");
Clear.setActionCommand ("Clear");
Exit = new JButton ("Exit");
Exit.setActionCommand ("Exit");

//Set up labels and field sizes for GUI
labelTitle = new JLabel ("Mortgage Amortization Calculator");
labelInstructions = new JLabel ("Enter the Principle, Interest rate, and Loan Duration.");
LabelOr = new JLabel (" Or Pick From These Loan Options After Entering Loan Amount");
Payment = new JLabel ("Monthly Payment:");
Principle = new JLabel ("Amount of Loan:");
fieldPayment = new JTextField ("", 12);
fieldLoanAmount = new JTextField ("", 10);
AmortizationTable = new JLabel ("Amortization Table");
areaAmortTable = new JTextArea (10, 300);
Interest = new JLabel ("Interest Rate:");
Duration = new JLabel ("Duration:");
fieldInterest = new JTextField ("", 12);
fieldDuration = new JTextField ("", 10);

JScrollPane scrollPane = new JScrollPane (areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

//Set up listeners for each button.
Calculate.addActionListener(this);
Calculate7.addActionListener(this);
Calculate15.addActionListener(this);
Calculate30.addActionListener(this);
Clear.addActionListener(this);
Exit.addActionListener(this);

//Constructs GUI.
JPanel mine = new JPanel();
mine.setLayout (null);

mine.add (labelTitle);
labelTitle.setBounds (110, 30, 700, 15);

mine.add (labelInstructions);
labelInstructions.setBounds (80, 70, 450, 15);

mine.add (Principle);
Principle.setBounds (130, 110, 100, 25);

mine.add(Interest);
Interest.setBounds (130 , 140, 120, 25);

mine.add(Calculate);
Calculate.setBounds(350, 145, 115, 25);

mine.add(Duration);
Duration.setBounds(130, 170, 120, 25);

mine.add (fieldLoanAmount);
fieldLoanAmount.setBounds (240, 110, 100, 25);

mine.add(fieldInterest);
fieldInterest.setBounds (240, 140, 100, 25);

mine.add(fieldDuration);
fieldDuration.setBounds (240, 170, 100, 25);

mine.add(LabelOr);
LabelOr.setBounds(80, 220, 400, 25);

mine.add (Calculate7);
Calculate7.setBounds (40, 260, 125, 30);

mine.add (Calculate15);
Calculate15.setBounds (180, 260, 125, 30);

mine.add (Calculate30);
Calculate30.setBounds (320, 260, 125, 30);

mine.add (Payment);
Payment.setBounds (130, 300, 100, 25);

mine.add (fieldPayment);
fieldPayment.setBounds (240, 300, 100, 25);
fieldPayment.setEditable (false);

mine.add (AmortizationTable);
AmortizationTable.setBounds (180, 330, 300, 25);

mine.add (scrollPane);
scrollPane.setBounds (50, 350, 380, 260);
areaAmortTable.setEditable(false);

mine.add (Clear);
Clear.setBounds (110, 620, 125, 30);

mine.add (Exit);
Exit.setBounds (250, 620, 125, 30);

this.setContentPane(mine);
this.pack();
this.setTitle("Mortgage Amortization Calculator");

setScreenSize(Toolkit.getDefaultToolkit().getScree nSize());
setSize(500, 700);
}
public double loanAmount ()
{
double loanAmount = Double.parseDouble(fieldLoanAmount.getText());
System.out.println("loanAmount = "+loanAmount+" fieldLoanAmount.getText()= "+fieldLoanAmount.getText());
return loanAmount;
}

public DecimalFormat getDecimalPlaces() {
return decimalPlaces;
}

public void setDecimalPlaces(DecimalFormat decimalPlaces) {
this.decimalPlaces = decimalPlaces;
}

public DecimalFormat getDecimalPlaces2() {
return decimalPlaces2;
}

public void setDecimalPlaces2(DecimalFormat decimalPlaces2) {
this.decimalPlaces2 = decimalPlaces2;
}

public DecimalFormat getDecimalPlaces4() {
return decimalPlaces4;
}

public void setDecimalPlaces4(DecimalFormat decimalPlaces4) {
this.decimalPlaces4 = decimalPlaces4;
}

public Dimension getScreenSize() {
return screenSize;
}

public void setScreenSize(Dimension screenSize) {
this.screenSize = screenSize;
}

public DecimalFormat getDecimalPlaces3() {
return decimalPlaces3;
}

public void setDecimalPlaces3(DecimalFormat decimalPlaces3) {
this.decimalPlaces3 = decimalPlaces3;
}
}
// end of program


Feb 13 '12 #5

P: 5
I changed the code that you specified, but the program still terminates when you press the calculate button. I see where I forget to put in those variables, but there are still problems with the exit over-ride.
Feb 13 '12 #6

P: 21
Hi Eric,

Try this It is working fine.


Expand|Select|Wrap|Line Numbers
  1. import java.awt.*; 
  2. import java.awt.event.*; 
  3. import javax.swing.*; 
  4. import java.text.DecimalFormat; 
  5. import java.text.NumberFormat; 
  6. public class MortgageCalculatorWeek3 { 
  7.  
  8. public static void main(String[] args) { // GUI JFrame Functions for Visibility and exit tool bar. 
  9. JFrame window = new Calculator (); 
  10. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  11. window.setVisible(true); 
  12. class Calculator extends JFrame implements ActionListener{ // Shows that the class Calculator extends the JFrame. 
  13.  
  14. private static final long serialVersionUID = 1L; // Added to get rid of an error 
  15. private JButton Calculate, Calculate7, Calculate15, Calculate30, Clear, Exit; // Declares the buttons 
  16. private JLabel Payment, Principle, labelTitle, labelInstructions,AmortizationTable, Interest, Duration, LabelOr; // declares the JLables 
  17. private JTextField fieldPayment, fieldLoanAmount, fieldInterest, fieldDuration; // Declares the JTextfields 
  18. private JTextArea areaAmortTable; // Declares the Amortization table text area. The area supports more lines than the JTextfield. 
  19.  
  20. //Declare variables and the loan array and assigns values. 
  21. double InterestPaid,PrinciplePaid,Balance,monthlyPay,loanAmount; 
  22. int[] loanTerm = {7,15,30}; // Declares Loan terms 
  23. double[] intRate = {5.35,5.50,5.75}; // Declares interest rates 
  24. double loanAmt = loanAmount; // Declares area for user input for principle amount. 
  25. private DecimalFormat decimalPlaces; // sets decimal places for first calculate button 
  26. private DecimalFormat decimalPlaces2; // sets decimal places for second calculate button. 
  27. private DecimalFormat decimalPlaces3; // sets decimal places for third calculate button. 
  28. private DecimalFormat decimalPlaces4; 
  29. private Dimension screenSize; // sets screen size. 
  30. int Months; 
  31. double Inter; 
  32.  
  33.  
  34. public void actionPerformed (ActionEvent e) 
  35. //Provides looping functions for the buttons pressed. 
  36. if ("Calculate".equals(e.getActionCommand())) 
  37. {
  38.  
  39. setDecimalPlaces4(new DecimalFormat("0.00")); 
  40. NumberFormat currency = NumberFormat.getCurrencyInstance(); 
  41. double paymentAmount = Calculate(); 
  42.  
  43.  
  44. fieldPayment.setText("" + (currency.format(paymentAmount))); 
  45. areaAmortTable.append ("Payment \t Balance After Payment \t Interest Paid\n"); 
  46.  
  47. loanAmt = Double.parseDouble(fieldLoanAmount.getText()); 
  48. for (int x = 1; x <=(Inter*12); x++){ 
  49. Balance=loanAmt; 
  50. InterestPaid=(((Inter/100.0)/12.0)*Balance); 
  51. PrinciplePaid=(paymentAmount-InterestPaid); 
  52. loanAmt=(Balance-PrinciplePaid); 
  53. areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n"); 
  54. if (Balance <= 0) //ending statement, balance at $0.00 
  55. areaAmortTable.append ("\tRemaining balance = $0.00"); 
  56. }
  57. else if ("Calculate7".equals(e.getActionCommand())) 
  58. setDecimalPlaces(new DecimalFormat("0.00")); 
  59. NumberFormat currency = NumberFormat.getCurrencyInstance(); 
  60. double paymentAmount = CalculatePayment7(); 
  61.  
  62. fieldPayment.setText("" + (currency.format(paymentAmount))); 
  63. areaAmortTable.append ("Payment \t Balance After Payment \t Interest Paid\n"); // sets fields for loan amortization table 
  64.  
  65. loanAmt = Double.parseDouble(fieldLoanAmount.getText()); // allows user to input principle amount. 
  66. for (int x = 1; x <=(loanTerm[0]*12); x++) //Loop through all monthly payments for each loan 
  67. Balance=loanAmt; //Set Balance to Loan Amount 
  68.  
  69. //Calculations on monthly payment 
  70. InterestPaid=(((intRate[0]/100.0)/12.0)*Balance); //Monthly Interest paid 
  71. PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle 
  72. loanAmt=(Balance-PrinciplePaid); //loan balance after payment 
  73.  
  74. areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n"); 
  75.  
  76. if (Balance <= 0) //ending statement, balance at $0.00 
  77. areaAmortTable.append ("\tRemaining balance = $0.00"); 
  78. else if ("Calculate15".equals(e.getActionCommand())) // tells the first button what actions to perform. 
  79. setDecimalPlaces2(new DecimalFormat("0.00")); 
  80. NumberFormat currency = NumberFormat.getCurrencyInstance(); 
  81. double paymentAmount = CalculatePayment15 (); 
  82. fieldPayment.setText("" + (currency.format(paymentAmount))); 
  83.  
  84. areaAmortTable.append ("Payment \t Balance After Payment \t Interest Paid\n"); 
  85. loanAmt = Double.parseDouble(fieldLoanAmount.getText()); 
  86. for (int x = 1; x <=(loanTerm[1]*12); x++) 
  87. Balance = loanAmt; 
  88. //Calculations on monthly payment 
  89. InterestPaid=((intRate[1]/100/12)*Balance); //Monthly Interest paid 
  90. PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle 
  91. loanAmt=((Balance-PrinciplePaid)); //loan balance after payment 
  92. areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n"); 
  93. if (loanAmt <= 0) //ending statement, balance at $0.00 
  94. areaAmortTable.append ("\tRemaining balance = $0.00"); 
  95. else if ("Calculate30".equals(e.getActionCommand())) 
  96. setDecimalPlaces3(new DecimalFormat("0.00")); 
  97. NumberFormat currency = NumberFormat.getCurrencyInstance(); 
  98. double paymentAmount = CalculatePayment30 (); 
  99. fieldPayment.setText("" + (currency.format(paymentAmount))); 
  100. areaAmortTable.append ("Payment \t Balance After Payment \t Interest Paid\n"); 
  101. loanAmt = Double.parseDouble(fieldLoanAmount.getText()); 
  102.  
  103. for (int x = 1; x <=(loanTerm[2]*12); x++) 
  104. Balance = loanAmt; 
  105. //Calculations on monthly payment 
  106. InterestPaid=((intRate[2]/100/12)*Balance); //Monthly Interest paid 
  107. PrinciplePaid=(paymentAmount-InterestPaid); //Applied towards principle 
  108. loanAmt=((Balance-PrinciplePaid)); //loan balance after payment 
  109. areaAmortTable.append (x + " \t " + currency.format(loanAmt) + " \t\t " + currency.format(InterestPaid)+"\n"); 
  110. if (loanAmt <= 0) //ending statement, balance at $0.00 
  111. areaAmortTable.append ("\tRemaining balance = $0.00"); 
  112.  
  113.  
  114. else if ("Clear".equals(e.getActionCommand())) 
  115. ClearFields (); 
  116.  
  117. else 
  118. System.exit(0); //End and close application. 
  119.  
  120. public double CalculatePayment7 () 
  121.  
  122. try //Check for number entry 
  123. //Perform payment calculations if input is valid 
  124.  
  125. fieldPayment.setText (""); 
  126. double Payment =(loanAmount() * (intRate[0]/100/12)) / (1 - Math.pow(1/(1 + (intRate[0]/100/12)),loanTerm[0]*12)); 
  127. return Payment; 
  128.  
  129. catch (NumberFormatException event) 
  130. //Display error message for invalid characters and beep at user. 
  131. // beeps when there is an error 
  132. JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "ERROR", JOptionPane.ERROR_MESSAGE); 
  133. System.out.print ( "\007" ); 
  134. return 0; 
  135.  
  136. public double CalculatePayment15 () 
  137.  
  138. try //Check for valid numeric input 
  139. //Display error message for invalid characters and beep at user. 
  140. fieldPayment.setText (""); 
  141. double Payment =(loanAmount() * (intRate[1]/100/12)) / (1 - Math.pow(1/(1 + (intRate[1]/100/12)),loanTerm[1]*12)); 
  142. return Payment; 
  143.  
  144. catch (NumberFormatException event) 
  145. // Display error message for invalid characters and beep at user. 
  146. System.out.print ( "\007" ); // beeps when there is an error 
  147. JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "ERROR", JOptionPane.ERROR_MESSAGE); 
  148. return 0; 
  149.  
  150. public double CalculatePayment30 () 
  151. //Check for valid numeric input 
  152. try 
  153. //Perform payment calculations if input is valid 
  154. fieldPayment.setText (""); 
  155. double Payment =(loanAmount() * (intRate[2]/100/12)) / (1 - Math.pow(1/(1 + (intRate[2]/100/12)),loanTerm[2]*12)); 
  156. return Payment; 
  157.  
  158. catch (NumberFormatException event) 
  159. // Display error message for invalid characters and beep at user. 
  160. System.out.print ( "\007" ); // beeps when there is an error 
  161. JOptionPane.showMessageDialog(null, " Invalid entry, please enter numbers only", "Error", JOptionPane.ERROR_MESSAGE); 
  162. return 0; 
  163.  
  164. public double Calculate(){ 
  165. try{ 
  166.  
  167. fieldPayment.setText(""); 
  168. //Modified by neeraj **************************//////////
  169. Inter= Double.parseDouble(fieldInterest.getText()); 
  170. Months= Integer.parseInt(fieldDuration.getText()); 
  171. //Modified by neeraj **************************//////////
  172. double Payment =(loanAmount() * (Inter/100/12)) / (1 - Math.pow(1/(1 + (Inter/100/12)),Months*12)); 
  173. return Payment; 
  174.  
  175.  
  176. catch (NumberFormatException event){ 
  177. JOptionPane.showMessageDialog(null, "Invalid entry, please enter numbers only", "Error", JOptionPane.ERROR_MESSAGE); 
  178. return 0; 
  179.  
  180.  
  181. public void ClearFields () 
  182. //clears all fields for new input. 
  183. fieldPayment.setText (""); 
  184. fieldLoanAmount.setText (""); 
  185. areaAmortTable.setText (""); 
  186. fieldInterest.setText (""); 
  187. fieldDuration.setText (""); 
  188.  
  189.  
  190. public Calculator () 
  191. //Sets up buttons for GUI 
  192. Calculate = new JButton ("Calculate"); 
  193. Calculate.setActionCommand ("Calculate"); 
  194. Calculate7 = new JButton ("7 Year, 5.35%"); 
  195. Calculate7.setActionCommand ("Calculate7"); 
  196. Calculate15 = new JButton ("15 Year, 5.50%"); 
  197. Calculate15.setActionCommand ("Calculate15"); 
  198. Calculate30 = new JButton ("30 Year, 5.75%"); 
  199. Calculate30.setActionCommand ("Calculate30"); 
  200. Clear = new JButton ("Clear "); 
  201. Clear.setActionCommand ("Clear"); 
  202. Exit = new JButton ("Exit"); 
  203. Exit.setActionCommand ("Exit"); 
  204.  
  205. //Set up labels and field sizes for GUI 
  206. labelTitle = new JLabel ("Mortgage Amortization Calculator"); 
  207. labelInstructions = new JLabel ("Enter the Principle, Interest rate, and Loan Duration."); 
  208. LabelOr = new JLabel (" Or Pick From These Loan Options After Entering Loan Amount"); 
  209. Payment = new JLabel ("Monthly Payment:"); 
  210. Principle = new JLabel ("Amount of Loan:"); 
  211. fieldPayment = new JTextField ("", 12); 
  212. fieldLoanAmount = new JTextField ("", 10); 
  213. AmortizationTable = new JLabel ("Amortization Table"); 
  214. areaAmortTable = new JTextArea (10, 300); 
  215. Interest = new JLabel ("Interest Rate:"); 
  216. Duration = new JLabel ("Duration:"); 
  217. fieldInterest = new JTextField ("", 12); 
  218. fieldDuration = new JTextField ("", 10); 
  219.  
  220. JScrollPane scrollPane = new JScrollPane (areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
  221. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
  222.  
  223. //Set up listeners for each button. 
  224. Calculate.addActionListener(this); 
  225. Calculate7.addActionListener(this); 
  226. Calculate15.addActionListener(this); 
  227. Calculate30.addActionListener(this); 
  228. Clear.addActionListener(this); 
  229. Exit.addActionListener(this); 
  230.  
  231. //Constructs GUI. 
  232. JPanel mine = new JPanel(); 
  233. mine.setLayout (null); 
  234.  
  235. mine.add (labelTitle); 
  236. labelTitle.setBounds (110, 30, 700, 15); 
  237.  
  238. mine.add (labelInstructions); 
  239. labelInstructions.setBounds (80, 70, 450, 15); 
  240.  
  241. mine.add (Principle); 
  242. Principle.setBounds (130, 110, 100, 25); 
  243.  
  244. mine.add(Interest); 
  245. Interest.setBounds (130 , 140, 120, 25); 
  246.  
  247. mine.add(Calculate); 
  248. Calculate.setBounds(350, 145, 115, 25); 
  249.  
  250. mine.add(Duration); 
  251. Duration.setBounds(130, 170, 120, 25); 
  252.  
  253. mine.add (fieldLoanAmount); 
  254. fieldLoanAmount.setBounds (240, 110, 100, 25); 
  255.  
  256. mine.add(fieldInterest); 
  257. fieldInterest.setBounds (240, 140, 100, 25); 
  258.  
  259. mine.add(fieldDuration); 
  260. fieldDuration.setBounds (240, 170, 100, 25); 
  261.  
  262. mine.add(LabelOr); 
  263. LabelOr.setBounds(80, 220, 400, 25); 
  264.  
  265. mine.add (Calculate7); 
  266. Calculate7.setBounds (40, 260, 125, 30); 
  267.  
  268. mine.add (Calculate15); 
  269. Calculate15.setBounds (180, 260, 125, 30); 
  270.  
  271. mine.add (Calculate30); 
  272. Calculate30.setBounds (320, 260, 125, 30); 
  273.  
  274. mine.add (Payment); 
  275. Payment.setBounds (130, 300, 100, 25); 
  276.  
  277. mine.add (fieldPayment); 
  278. fieldPayment.setBounds (240, 300, 100, 25); 
  279. fieldPayment.setEditable (false); 
  280.  
  281. mine.add (AmortizationTable); 
  282. AmortizationTable.setBounds (180, 330, 300, 25); 
  283.  
  284. mine.add (scrollPane); 
  285. scrollPane.setBounds (50, 350, 380, 260); 
  286. areaAmortTable.setEditable(false); 
  287.  
  288. mine.add (Clear); 
  289. Clear.setBounds (110, 620, 125, 30); 
  290.  
  291. mine.add (Exit); 
  292. Exit.setBounds (250, 620, 125, 30); 
  293.  
  294. this.setContentPane(mine); 
  295. this.pack(); 
  296. this.setTitle("Mortgage Amortization Calculator"); 
  297.  
  298. setScreenSize(Toolkit.getDefaultToolkit().getScreenSize()); 
  299. setSize(500, 700); 
  300. public double loanAmount () 
  301. double loanAmount = Double.parseDouble(fieldLoanAmount.getText()); 
  302. return loanAmount; 
  303.  
  304. public DecimalFormat getDecimalPlaces() { 
  305. return decimalPlaces; 
  306.  
  307. public void setDecimalPlaces(DecimalFormat decimalPlaces) { 
  308. this.decimalPlaces = decimalPlaces; 
  309.  
  310. public DecimalFormat getDecimalPlaces2() { 
  311. return decimalPlaces2; 
  312.  
  313. public void setDecimalPlaces2(DecimalFormat decimalPlaces2) { 
  314. this.decimalPlaces2 = decimalPlaces2; 
  315.  
  316. public DecimalFormat getDecimalPlaces4() { 
  317. return decimalPlaces4; 
  318.  
  319. public void setDecimalPlaces4(DecimalFormat decimalPlaces4) { 
  320. this.decimalPlaces4 = decimalPlaces4; 
  321.  
  322. public Dimension getScreenSize() { 
  323. return screenSize; 
  324.  
  325. public void setScreenSize(Dimension screenSize) { 
  326. this.screenSize = screenSize; 
  327.  
  328. public DecimalFormat getDecimalPlaces3() { 
  329. return decimalPlaces3; 
  330.  
  331. public void setDecimalPlaces3(DecimalFormat decimalPlaces3) { 
  332. this.decimalPlaces3 = decimalPlaces3; 
  333. // end of program 
  334.  
  335.  
  336.  
  337.  
Feb 14 '12 #7

P: 5
It works for the calculation, but does not show the Amortization.
Feb 14 '12 #8

P: 21
Hi Eric,

Replace the for loop
for (int x = 1; x <=(Inter*12); x++){
inside Calculate event(at line number 51) as
for (int x = 1; x <=(Months*12); x++){
it will work fine.
Feb 15 '12 #9

P: 5
That did the trick, Thanks so much.
Feb 15 '12 #10

Post your reply

Sign in to post your reply or Sign up for a free account.