/* Clock24To12.java */
import java.util.ArrayList;
public class Clock24To12 implements Visitor
{
String[] hours =
{
"00","01","02","03","04","05",
"06","07","08","09","10","11",
"12","01","02","03","04","05",
"06","07","08","09","10","11"
};
String[] am_pm =
{
"AM","AM","AM","AM","AM","AM",
"AM","AM","AM","AM","AM","AM",
"PM","PM","PM","PM","PM","PM",
"PM","PM","PM","PM","PM","PM"
};
int hour;
public Object visit(Rule_Clock rule)
{
visitRules(rule.rules);
System.out.print(" " + am_pm[hour]);
return false;
}
public Object visit(Rule_Hours rule)
{
/* USE 24 HOUR OF DAY AS INDEX */
hour = Integer.parseInt(rule.spelling);
System.out.print(hours[hour]);
return false;
}
public Object visit(Rule_Minutes rule)
{
/* SIMPLY OUTPUT MINUTES */
System.out.print(rule.spelling);
return false;
}
public Object visit(Rule_Seconds rule)
{
/* SIMPLY OUTPUT SECONDS */
System.out.print(rule.spelling);
return false;
}
public Object visit(Rule_Separator rule)
{
/* SIMPLY OUTPUT SEPARATOR */
System.out.print(rule.spelling);
return false;
}
public Object visit(Terminal_StringValue value)
{
/* WON'T BE VISITED */
return true;
}
public Object visit(Terminal_NumericValue value)
{
/* WON'T BE VISITED */
return true;
}
private Boolean visitRules(ArrayList<Rule> rules)
{
Boolean terminal = Boolean.FALSE;
for (Rule rule : rules)
terminal = (Boolean)rule.accept(this);
return terminal;
}
}
/* eof */