Encapsulation question regarding ArrayLists
Hi there. I've been working on a side project outside of class (I'm a Software Engineering student at Uni) which is really just a simple Banking system to help my understanding of Java. Basically the program reads existing accounts from a .txt file which are then placed into an ArrayList (they are split into Account objects).
The user can then choose to create a new account which added to the ArrayList or edit an existing one. I have three classes - the main class, Account class and UserInterface class. The main class holds all the methods such as SaveAccount (writes the accounts into file) etc. the Account class defines the account and then the UserInterface produces a UI to the user and interacts with them. The problem is I have the ArrayList in the main class and the only way to access it in UserInface is through making it public. My lecturer said this is bad since then the UserInterface class can edit it which I don't want. The thing is though I can't make the Account methods private as I want to edit them until they are put into the ArrayList so I'm not sure how to do it.
My only theory is copying the object to a duplicate class which has private settings before throwing it into the ArrayList. Would this work/be the best solution? Sorry if my question is long winded, I'll try to make it more concise if necessary.
P.S I wasn't sure if this should go in theory as I'm not posting code but it sort of addresses ArrayLists directly
Re: Encapsulation question regarding ArrayLists
If I understand your question correctly, you don't want to have a method with returns the full arraylist for fear of that class modifying the list, but without it how can another class access its contents?
Code java:
public List getList(){
...
}
If so, create a method which retrieves an object in the List at a particular position
Code java:
public Object getItem(int pos){
...
}
Or somehow index those objects so there is an easy way for the client to access an object by key or position
Re: Encapsulation question regarding ArrayLists
I think you have understood what I was trying to say, but retrieving the objects that way would still require the ArrayList to be public to be read from would it not?
Basically I need public objects until their are read into an uneditable ArrayList which can still be seen in another class. Are you trying to suggest that I set all details of object (aka account) and then pass it into a list? If so does a list just act like an ArrayList or Array? (aka you can select an index to specify which account) My account objects have an ID, Type and OverdraftLimit.
Re: Encapsulation question regarding ArrayLists
By providing a public method to access a value in the array rather than the array itself, the client has no clue how the data is stored and thus the ArrayList is encapsulated: the client doesn't care how the objects are stored so long as the method retrieves an object specified by the parameter (there are several ways to deal with whether the said object doesn't exist).
The client (UserInterface) then can add a new account to the main class, or if it wishes to edit an account it does so through the accessor method I outlined above.