Accessing ArrayLists from seperate file

Posted 23 November 2012 - 10:54 PM

I'm trying to build a program for cataloging a small library for a local club I'm a part of (This is something I wish to do, I'm not being payed or commissioned to do it, it's more of a technical exercise for me). The design of the program is such that there is a few different '.java' files, and in one of them is the ArrayList in which everything is being read and written from/to.

My problem is that I'm not sure if you can access this ArrayList within a class which has been called from a separate '.java' file. And if it can be done, how would this be done.
The ArrayList is set as a public type for the purpose of access across classes (and hopefully files).

The ArrayList is located in this file, at line 12, and the 'SearchMenu' class is called at line 95 (I commented it out as the file is currently empty).

In the 'SearchMenu.java' file, I want to access the ArrayList from 'Library.java', so it can be searched for various entries.

At this stage, I have put the 'Administration' menu in a separate class in the 'Library.java' file, I would like to have this in a separate file too, it would need to be able to edit the ArrayList though, and I wondered if saving the file before the class is loaded, then load the saved file to a separate ArrayList to edit, then save on exit, and then the Library.java would reload the edited save to the ArrayList...but I think this may be rather inefficient and rather messy.
(The Administration menu is called in case 3, on line 99, and the 'AdministrationMenu()' class starts at line 169 to 223).

Re: Accessing ArrayLists from seperate file

Posted 24 November 2012 - 07:51 AM

First things first. You have more than one scanner with System.in parameters in your runMenu method. You can get rid of all but one scanner and use it everywhere.

I don't know what your Administration and SearchMenu classes look like so I can't comment on them. I don't think it's a good idea to save your library to a text file every time so you can share it between your library and SearchMenu. But I do think that if your searchMenu is only searching the library for some sort of media, then it should be a method in the library class no?

Re: Accessing ArrayLists from seperate file

Posted 24 November 2012 - 04:59 PM

I am aware of the multiple scanners, I usually do this to make sure everything is working properly first, then I start trimming what really isn't necessary (such as the multiple scanners). The 'Administration' menu is currently in the Library file (which I have posted just above), and with the search menu, I'm yet to write it (I wanted to work out how to access the ArrayList from another file first).

I wanted to put the 'Administration menu' and the 'Search Menu' in separate files, to keep the files a little neater...But it would seem that this may not be possible, or somewhat difficult at least.

If I am reading this right, it would be far easier to simply keep it all in the one file, simply as separate classes no?

I had only just done work on inheritance and super/sub classes at Uni (in Programming 1), so I hadn't done anything quite like this, but I thought it would be a similar process to complete such a task. But by the sounds of it, I can't achieve such a feat.

Re: Accessing ArrayLists from seperate file

Posted 25 November 2012 - 09:46 PM

Scenario for problem:
While running the 'Library.java' file menu, I select the option in the menu for the Search Menu (to search for item in the ArrayList by title, author, year, etc). The SearchMenu.java file is called and run with the following line:

SearchMenu sear = new SearchMenu();

That bit is fine.

Problem:
The search's require access to the ArrayList, which is in 'Library.java'. The 'Search' methods are written similar to the following;