I can not get my calculator to calculate at all. please help me. thank you
Code:package mortgagecalculator; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import java.text.NumberFormat; import java.math.*; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JComboBox; import javax.swing.JFrame; public class MortgageCalculator { double amount; double term; double payments; double amortize; double interestRateMonths; double monthlyInterestPayment; double monthlyPrincipalPayment; int termMonths; double balance; int Labels; // Instance Variables JLabel amountLabl, termLabl, IntrateLabl, paymentsLabl, amortizeLab1; JTextField amountTxf, termTxf, intrateTxf, paymentsTxf, amortizeTxf; JButton calcBtn; JButton clearButton; JButton exitButton; JFrame frame; JComboBox Cbox; public MortgageCalculator() { System.out.println("Mortgage Calculator."); String Labels[] = { "7 Years at 5.35%", "15 years at 5.5%", "30 years at 5.75%",}; amountLabl = new JLabel(" Loan Amount In Whole Dollars"); termLabl = new JLabel(" Terms In Years"); IntrateLabl = new JLabel(" Interest Rate - Decimal Format (0.000)"); paymentsLabl = new JLabel(" Monthly Payment Amount"); amountTxf = new JTextField(); JComboBox cBox1 = new JComboBox(Labels); Cbox = new JComboBox(Labels); termTxf = new JTextField(); intrateTxf = new JTextField(); amortizeTxf = new JTextField(); amortizeLab1 = new JLabel("Amortization"); paymentsTxf = new JTextField(); calcBtn = new JButton("Calculate"); clearButton = new JButton("Clear"); exitButton = new JButton("Exit"); frame = new JFrame("Mortgage Calculator"); frame.setLayout(new GridLayout(16,16)); frame.add(amountLabl); frame.add(amountTxf); frame.add(cBox1); /* Remove these frames when action listener is working */ frame.add(termLabl); frame.add(termTxf); frame.add(IntrateLabl); frame.add(intrateTxf); frame.add(paymentsLabl); frame.add(paymentsTxf); // Added amortize textfield and label frame.add(amortizeLab1); frame.add(amortizeTxf); frame.add(calcBtn); frame.add(clearButton); frame.add(exitButton); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); clearButton.addActionListener(new Handler()); calcBtn.addActionListener(new Handler()); exitButton.addActionListener(new Handler()); frame.setLocation(300, 300); frame.pack(); frame.setVisible(true); } private class Handler implements ActionListener { public void actionPerformed(ActionEvent event) { Object command = event.getSource(); if(command == calcBtn) { calculate(); } if(command == clearButton) { amountTxf.setText(null); termTxf.setText(null); intrateTxf.setText(null); paymentsTxf.setText(null); amortizeTxf.setText(null); } if(command == exitButton) { System.exit(0); } } } public static void main(String[] args) { new MortgageCalculator(); } private void calculate() { DecimalFormat decimalPlaces=new DecimalFormat("0,000.00"); String Labels = (String) Cbox.getSelectedItem(); if (Labels.equals("7 years at 5.35%")) { double amount1 = Double.parseDouble(amountTxf.getText()); int years = 7; int months = (years * 12); double interest = .0535; double termMonth = months; double terms = 7; double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12))))); paymentsTxf.setText("" + decimalPlaces.format(payments)); } if (Labels.equals("15 years at 5.5%")) { double amount2 = Double.parseDouble(amountTxf.getText()); int years = 15; int months = (years * 12); double interest = .055; double termMonth = months; double terms = 15; double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12))))); paymentsTxf.setText("" + decimalPlaces.format(payments)); } if (Labels.equals("30 years at 5.75%")) { double amount3 = Double.parseDouble(amountTxf.getText()); int years = 30; int months = (years * 12); double interest = .0575; double termMonth = months; double terms = 15; double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12))))); paymentsTxf.setText("" + decimalPlaces.format(payments)); } /* This is the area for the amortization process */ // displays monthly mortgage payment resulting from above calculation System.out.println("\n\nBased on the above criteria, " + "your monthly payment will be: " + "$" + decimalPlaces.format(payments)); // formula(s) to calculate monthly interest and principal payments monthlyInterestPayment = (amount * interestRateMonths); monthlyPrincipalPayment = (payments - monthlyInterestPayment); // start while loop while(termMonths > 0); // { // information to display System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) + "\t\t$" + decimalPlaces.format(monthlyInterestPayment) + "\t\t$" + decimalPlaces.format(amount)); // decrement months termMonths--; // calculate interest and principal payments monthlyInterestPayment = (amount * interestRateMonths); monthlyPrincipalPayment = (payments - monthlyInterestPayment); balance = (payments - monthlyPrincipalPayment); System.out.println(balance); /**/ /* Put amortization into a Jtextarea */ //payments - principal show each payment }}


Reply With Quote
Bookmarks