Jose Antonio
Jose Antonio

Reputation: 444

Messages in java

I have a challenge:

Imagine you have a set of messages like this:

Code / Message

200567 = A new user was created
462001 = Unknown client number
...

I'm trying to find the neatest, lightest, and easiest to maintain way to use this messages in java.

The rules are:

  1. You need to be able to access the message by its code
  2. You need to be able to print the code
  3. You need to be able to easily change the number of a code in the future

Other notes:

So, anybody has ideas?
(Sorry about my lousy English)

Upvotes: 1

Views: 417

Answers (4)

Woot4Moo
Woot4Moo

Reputation: 24326

I would put this in an enumeration.

public enum MessageType {  

NEW_USER("String 123", "A new user was created");
private String code, message;

private MessageType(String code, String message) { this.code = code;
this.message = message
}

I would go for enumerations because they are to be checked into source control, typically in the environments where I work the properties file is meant to be configured by the individual. Such as an ant build properties file.

Upvotes: 1

DGH
DGH

Reputation: 11549

Your best bet is probably to have a HashMap<string, string> or HashMap<int, string> that is a static member of your main class or some other relevant class. Write a properties file and a simple method that is called near the start of the program to populate the HashMap.

http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html

Upvotes: 0

anon
anon

Reputation:

If I had to implemtn this I would use a HashMap. The code are the keys and the message the values.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240918

Go for Properties File. Use ResourceBundle

200567 A new user was created
462001 Unknown client number

Upvotes: 4

Related Questions