Results 1 to 6 of 6
  1. #1
    gigi85 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Arraylist accessibility

    Hello everyone!!
    I'm new to the forum and i would like to ask you how i could make anArraylist accessible from every class.here's my case:
    I am building a distributed application in java emulating a bank server and an ATM machine.i have created a NetBeans project that has 4 classes.
    there is one class for the accounts.
    in my server class i construct 3 account objects and i initialize an arraylist containing them.i want to access this arraylist(called "accounts") from another class(which contains the request/reply protocol).
    here's my code:

    public Account a1 = new Account ("100","1000",10000,false);
    public Account a2 = new Account ("200","2000",5000,false);
    public Account a3 = new Account ("300","3000",6000,false);


    public ArrayList<Account> accounts = new ArrayList<Account>();

    public void initAccounts() {
    accounts.add(a1);
    accounts.add(a2);
    accounts.add(a3);
    }

    i tried making it static but it didn't work.

    Anything would be helpful.
    thanks in advance!!:)
    Last edited by gigi85; 02-25-2011 at 12:10 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    add a getAccounts method to the class?

    Edit: I have the feeling that wanting to get the entire list is probably a design error though.

  3. #3
    gigi85 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    it is very possible!
    however, i need to make myself clear.the Account class has some methods for handling its components, such as getBalance, deposit etc.
    After the instantiation of the arraylist "accounts" (which holds Account objects) i want to be able to access the Account methods but from the protocol class in which my code is like accounts.get(i).getBalance()
    Last edited by gigi85; 02-25-2011 at 01:20 PM.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Add a getNumAccounts() and getAccount(int index) to the class containing the list.

  5. #5
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Default

    As masijade pointed, I see other design issues as well. Usually, we assign some kind of identification property with each account, e.g. accountNumber, username, etc.

    But, In your case you want to access the account by its index in the list. this seems a bit strange to me. I suggest adding an identifier property to each account and then storing the accounts in a map, where identifier property be used as key. Remember, this property must be unique for each account.

  6. #6
    gigi85 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    ok, i didn't mention it but my application uses client-server architecture.
    my Account class has a method for validating a pin (checking if the pin entered by the client equals that of the Account).So, i have to run the Arraylist(which holds Account objects) to see if any Account is validated, and if so i need to know this index in order to use this particular Account's methods (deposit etc)
    Anyway, my problem is solved.i had to put the Arraylist ιn my bankprotocol Class parameters and i hadn't...:confused:
    Thanks for yor time!!
    Cheers!:)

Similar Threads

  1. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 11:48 AM
  2. Suggestion to Java developers: more accessibility
    By lamborman in forum Advanced Java
    Replies: 10
    Last Post: 01-22-2011, 07:30 AM
  3. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  4. Accessibility of JComboBox problem
    By abedules78 in forum AWT / Swing
    Replies: 0
    Last Post: 12-26-2008, 01:11 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •