You haven't made particularly clear in your question as to whether you just want to store the prisoner details in memory while the program is running, or if you want to persist the prisoners to disk, so that you can close your program and load them again next time you start it.
If its the former, you can just store the prisoners in an array or a list. For example assuming your prisoner class looks something like this:
public class Prisoner {
private String name;
private String crime;
public Prisoner(String name, String crime) {
this.name = name;
this.crime = crime;
}
public String getName() {
return name;
}
public String getCrime() {
return crime;
}
}
You can then store the prisoners in a list...
List<Prisoner> prisoners = new LinkedList<Prisoner>();
prisoners.add(new Prisoner("Bob", "Murder"));
prisoners.add(new Prisoner("John", "Fraud"));
...and then iterate over the list and print the details out...
for(Prisoner p : prisoners) {
System.out.println(p.getName() + " committed " + p.getCrime());
}
If you're looking for a way to persist the prisoner details between runs of the program there are a number of possible approaches, most of which have already mentioned. In most cases a database is the best solution for storing records with JDBC being the simplest way of connecting to and interacting with a database.
For simplicity however, I would suggest storing the details in a CSV (comma separated value) file. A CSV file is simply a plain text file that stores each record on a new line, with a comma separating each field. For example:
Bob, Murder
John, Fraud
There are a number of CSV reading libraries around (see here), however its quite easy to read + write to a CSV file with no external libraries. Below is an example:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class PrisonerStore {
/**
* The file the prisoners are stored in
*/
private File store;
public PrisonerStore(File store) {
this.store = store;
}
/**
* Saves the specified prisoner to the file
* @param prisoner
*/
public void savePrisoner(Prisoner prisoner) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(store, true));
writer.write(prisoner.getName() + "," + prisoner.getCrime());
writer.newLine();
writer.close();
}
/**
* Reads all prisoners from the file and returns a list of prisoners
* @return
*/
public List<Prisoner> loadPrisoners() throws IOException{
List<Prisoner> prisoners = new LinkedList<Prisoner>();
BufferedReader br = new BufferedReader(new FileReader(store));
//Read each line of the file and create a Prisoner object from it
String line = null;
while((line = br.readLine()) != null) {
String[] parts = line.split(",");
Prisoner p = new Prisoner(parts[0], parts[1]);
prisoners.add(p);
}
br.close();
return prisoners;
}
}
In your code you can then do something like the following:
PrisonerStore store = new PrisonerStore(new File("C:\\myFile.csv"));
Prisoner p1 = new Prisoner("Bob", "Murder");
Prisoner p2 = new Prisoner("John", "Fraud");
try {
store.savePrisoner(p1);
store.savePrisoner(p2);
List<Prisoner> list = store.loadPrisoners();
for(Prisoner p : list) {
System.out.println(p.getName() + " committed " + p.getCrime());
}
} catch (IOException e) {
System.out.println("Error storing prisoners");
}