0

According to my assignment which asks to develop a small-scale Student Accommodation Management System :

The application should be developed using object-oriented concepts using Student class and Apartment class, implementing the appropriate data fields and methods for the classes. Data may be stored in collections i.e. array of objects, vectors, etc. or into data files except a database.

So far, I have worked with Sets. I am not sure if it the right way but I added HashSets to my classes. Example:

public static Set<Apartment> listOfApartments = new HashSet<Apartment>();
// in Apartment Class) 

Now that I just realized I actually need persistent collections or some solutions to actually store the data permanently.

Any Suggestions?

Oleksi
  • 12,947
  • 4
  • 56
  • 80
Sasha
  • 29
  • 1
  • 2
  • 8

2 Answers2

3

If I where you I would use something such as an ArrayList to store data, especially students. Sets do not allow duplicate data so this could cause problems down the line.

With regards to persisting your data, you should take a look at the ObjectOutputStream to store your objects and to the ObjectInputStream to load them back into your application. You can take a look here for an ObjectStreams tutorial.

What I would recommend though is to use something such as XStream (you can see how to use it here). This will allow your application to store data in a human readable way (which is helpful for debugging) and will also allow your data to be read by different programming languages.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • 1
    +1 XStream then save it do file using BufferedWriter... and read using BufferedReader. – Rudy Apr 25 '12 at 07:34
0

If Appartment is Serializable, then Set<Apartment> is also Serializable and doens't require any extra work to persist it using java.io classes

To make a class Serializable, you must :

  1. make it implement the interface java.io.Serializable
  2. add a default constructor

It is that easy

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89