Arrays of Arrays in Java

This is a nasty one for me… I’m a PHP guy working in Java on a JSP project. I know how to do what I’m attempting through too much code and a complete lack of finesse.

I’d prefer to do it right. Here is the situation:

I’m writing a small display to show customers what days they can water their lawns based on their watering group (ABCDE) and what time of year it is. Our seasons look like this: Summer (5-1 to 8-31) Spring (3-1 to 4-30) Fall (9-1 to 10-31) Winter (11-1 to 2-28)

An example might be:

If I’m in group A, here would be my allowed times: Winter: Mondays only Spring: Tues, Thurs, Sat Summer: Any Day Fall: Tues, Thurs, Sat

If I was writing this in PHP I would use arrays like this:

//M=Monday,t=Tuesday,T=Thursday.... etc
$schedule["A"]["Winter"]='M';
$schedule["A"]["Spring"]='tTS';
$schedule["A"]["Summer"]='Any';
$schedule["A"]["Fall"]='tTS';
$schedule["B"]["Winter"]='t';

I COULD make the days arrays (array(“Tuesday”,”Thursday”,”Saturday”)) etc, but it is not necessary for what I’m really trying to accomplish.

I will also need to setup arrays to determine what season I’m in:

$seasons["Summer"]["start"]=0501;
$seasons["Summer"]["end"]=0801;

Can anyone suggest a really cool way to do this? I will have today’s date and the group letter. I will need to get out of my function a day (M) or a series of days (tTS), (Any).

Answer

You could do essentially the same code with Hashtables (or some other Map):

Hashtable<String, Hashtable<String, String>> schedule
    = new Hashtable<String, Hashtable<String, String>>();
schedule.put("A", new Hashtable<String, String>());
schedule.put("B", new Hashtable<String, String>());
schedule.put("C", new Hashtable<String, String>());
schedule.put("D", new Hashtable<String, String>());
schedule.put("E", new Hashtable<String, String>());

schedule.get("A").put("Winter", "M");
schedule.get("A").put("Spring", "tTS");
// Etc...

Not as elegant, but then again, Java isn’t a dynamic language, and it doesn’t have hashes on the language level.

Note: You might be able to do a better solution, this just popped in my head as I read your question.