0

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);


        }


    }
5
  • what ur issue is? do you get any error? Commented Apr 23, 2015 at 1:46
  • There is no error, just that there is a problem with my arraylist and class function calls that I can't find that instead of returning say, 4 when I add 4 accounts instead returns 0. Commented Apr 23, 2015 at 1:51
  • Where is Name class? Commented Apr 23, 2015 at 1:55
  • paste.ofcode.org/BhsE2Wn26WRn7WyJxzweVf Commented Apr 23, 2015 at 2:00
  • where are makeFrame(); showFrame(); nextAcctNo = 1001; ?? Commented Apr 23, 2015 at 3:39

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I tried removing that and changing this line of code String accountTotal = Integer.toString (accounts.getNumberOfAccounts()); but now run into this problem - i.imgur.com/izm6w12.png
accounts isn't initialized in the constructor (atleast i think so). initialize it and it should work.
I tried both of these in the constructor but both don't fix it, unless I've wrote them wrong? accounts= new BasicAccountList (); ...................... BasicAccountList accounts = new BasicAccountList();
you need to change these two things: add accounts = new BasicAccountList(); in the constructor and replace accountlist.getNumberOfAccounts() in actionPerformed with accounts.getNumberOfAccounts(). if this doesn't fix it: please mark the line where the exception is thrown
Now there is no exception thrown but the original problem of 0 accounts is still there, it feels to me like the accounts aren't actually being added when I'm pressing Add Account. Could it be a problem with this line? BasicAccount newaccount = new BasicAccount(firstNameText, lastNameText, finalAccountNumber);
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.