Question: 1. Integrate a user login page into the following program: 2. Add a way to compare different kinds of mortgages into the program: 3. Add
1. Integrate a user login page into the following program:
2. Add a way to compare different kinds of mortgages into the program:
3. Add a download button so that a pdf/text document containing the mortgage details can be downloaded
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MortgageCalculatorGUI extends JFrame implements ActionListener {
// Declare components
private JLabel clientNameLabel, mortgageNameLabel, totalMortgageLabel, downPaymentLabel, yearlyInterestLabel, loanDurationLabel;
private JTextField clientNameField, mortgageNameField, totalMortgageField, downPaymentField, yearlyInterestField, loanDurationField;
private JCheckBox downPaymentIsPercentageCheckBox;
private JButton calculateButton;
private JTable amortizationTable;
private JScrollPane amortizationTableScrollPane;
// Constructor
public MortgageCalculatorGUI() {
super("Mortgage Calculator");
// Initialize components
clientNameLabel = new JLabel("Client Name:");
clientNameField = new JTextField(20);
mortgageNameLabel = new JLabel("Mortgage Name:");
mortgageNameField = new JTextField(20);
totalMortgageLabel = new JLabel("Total Mortgage:");
totalMortgageField = new JTextField(20);
downPaymentLabel = new JLabel("Down Payment:");
downPaymentField = new JTextField(20);
downPaymentIsPercentageCheckBox = new JCheckBox("Percentage", true);
yearlyInterestLabel = new JLabel("Yearly Interest Rate:");
yearlyInterestField = new JTextField(20);
loanDurationLabel = new JLabel("Loan Duration (in years):");
loanDurationField = new JTextField(20);
calculateButton = new JButton("Calculate");
calculateButton.addActionListener(this);
// Set up amortization table
String[] columnNames = {"Month", "Balance", "Payment", "Interest", "Principal"};
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
amortizationTable = new JTable(tableModel);
amortizationTableScrollPane = new JScrollPane(amortizationTable);
// Set up layout using GridBagLayout for more flexibility
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Add components to the frame using GridBagLayout constraints
c.gridx = 0;
c.gridy = 0;
add(clientNameLabel, c);
c.gridx = 1;
c.gridy = 0;
add(clientNameField, c);
c.gridx = 0;
c.gridy = 1;
add(mortgageNameLabel, c);
c.gridx = 1;
c.gridy = 1;
add(mortgageNameField, c);
c.gridx = 0;
c.gridy = 2;
add(totalMortgageLabel, c);
c.gridx = 1;
c.gridy = 2;
add(totalMortgageField, c);
c.gridx = 0;
c.gridy = 3;
add(downPaymentLabel, c);
c.gridx = 1;
c.gridy = 3;
add(downPaymentField, c);
c.gridx = 2;
c.gridy = 3;
add(downPaymentIsPercentageCheckBox, c);
c.gridx = 0;
c.gridy = 4;
add(yearlyInterestLabel, c);
c.gridx = 1;
c.gridy = 4;
add(yearlyInterestField, c);
c.gridx = 0;
c.gridy = 5;
add(loanDurationLabel, c);
c.gridx = 1;
c.gridy = 5;
add(loanDurationField, c);
c.gridx = 0;
c.gridy = 6;
add(calculateButton, c);
c.gridx = 0;
c.gridy = 7;
c.gridwidth = 3;
add(amortizationTableScrollPane, c);
// Set up frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
// Event handler
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == calculateButton) {
// Get values from text fields
String clientName = clientNameField.getText();
String mortgageName = mortgageNameField.getText();
double totalMortgage = Double.parseDouble(totalMortgageField.getText());
double downPayment = Double.parseDouble(downPaymentField.getText());
double yearlyInterest = Double.parseDouble(yearlyInterestField.getText());
int loanDuration = Integer.parseInt(loanDurationField.getText());
// Calculate values
double monthlyInterest = yearlyInterest / 12;
double monthlyPayment = totalMortgage * monthlyInterest / (1 - Math.pow(1 + monthlyInterest, -loanDuration * 12));
double totalPayment = monthlyPayment * loanDuration * 12;
double totalInterest = totalPayment - totalMortgage;
// Add values to amortization table
DefaultTableModel tableModel = (DefaultTableModel) amortizationTable.getModel();
tableModel.setRowCount(0);
double balance = totalMortgage - downPayment;
for (int i = 1; i <= loanDuration * 12; i++) {
double interest = balance * monthlyInterest;
double principal = monthlyPayment - interest;
balance -= principal;
tableModel.addRow(new Object[]{i, balance, monthlyPayment, interest, principal});
}
}
}
// Main method
public static void main(String[] args) {
new MortgageCalculatorGUI();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
