// TimeTest5.java
// Tests the compareTo method of class Time
import java.util.StringTokenizer;
public class TimeTest5 {
public static void main(String[] args)
{
Time t1 = inputTime();
while ( true ) {
System.out.println("The time you just now entered will be "
+ "compared to the next time you enter.");
Time t2 = inputTime();
System.out.print("According to compareTo: " + t1);
if ( t1.compareTo(t2) < 0 )
System.out.print(" < ");
else if ( t1.compareTo(t2) == 0 )
System.out.print(" == ");
else
System.out.print(" > ");
System.out.println(t2);
t1 = t2;
} // while
} // class main
public static Time inputTime()
{
try {
System.out.print("Enter a 24-hour time in "
+ "HH:MM:SS or HH MM SS format "
+ "(or X to quit);>");
String line = ConsoleInput.readLine();
if ( line.equalsIgnoreCase("X") )
System.exit(0);
StringTokenizer st = new StringTokenizer(line, " :\t");
int h = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int s = Integer.parseInt(st.nextToken());
Time time = new Time(h, m, s);
System.out.println("You entered " + time + ", or "
+ time.to12HourString()
+ ", " + time.getTotalSeconds()
+ " seconds after midnight.");
return time;
} catch ( RuntimeException re ) {
System.out.println("Not a valid time input. Try again.");
return inputTime(); // recursive call to this method itself
} // catch
} // method testTime(int, int, int)
} // class TimeTest5