0

I am trying to create a banking system which can state if the user has selected the SavingJRadioButton or the CheckingJRadioButton. Before using getters and setters, I used public static variables which worked, but my teacher wants getters and setters. So far my code looks like this:

public class CreateAccount extends JFrame
{
    public JRadioButton
    checkingJRadioButton,
    savingJRadioButton;


    public JRadioButton getCheckingJRadioButton()
    {
        return checkingJRadioButton;
    }

    public void setCheckingJRadioButton(JRadioButton cJB) 
    {
        checkingJRadioButton = cJB;
    }

    public JRadioButton getSavingJRadioButton() 
    {
        return savingJRadioButton;
    }

    public void setSavingJRadioButton(JRadioButton sJB) 
    {
        savingJRadioButton = sJB;
    }

    public CreateAccount()
    {
        createCreateAccount();
    }

    public void createCreateAccount()
    {

    createaccountJButton = new JButton("Create Account");
        createaccountJButton.setBounds(280, 600, 150, 50);
        createaccountJButton.addActionListener(new ActionListener()

                {
                    public void actionPerformed(ActionEvent e)
                    {
                        double balance = validatebalance();
                        String address = addressJTextField.getText();
                        String name = nameJTextField.getText();

                        if(address.equals("") || name.equals(""))
                        {
                            JOptionPane.showMessageDialog(null, "Your account was not created." + "\nPlease enter a valid address and name."
                            , "ERROR", JOptionPane.ERROR_MESSAGE);

                            addressJTextField.setBackground(Color.red);
                            nameJTextField.setBackground(Color.red);
                        }
                        else
                        {
                            if(savingJRadioButton.isSelected())
                            {
                                getSavingJRadioButton();
                                savings = new savingAccount(balance, address, name);
                                //Pass data through the constructor
                                TransactionScreen screen = new TransactionScreen(savings);
                                closeNewAccountScreen();
                                JOptionPane.showMessageDialog(null, "Your Account Was Created!", "NEW ACCOUNT", JOptionPane.INFORMATION_MESSAGE);
                            }
                            else if(checkingJRadioButton.isSelected())
                            {
                                //open checking account
                                getCheckingJRadioButton();
                                checkings = new checkingAccount(balance, address, name);
                                TransactionScreen screen = new TransactionScreen(checkings);
                                closeNewAccountScreen();
                                JOptionPane.showMessageDialog(null, "Your Account Was Created!", "NEW ACCOUNT", JOptionPane.INFORMATION_MESSAGE);

                            }

                        }
                    }

                }

            );

        contentPane.add(createaccountJButton);
        }

That was my CreateAccount class, but now I have to pass it to the screen containing the withdraw and deposit buttons, but I think this is where it all goes down.

public class TransactionScreen extends JFrame
{
    public JButton 
    depositJButton,
    withdrawJButton,
    createaccountJButton;

    private JLabel 
    depositPictureJLabel,
    withdrawPictureJLabel;

    private JLabel screenJLabel;
    private CreateAccount createAccount;
    private savingAccount savings;
    private checkingAccount checkings;
    private CreateAccount checkingJRadioButton;


    public TransactionScreen()
    {
        createTransactionsScreen();
        createAccount = new CreateAccount();
    }
    public void createTransactionsScreen()
    {
        Container contentPane = getContentPane(); 
        contentPane.setLayout(null);
        contentPane.setBackground(Color.white);

        depositJButton = new JButton("Deposit");
        depositJButton.setBounds(430, 150, 150, 75);
        contentPane.add(depositJButton);
        depositJButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
             {


                if(createAccount.savingJRadioButton.isSelected())
                {
                    createAccount.getSavingJRadioButton();
                    DepositScreen depositScreen = new DepositScreen(savings);
                    closeTransactionScreen();
                }
                if(createAccount.checkingJRadioButton.isSelected())
                {
                    createAccount.getCheckingJRadioButton();
                    DepositScreen depositScreen  = new DepositScreen(checkings);
                    closeTransactionScreen();
                }


             }
        }
    );
    }

Now for some reason the JRadioButtons do not pass to the Transaction Screen. Please help.
Thanks.

2
Contributors
1
Reply
16
Views
1 Year
Discussion Span
Last Post by JamesCherrill
1

You call the getters OK, but you don't do anything with the JButtons that they return.

In your original code you used public variables to refer to the radio buttons. Now you just need to replace those public variables by calls to the getter methods. At the same time you are moving from using static members to using proper instance members. Eg

before...

class A {
    static public String s;
    ...
 }
 class B {
    ...
    System.out.println(A.s);
    ...
 }

after...

class A {
    public String getS() {...
    ...
 }
 class B {
    ...
    A aInstance = new A();
    ...
    System.out.println(aInstance.getS());
    ...
 }

Yes, I know it looks like a harder way to do the same thing, but trust me (and your teacher) there are very very good reasons why this is the better approach for real-life programs. You will see that before too long.

Edited by JamesCherrill

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.