In this loan class, I need another option within it displaying if the user wants
ID: 3575411 • Letter: I
Question
In this loan class, I need another option within it displaying if the user wants to take out a loan for Mortgage or Student loans. I need this code to display, and allowing the user to add the amounts. PLEASE help me for adding additional loan options! do not just copy and paste the code, like everyone else.
loan.java file:
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 showpayment =new JLabel("Are you making a payment on your loan",SwingConstants.CENTER);
JPanel mainpanel =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
mainpanel.setLayout(new BorderLayout()); // setting layout of the display panel to borderLayout
mainpanel.add(showpayment, BorderLayout.CENTER); // displaying payment label
mainpanel.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(mainpanel); // 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
}
}
}
----------------------------------------------------------------------------------------------
customer.java file:
public class customer extends math{
public customer() {
balChk=3000;
balSav=3000;
loanPay=10000;
}
public static double getBalanceCheck() {
return balChk;
}
public static double getBalanceSav() {
return getBalanceSav();
}
public double getLoanPayment() {
return loanPay;
}
public static void setBalanceCheck(double a) {
balChk = a;
}
public static void setBalanceSav(double a) {
balSav = a;
}
public void setLoanPayment(double a) {
loanPay = a;
}
}
Explanation / Answer
public class customer
{
private double balChk;
private double balSav;
private double loanPay;
private int numMonths;
public customer()
{
balChk=3000;
balSav=3000;
loanPay=10000;
}
public void setMonths(int numMonths){
this.numMonths=numMonths;
}
public int getMonths(){
return numMonths;
}
public double getBalanceCheck() {
return balChk;
}
public double getBalanceSav() {
return balSav;
}
public double getLoanPayment() {
return loanPay;
}
public void setBalanceCheck(double a) {
balChk = a;
}
public void setBalanceSav(double a) {
balSav = a;
}
public void setLoanPayment(double a) {
loanPay = a;
}
}
----------------------------------------------------------------------------------------------------------------------------------
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
{
//main method
public static void main(String[] args) {
//create an instance of the loan class
new loan();
}
JLabel showpayment =
new JLabel("Are you making a payment on your loan",
SwingConstants.CENTER);
JPanel mainpanel =new JPanel();
JPanel buttons=new JPanel();
customer cust=new customer();
JButton yesButton =new JButton("Yes");
JButton noButton =new JButton("No");
//Create two radio buttons for mortagae and student
JRadioButton mortageRdBtn=new JRadioButton("Mortgage");
JRadioButton stdRdBtn=new JRadioButton("Student");
//create two panels for radio and txt panels
JPanel radioPanel=new JPanel();
JPanel txtPanel=new JPanel();
//Create a text field for months text field
JTextField monthstxtField=new JTextField(10);
public loan(){
super("Loan Payment"); // setting title of the window to Load Payment
//setSize(300,450); // setting size of the window
setDefaultCloseOperation(EXIT_ON_CLOSE); // setting what should happen when window is closed
mainpanel.setLayout(new BorderLayout()); // setting layout of the display panel to borderLayout
mainpanel.add(showpayment, BorderLayout.CENTER); // displaying payment label
mainpanel.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(mainpanel,BorderLayout.NORTH); // adding this panel to the window
//Create a button group for mutual exclusive of radio buttons
ButtonGroup btnGrp=new ButtonGroup();
btnGrp.add(mortageRdBtn);
btnGrp.add(stdRdBtn);
//add radio buttons to radio panel
radioPanel.add(mortageRdBtn);
radioPanel.add(stdRdBtn);
radioPanel.setBorder(BorderFactory.createTitledBorder("Select Loan Type"));
add(radioPanel,BorderLayout.CENTER);
JLabel loanMonthsLabel =
new JLabel("Enter number of months of loan ",
SwingConstants.CENTER);
//add controls to add to txtpanel
txtPanel.add(loanMonthsLabel);
txtPanel.add(monthstxtField);
add(txtPanel,BorderLayout.SOUTH);
//auto set the controls on the frame
pack();
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);
cust.setLoanPayment(res); // make the loan payment
JOptionPane.showMessageDialog(new JFrame(),cust.getLoanPayment()); // 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(),
" "+cust.getBalanceCheck()+" "+
cust.getBalanceSav()+" "
+cust.getLoanPayment()+" ");
}
}
}
else if(e.getSource()== noButton){ // do this when yesButton is clicked
this.dispose();
}
}
}
--------------------------------------------------------------------------------------------------------
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.