Ryan Conway
Ryan Conway

Reputation: 125

Using another classes values from its getter method

I've spent a while trying different things to try to get this to homework assignment to work correctly but I can't figure it out and it's the very last part which I presume is staring me in the face. When I enter a first name and last name and press Add account and then confirm it should add an account to an arraylist and then when I press No. of Accounts It should show me how many accounts there are in total, however it keeps showing 0.

enter image description here

BasicAccountList

import java.util.*;


public class BasicAccountList
{
    private ArrayList < BasicAccount> accounts;

    /**
     * Create a BasicAccount. 
     */
    public BasicAccountList()
    {
        accounts = new ArrayList < BasicAccount>();
    }

    /**
     * Add an account to this account list.
     * @param account the accountobject to be added
     */
    public void addAccount(BasicAccount account)
    {
        accounts.add(account);
    }

    /**
     * Return the number of accounts currently held.
     * 
     * @return the number of accounts
     */
    public int getNumberOfAccounts()
    {
        return accounts.size();
    }

}

BasicAccount

public class BasicAccount
{
    private Name name;
    private String accountNumber;

    /**
     * Constructor for objects of class Account.
     * The number of pointsHeld should should be set to
     * the supplied value.
     * 
     * @param fName The Account Holder's first name 
     * @param lName The Account Holder's last name
     * @param acctNumber The account number
     */
    public BasicAccount(String fName, String lName, String acctNumber)
    {

        name = new Name (fName, lName);
        accountNumber = acctNumber; 
    }

    // accessors

    /**
     * Get the Account Holder's first name
     * 
     * @return the Account Holder's first name
     */
    public String getFirstName()
    {
        return name.getFirst();
    }

    /**
     * Get the Account Holder's last name
     * 
     * @return the Account Holder's last name
     */
    public String getLastName()
    {
        return name.getLast();
    }

    /**
     * Get the Account Holder's account Number
     * 
     * @return the Account Holder's account number
     */
    public String getAccountNumber()
    {
        return accountNumber;
    }


    public void printAccountDetails()
    {
        System.out.println( toString());
    }     

    /**
     * Return details of an account as a formated string
     * 
     * @return the account details of a particular account
     */

    public String toString()    
    {
        String output = accountNumber + " ";
        output = output + name.toString() + "\n";      
        return output;
    }

    // mutators         
    /**
     * Change the first name
     * 
     * @param fName the new first name
     * 
     */
    public void setFirstName(String fName)
    {
        name.setFirst (fName);
    }

    /**
     * Change the last name
     * 
     * @param lName the new last name
     * 
     */
    public void setLastName(String lName)
    {
        name.setLast(lName);
    }


} // end Account class

Relevant code in the GUI class

/**
 * Write a description of class HW4GUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*;

public class HW4GUI extends JFrame implements ActionListener         
{
    private BasicAccountList accounts; 
    private JPanel buttonPanel; 
    private JButton jbtAdd;
    private JButton jbtNumber;
    private JButton jbtQuit;
    private JLabel jlbAcctNo;
    private  JLabel jlbFName;
    private JLabel jlbLName;
    private JTextField jtfAcctNo;
    private  JTextField jtfFName;
    private  JTextField jtfLName;
    private int nextAcctNo;
    private JPanel textPanel;

    public HW4GUI ()
    {
        makeFrame();
        showFrame();
        nextAcctNo = 1001;


    }
    public void actionPerformed(ActionEvent ae) 
    {

        BasicAccountList accountlist = new BasicAccountList ();
        String item = ae.getActionCommand();
        String firstNameText = jtfFName.getText();
        String lastNameText = jtfLName.getText();
        String finalAccountNumber = jtfAcctNo.getText();

        if(item.equals("No. of Accounts"))
        {
            jbtAdd.setEnabled(false);
            jbtNumber.setText ("Clear");
            jlbAcctNo.setText("No. of accounts:");


            //accounts.getNumberOfAccounts();

            BasicAccount newaccount = new BasicAccount(firstNameText, lastNameText, finalAccountNumber);

            String accountTotal = Integer.toString (accountlist.getNumberOfAccounts());

            jtfAcctNo.setText (accountTotal);


        }


    }

Upvotes: 0

Views: 61

Answers (2)

Tanmay Patil
Tanmay Patil

Reputation: 7057

Instead of implementing ActionListener in the class implementing JFrame, it would be better if you do it in a separate class (possibly in an anonymous class, but any class is fine).

Now you can have two separate classes implementing ActionListener and thus two separate implementations of actionPerformed, one for each button.

Attach those ActionListeners to the respective buttons and you should be good to go.

Note: account list should be a member of the frame, so you can share it across both ActionListeners.

Good luck.

Upvotes: 0

user4668606
user4668606

Reputation:

You create another BasicAccountList inside the actionPerformed method. This means, every time you click a button, you generate a new BasicAccountList and perform all operations on this list, not the one held by HW4GUI.

Upvotes: 1

Related Questions