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.
Code:
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_CLOSE); 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,loanAmount; 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(""); 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().getScreenSize()); setSize(500, 700); } public double loanAmount () { double loanAmount = Double.parseDouble(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
Comment