Results 1 to 6 of 6
Thread: Arraylist accessibility
- 02-25-2011, 11:38 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
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.
- 02-25-2011, 12:13 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 02-25-2011, 01:18 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
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.
- 02-25-2011, 01:22 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Add a getNumAccounts() and getAccount(int index) to the class containing the list.
- 02-25-2011, 05:58 PM #5
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
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.
- 02-25-2011, 11:51 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
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
-
sorting arraylist based on another arraylist
By busdude in forum New To JavaReplies: 4Last Post: 02-07-2011, 11:48 AM -
Suggestion to Java developers: more accessibility
By lamborman in forum Advanced JavaReplies: 10Last Post: 01-22-2011, 07:30 AM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
Accessibility of JComboBox problem
By abedules78 in forum AWT / SwingReplies: 0Last Post: 12-26-2008, 01:11 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks