Question
Java GUI program Create a GUI program that should a table of loan comparison information. Specifications: 1.GUI Program using Border Pane and Grid Pane. 2.Ask
Java GUI program
Create a GUI program that should a table of loan comparison information.
Specifications:
1.GUI Program using Border Pane and Grid Pane.
2.Ask user for Loan Amount and Number of Years.
3.There should be a Show Table button that displays the result.
4.The table should show monthly and total payments for interest rates from 5% to 8% in increments of 0.125%.
5.The interest rate should always have 3 decimal places.
6.Monthly Payment and Total Payment should be formatted as currency(NumberFormat class).
7.North quadrant of Border Pane should contain user interface(text labels, text fields, button).
8.Center quadrant should contain the results table.
Error Checking:
Inputs
1.Make sure all fields are numbers.
2.The Loan Amount can be between $1,000 and $100,000.
3.The Number of Years can be between 1 and 20.
4.All fields need to be correct when the Show Table button is pressed.
5.The user will be able to change any of the fields and press the Show Table button.
6.Use a Try-Catch blocks where you deem appropriate.
***What it should look like***
What I have so far.*******************
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.DecimalFormat; public class LoanComparison extends JFrame { private JTextField LoanAmount; private JTextField NumberOfYears; private JButton ShowTable; private JPanel Results; private JPanel Interface; private JPanel Window; public LoanComparison() { setTitle("Loan Comaprison Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel Years = new JLabel(" Number of Years:"); JLabel LoanAmnt = new JLabel(" Investment Amount:"); NumberOfYears = new JTextField(); LoanAmount = new JTextField(); ShowTable = new JButton();
JPanel Interface = new JPanel(); Interface.add(LoanAmnt); Interface.add(LoanAmount); Interface.add(Years); Interface.add(NumberOfYears); Interface.add(ShowTable);
JPanel Results = new JPanel(); Results.setLayout(new GridLayout(3,8));
JPanel Window = new JPanel(); Window.setLayout(new BorderLayout()); mainWindow.setPadding(new Insets(10, 10, 10, 10)) Window.add(Interface, BorderLayout.NORTH); Window.add(Results, BorderLayout.CENTER);
Listener run = new Listener(); ShowTable.addActionListener(run); setVisible(true); } public static void main(String[] args) { new LoanComparison(); } private void Calculate() { //Try-catch block for investment amount. Values below 1 or above 10000 cause an error. Loan loan = new Loan (); boolean correctData = true; try { loan.setAnnualInterestRate(5); int lYears = Integer.parseInt(NumberOfYears.getText()); double Loan = Double.parseDouble(LoanAmount.getText()); if( Loan 100000) { throw new Exception(); }
} catch (Exception e) { JOptionPane.showMessageDialog(null, " Error: Only values between 1000 and 100000 are accepted"); } //Try-catch block for years. Values below 1 or above 20 cause and error. try{ int lYears = Integer.parseInt(NumberOfYears.getText()); if(lYears 20){ throw new Exception();
} }catch (Exception e){ JOptionPane.showMessageDialog(null, " Error: Only 1 to 20 years accepted."); } if (correctData) // If correct data was input { // Print text area header do // Print lines of loan information until stopped { // Print current loan annual interest rate, then the corresponding monthly payment, then the corresponding total payment new DecimalFormat("0.000%").format(loan.getAnnualInterestRate()/100.0) ; new DecimalFormat("$###,##0.00").format(loan.getMonthlyPayment()); new DecimalFormat("$###,##0.00").format(loan.getTotalPayment()); loan.setAnnualInterestRate(loan.getAnnualInterestRate() + 0.125); // Increase the annual interest rate by 0.125 percent
} while (loan.getAnnualInterestRate()
private class Listener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == ShowTable) { Calculate(); }
} } } }
**********Loan Class******************Used to run program*********
public class Loan { private double annualInterestRate; private int numberOfYears; private double loanAmount; private java.util.Date loanDate;
/** Default constructor */ public Loan() { this(2.5, 1, 1000); }
/** Construct a loan with specified annual interest rate, number of years, and loan amount */ public Loan(double annualInterestRate, int numberOfYears, double loanAmount) { this.annualInterestRate = annualInterestRate; this.numberOfYears = numberOfYears; this.loanAmount = loanAmount; loanDate = new java.util.Date(); }
/** Return annualInterestRate */ public double getAnnualInterestRate() { return annualInterestRate; }
/** Set a new annualInterestRate */ public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; }
/** Return numberOfYears */ public int getNumberOfYears() { return numberOfYears; }
/** Set a new numberOfYears */ public void setNumberOfYears(int numberOfYears) { this.numberOfYears = numberOfYears; }
/** Return loanAmount */ public double getLoanAmount() { return loanAmount; }
/** Set a newloanAmount */ public void setLoanAmount(double loanAmount) { this.loanAmount = loanAmount; }
/** Find monthly payment */ public double getMonthlyPayment() { double monthlyInterestRate = annualInterestRate / 1200; double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12))); return monthlyPayment; }
/** Find total payment */ public double getTotalPayment() { double totalPayment = getMonthlyPayment() * numberOfYears * 12; return totalPayment; }
/** Return loan date */ public java.util.Date getLoanDate() { return loanDate; } }
Edit(Note): I realize that writing this program with Javafx would be much easier but we are required to use BorderLayout not BorderPane.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started