Java: create a lookup table using hashmap or some other java collection?

Extending this question , I accepted the answer, which says that use a lookup table or hashmap for this case, since it would be better to handle several conditions .

Current design.

Class for storing messages.

    public class ProgressMessages 
    {
     public static String msg1="Welcome here .....";
     .
     .
     .
     //around 100 more similar static variables.
    }

Condition and display the correct message from the above class.

    int x=calculatedVal1(m,n); 
    int y=calculatedVal2(o,q);

    SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d=new Date();
    String s=formater.format(d);

    try {
          long d1 = formater.parse("2013-01-10 13:53:01").getTime();
          long d2=formater.parse(s).getTime();
          totaldays=Math.abs((d1-d2)/(1000*60*60*24));
         } catch (ParseException e) {
            e.printStackTrace();
         }

    if(totaldays<7&&x<20)
     {
      System.out.println("my message...1"+ProgressMessages.msg1); 
     }

    else if(totaldays<7&&x>10&&y<20)
    {
      System.out.println("my message...2"+ProgressMessages.msg2);
    }   
    ....
    //obvioulsy 100 more else-if to display 100 messages.

I really would like to know how a lookup table will help in this case?

How it should be implemented in java hashmap / hashtable etc. What would be beneficial?

---- Editing ---

I am going to use @assylias anwer as it is cleaner to use. But if I use Enums, then I have a fix.

To describe the big picture ...

. ...

1) " " + nameOfUser + " " + succssRate + "%" + + + +%.

2) " ..." + succssRate + "%" + + + "%" .

3.) " " + exerCiseName ".

Enumeration, String. ? assylas ?

+3
3

Map<Criteria, Message> lookupTable;

Criteria - , ( equals() hashCode()), .

Message - , , String, .

:

Criteria criteria = ... // gather your criteria somehow
Message msg = lookupTable.getMessage(criteria);
// use your variable setting methods here
String message = msg.toString();
+2

, , . , , .

public enum Message {

    MESSAGE1("Welcome") {
        @Override
        boolean isApplicable(long totalDays, int x, int y) {
            return totalDays < 7 && x < 20;
        }
    },
    MESSAGE2("Bye bye") {
        @Override
        boolean isApplicable(long totalDays, int x, int y) {
            return totalDays < 7 && x > 10 && y < 20;
        }
    };

    private String msg;

    Message(String msg) {
        this.msg = msg;
    }

    abstract boolean isApplicable(long totalDays, int x, int y);

    public static String lookupMessage(long totalDays, int x, int y) {
        for (Message m : Message.values()) {
            if (m.isApplicable(totalDays, x, y)) {
                return m.msg;
            }
        }
        throw new IllegalArgumentException();
    }
}

, /else if, :

System.out.println(Message.lookupMessage(1, 2, 3));

1: , , O (n), n 100 , . , , .

2: / , . ( ), .

+3

, - . 1) . 2) Flexible.If - , .

0

All Articles