tags:

views:

124

answers:

5

I am creating a prison system where I need to store the names and because I need to print out all the prisoner information in one of the methods. I want to make it remember and store information such as name, id and crimes etc. How can I go about doing this?

About the posted answers, I don't think it needs to be something that complicated because I haven't learnt any of this for the assignment. All I want is for my program to print out the prisoner ID, name, starting and ending date, crime with just one run of the program after I am prompted to enter the information.

INPUT/OUTPUT

New Prisoner Enter Name: Enter crime: Enter Name: Enter crime:

Prisoner information (name) has committed (crime) (name) has committed (crime)

+2  A: 

The short answer is "a database."

Your question indicates that the following could be overwhelming but it could be worth some effort to read about "Macto," an end-to-end sample Ayende Rahein has been writing about.

David in Dakota
are there examples of code from what he's talking about?
Karen
A: 

Where do you want store information ? If you want store information in program (memory), you can use a static member variables,like this:

// Prisoner.java
class Prisoner {
 public String Name;
 public int Age;
}

// Prisoners.java
class Prisoners {
 public static Prisoner[] GetAll() {
  Prisoner[] _data;
  // Load from database to _data;
  return _data;
 }
}

// test.java
class test() {
 public static void out() {
  System.out.println(main.allPrisoner.getLength());
 }
}

// main.java

public class main{
 public static Prisoner[] allPrisoner; 

 public static main(String args[]){
  public allPrisoner = Prisoners.GetAll();
  // From now all prisoners will be stored in program memory until you close it
 }
}

So, If you are Web Development, you can use WebCache

i'm confused on what each part of the code does..
Karen
I am new in this website, so I don't why that does
+1  A: 

If these informations need to persist beyond the life of the VM, you'll have to write them on a physical storage (actually, persistence is the mechanism that allow to pass from a physical storage to an in memory representation).

There are several solutions for this purpose:

  • Java Object Serialization
  • A prevalent system with a library like Prevalayer
  • XML serialization with a library like XStream
  • A database (relational or not)

Serialization is Java built-in persistence mechanism but is very fragile. Prevalence is based on serialization but I have no experience with it and I'm not sure it solves the weakness of serialization. XML serialization is interesting and quite fast to put in place, especially with a library like XSteam. Finally, a database is the most "standard" solution but introduces some complexity. Depending on your needs, use straight JDBC or JPA for the data access.

My advice: If you don't need a database, go for XML serialization and use XStream. See the Two Minute Tutorial on XStream web site to get started. If you don't need persistence at all (beyond the life of the VM), just store the prisoners in a List.

Pascal Thivent
A: 

If you are looking to use a database, one place to start is with Hibernate. Its a java library that can provide java object to relational database table mapping.

If you want to persist to a file system using an object serialization routine, I'd recommend XStream to serialize XML or JSON text.

Based on the added text to the question, I'd recommend having a look at XStream just because it is so simple to use if you need to get the data structures to a file on the disk. However, more basically...

You probably just need to make a Prisoner class that has the stuff you need Prisoner to have, such as a name, identifier, etc, etc.

public class Prisoner
{
   private String name;
   private int identifier;

   public Prisoner(String aName, int anId)
   {
      name = aName;
      identifier = anId;
   }   

   public String toString()
   {
      return "Prisoner[ name = " + name + ", id = " + identifier + " ]";
   }
}

Then you can store them in a Map<String, Prisoner> to make finding them easier.

Map<String, Prisoner> prisonMap = new HashMap<String, Prisoner>();

To enter them in from the command line, you'll probably need to use System.in Sun provides a good tutorial for it.

If you just want to print them back out on the command line, you'll iterate over the Map's keyset and get each value or just iterate over the values and just use System.out.println() to print them out.

for(Prisoner p : prisonMap.values())
{
   System.out.println(p);
}

Or, use XStream to print out the XML to file or the System.out stream.

Jay R.
is there a really simple way to do this by using simple java code? Because I have never heard of the things you mentioned and I doubt they will need us to use those things.
Karen
XStream is really simple to use. Hibernate is definitely more challenging, but if you need to use a database, at a minimum you'll be dealing with JDBC and SQL statements.
Jay R.
And apparently everyone else added mostly the same stuff... Hopefully the tutorial links are helpful.
Jay R.
+1  A: 

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");
}
Jared Russell