Question
Need help with this assignment.
Write the program in Java (with a graphical user interface) so that it will allow the user to select which way they want to calculate a mortgage: by input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage payment or by input of the amount of a mortgage and then select from a menu of mortgage loans:
- 7 year at 5.35%
- 15 year at 5.5 %
- 30 year at 5.75%
In either case, display the mortgage payment amount and then, list the loan balance and interest paid for each payment over the term of the loan. Allow the user to loop back and enter a new amount and make a new selection, or quit. Insert comments in the program to document the program.
Explanation / Answer
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.NumberFormat; public class Mortgage extends JFrame { // define window width and height private static final int FRAME_WIDTH = 400; private static final int FRAME_HEIGHT = 400; // define text fields for amount of the mortgage, term and // interest rate private JTextField amountText; private JTextField termText; private JTextField rateText; // define buttons to calculate mortgage payment amount and // quit window private JButton calculateButton; private JButton quitButton; // define text area to display mortgage payment amount followed // by the loan balance and interest paid for each payment over // the term of the loan private JTextArea outputTextArea; // term (in years) array private int[] terms = {7, 15, 30}; // annual interest rate (%) array private double[] rates = {5.35, 5.50, 5.75}; // constructor public Mortgage() { super("Mortgage Payment Calculator"); Container container = getContentPane(); // set the Border Layout container.setLayout(new BorderLayout()); // create menu JMenuBar menuBar = new JMenuBar(); // Mortgage Loans menu JMenu loanMenu = new JMenu("Mortgage Loans"); menuBar.add(loanMenu); // add menu items JMenuItem term7MenuItem = new JMenuItem("7 year at 5.35%"); JMenuItem term15MenuItem = new JMenuItem("15 year at 5.5%"); JMenuItem term30MenuItem = new JMenuItem("30 year at 5.75%"); JMenuItem quitMenuItem = new JMenuItem("Quit"); loanMenu.add(term7MenuItem); loanMenu.add(term15MenuItem); loanMenu.add(term30MenuItem); loanMenu.addSeparator(); loanMenu.add(quitMenuItem); setJMenuBar(menuBar); // create labels JLabel label1 = new JLabel("Amount: "); JLabel label2 = new JLabel("Term: "); JLabel label3 = new JLabel("Interest Rate(%): "); // create text fields amountText = new JTextField(10); termText = new JTextField(10); rateText = new JTextField(10); // create calculate and quit buttons calculateButton = new JButton("Calculate"); quitButton = new JButton("Quit"); // create panel JPanel panel = new JPanel(new GridLayout(4, 2, 5, 5)); panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); // add labels and text fields panel.add(label1); panel.add(amountText); panel.add(label2); panel.add(termText); panel.add(label3); panel.add(rateText); panel.add(calculateButton); panel.add(quitButton); // add panel at north container.add(panel, BorderLayout.NORTH); // create output text area and make it not editable // and set font as "Courier New" outputTextArea = new JTextArea(); outputTextArea.setEditable(false); outputTextArea.setFont(new Font("Courier New", Font.PLAIN, 12)); // and output text area at the center JScrollPane scrollPane = new JScrollPane(outputTextArea); scrollPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); container.add(scrollPane, BorderLayout.CENTER); // 7 years at 5.35% loan menu item click event listener term7MenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double amount; try { amount = Double.parseDouble(amountText.getText()); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Amount must be numeric", "Error", JOptionPane.ERROR_MESSAGE); return; } if (amount