Question

I would like to store some data such as first name, last name, date of birth, address, telephone number, etc. There will probably be about 50 fields in total. What would be a good way to store that data? I was thinking of just using a class named Person and put bunch of members in it for the fields. Ex:

public class Person {
    private String fName;
    private String mName;
    private String lName;
    private String dob;
    private String address;
    private String phNumber;
    private String sex;
    private String aka;
    // etc...
    public void setfName(String st) {
        fName = st;
    }
    public String getfName() {
        return fName;
    }
    // etc....
}

Is there a better way to perform this task?

EDIT:

I should also mention that each of the data fields will be (hopefully if I figure out how to do it) inserted into a Microsoft Word template. I'm not necessarily planning on working with the data beyond that scope. What I have at this moment is a pdf document, which I was able to extract text from. I will parse that text and attempt to get all the data out of it. I will then insert that data into the word template (if it's possible).

Was it helpful?

Solution

It depends on what your needs are.

Using a String for every piece of information is probably a bad idea. You should use appropriate data types for the information you're storing.

  • A Calendar instance for date of birth
  • An enum for gender
  • A custom class for addresses which has fields for house number, street, unit number, city, state, zip code, etc.

OTHER TIPS

You could store them in a hashmap:

private Map<String, String> data = new HashMap<String, String>();

public void insertData(String key, String value)
{
    data.put(key, value);
}

public String lookup(String key)
{
    return data.get(key);
}
insertData("fName", "blah blah");
insertData("address", "blah blah");

String address = lookup("address");

You could get a list of all available keys like this:

Set<String> keys = data.keySet();

Another option is to use the builder pattern.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top