/*
RobustTimeTest.java by Craig Persiko
Test File for RobustTime class
This tests the Time class, which is used to store a time of day and output it.
It also store the current time of day, and throws exceptions if format is wrong.
*/
import javax.swing.JOptionPane;
class RobustTimeTest // feel free to convert this program to an applet if you wish.
{
public static void main(String args[])
{
RobustTime breakfastTime, lunchTime, dinnerTime;
inputCurTime();
breakfastTime = new RobustTime(9, 0); // store 9:00 AM
lunchTime = new RobustTime(12, 0); // store 12:00 PM
dinnerTime = new RobustTime(18, 30); // store 6:30 PM
System.out.print("It's now ");
RobustTime.showCurTime(false); // display current time
System.out.print("\nBreakfast is at ");
breakfastTime.show(true); // display with AM
if(breakfastTime.isLaterToday())
System.out.println("\nwhich is later today.");
else
System.out.println("\nwhich is not until tomorrow.");
System.out.print("Lunch is at ");
lunchTime.show(true); // display with PM
if(lunchTime.isLaterToday())
System.out.println("\nwhich is later today.");
else
System.out.println("\nwhich is not until tomorrow.");
System.out.print("Dinner is at ");
dinnerTime.show(false); // display without PM
if(dinnerTime.isLaterToday())
System.out.println("\nwhich is later today.");
else
System.out.println("\nwhich is not until tomorrow.");
RobustTime.addToCurTime(90); // make current time 90 minutes later
System.out.print("Imagine 90 minutes have passed and it is now ");
RobustTime.showCurTime(true); // display current time
if(lunchTime.isLaterToday())
System.out.println("\nLunch is later today.");
else
System.out.println("\nLunch is not until tomorrow.");
if(dinnerTime.isLaterToday())
System.out.println("Dinner is later today.");
else
System.out.println("Dinner is not until tomorrow.");
System.exit(0);
}
// Input current time from user, looping if its format is incorrect.
static void inputCurTime()
{
int colonIdx, hour, min;
String entry;
while(true) // loop until break statement after CurTime is properly set.
{
entry = JOptionPane.showInputDialog( // Beware that this won't work on hills unless it's an applet
"What time is it? (Please use 24-hour hh:mm format)");
try
{
colonIdx = entry.indexOf(':');
hour = Integer.parseInt(entry.substring(0, colonIdx));
min = Integer.parseInt(entry.substring(colonIdx+1));
RobustTime.setCurTime(hour, min);
break;
}
catch(InvalidTimeException e)
{
System.out.println(e.getMessage());
}
catch(NullPointerException e) // if user clicked "Cancel"
{
System.exit(0);
}
catch(Exception e)
{
System.out.println("Invalid Format: Please use 24-hour hh:mm format");
}
}
}
}
/* Sample Output:
What time is it? (Please use 24-hour hh:mm format) (displayed in dialog)
tenten (entered in dialog)
Invalid Format: Please use 24-hour hh:mm format
What time is it? (Please use 24-hour hh:mm format) (displayed in dialog)
ten:ten (entered in dialog)
Invalid Format: Please use 24-hour hh:mm format
What time is it? (Please use 24-hour hh:mm format) (displayed in dialog)
100:10 (entered in dialog)
Error: 100:10 is an invalid Time: hour must be between 0 and 23,
and minute must be between 0 and 59.
What time is it? (Please use 24-hour hh:mm format) (displayed in dialog)
10:100 (entered in dialog)
Error: 10:100 is an invalid Time: hour must be between 0 and 23,
and minute must be between 0 and 59.
What time is it? (Please use 24-hour hh:mm format) (displayed in dialog)
10:10 (entered in dialog)
It's now 10:10
Breakfast is at 9:00AM
which is not until tomorrow.
Lunch is at 12:00PM
which is later today.
Dinner is at 18:30
which is later today.
Imagine 90 minutes have passed and it is now 11:40AM
Lunch is later today.
Dinner is later today.
-----------------------------------------------------
What time is it? (Please use 24-hour hh:mm format)
(Cancel button clicked in dialog)
(Program ends)
*/
syntax highlighted by Code2HTML, v. 0.9