For the loan payments, the user is to choose whether the loan is for a mortgage,
ID: 3574687 • Letter: F
Question
For the loan payments, the user is to choose whether the loan is for a mortgage, a student loan, an auto loan, or a personal loan. For Mortgage: assume $250,000 balance and payment of $2000 per month; For Student Loan: assume $55,000 balance and payment of $200 per month; For Auto Loan, assume $45,000 balance and payment of $500 per month; For Personal Loan, assume $4,000 and payment of $1000 per month.
Here is the java gui code, what it does is gives the option to make payments to your actual loan amount.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class loan extends JFrame implements ActionListener{
JLabel paymentLabel =new JLabel("Are you making a payment on your loan",SwingConstants.CENTER);
JPanel canvasPanel =new JPanel();
JPanel buttons=new JPanel();
JButton yesButton =new JButton("Yes");
JButton noButton =new JButton("No");
public loan(){
super("Loan Payment"); // setting title of the window to Load Payment
setSize(300,150); // setting size of the window
setDefaultCloseOperation(EXIT_ON_CLOSE); // setting what should happen when window is closed
canvasPanel.setLayout(new BorderLayout()); // setting layout of the display panel to borderLayout
canvasPanel.add(paymentLabel, BorderLayout.CENTER); // displaying payment label
canvasPanel.add(buttons, BorderLayout.SOUTH); // adding buttons to the layout
buttons.add(yesButton);
buttons.add(noButton);
yesButton.addActionListener(this); // setting an action listener on yesButton
noButton.addActionListener(this); // setting an action listener on noButton
add(canvasPanel); // adding this panel to the window
setVisible(true); // setting the window visible
}
// this method gets called when yesButton or noButton is clicked
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()== yesButton){ // do this when yesButton is clicked
String input=JOptionPane.showInputDialog(new JFrame(), "How much are you paying on your loan?"); // show input dialog that takes input
if(input!=null){
Double res=Double.parseDouble(input);
customer.makeLoanPayment(res); // make the loan payment
JOptionPane.showMessageDialog(new JFrame(),customer.displayLoan()); // show message indicating if the customer is done with the transaction.
int selectedOption = JOptionPane.showConfirmDialog(null,
"Are you done with your transaction?",
"Choose",
JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.YES_OPTION) { // if yes, close the window..
this.dispose();
}
else{ // if no, redo this again
JOptionPane.showMessageDialog(new JFrame()," "+customer.displayCheck()+" "+customer.displaySav()+" "+customer.displayLoan()+" ");
}
}
}
else if(e.getSource()== noButton){ // do this when yesButton is clicked
this.dispose(); // close the window
}
}
}
Explanation / Answer
Solution:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class loan extends JFrame implements ActionListener{
JLabel paymentLabel=new JLabel("Are you making a payment on your loan",SwingConstants.CENTER);
JPanel canvasPanel=new JPanel();
JPanel buttons=new JPanel();
JButton yesButton=new JButton("Yes");
JButton noButton=new JButton("No");
public loan(){
super("Loan Payment"); // setting title of the window to Load Payment
setSize(300,150); // setting size of the window
setDefaultCloseOperation(EXIT_ON_CLOSE); // setting what should happen when window is closed
canvasPanel.setLayout(new BorderLayout()); // setting layout of the display panel to borderLayout
canvasPanel.add(paymentLabel, BorderLayout.CENTER); // displaying payment label
canvasPanel.add(buttons, BorderLayout.SOUTH); // adding buttons to the layout
buttons.add(yesButton);
buttons.add(noButton);
yesButton.addActionListener(this); // setting an action listener on yesButton
noButton.addActionListener(this); // setting an action listener on noButton
add(canvasPanel); // adding this panel to the window
setVisible(true); // setting the window visible
}
// this method gets called when yesButton or noButton is clicked
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==yesButton){ // do this when yesButton is clicked
String input=JOptionPane.showInputDialog(new JFrame(), "How much are you paying on your loan?"); // show input dialog that takes input
if(input!=null){
Double res=Double.parseDouble(input);
customer.makeLoanPayment(res); // make the loan payment
JOptionPane.showMessageDialog(new JFrame(),customer.displayLoan());// show message indicating if the customer is done with the transaction.
int selectedOption = JOptionPane.showConfirmDialog(null,
"Are you done with your transaction?",
"Choose",
JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.YES_OPTION) { // if yes, close the window..
this.dispose();
}
else{ // if no, redo this again
JOptionPane.showMessageDialog(new JFrame(),"<html>"+customer.displayCheck()+"<br>"+customer.displaySav()+"<br>"+customer.displayLoan()+"</html>");
}
}
}
else if(e.getSource()==noButton){ // do this when yesButton is clicked
this.dispose(); // close the window
}
}
}