import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application whose purpose is to test instances of the ** TimeOfDay and TimeOfDay1224 classes. */ public class TimeOfDayTester { private static final String YES = "YES"; private static final String NO = "NO"; private static Scanner input; private static boolean echo; // Should input data be echoed? private static boolean keepGoing; public static void main(String[] args) throws FileNotFoundException { // Establish income source. establishInputSource(args); // Depending upon which kind of object you want to test, comment // out one of the two lines below. TimeOfDay time = new TimeOfDay(0,0); //TimeOfDay time = new TimeOfDay1224(0,0); println("Welcome to the TimeOfDay tester program."); keepGoing = true; while (keepGoing) { try { doCommand(time); println(""); } catch (Exception e) { e.printStackTrace(); } } println("Goodbye from the TimeOfDay tester program."); } public static void doCommand(TimeOfDay time) { String spacesPattern = " +"; System.out.print("> "); // prompt for input String commandLine = input.nextLine(); if (echo) { println(commandLine); } String[] commandTokens = commandLine.split(spacesPattern); if (commandTokens.length == 0) { } // empty command else if (commandTokens.length == 1) { String comm0 = commandTokens[0]; if ("q".equals(comm0)) { keepGoing = false; } else if ("p".equals(comm0)) { reportTime(time); } else if ("h".equals(comm0)) { help(); } else if ("s24".equals(commandTokens[0])) { if (time instanceof TimeOfDay1224) { ((TimeOfDay1224)time).setTo24Mode(); reportTime(time); } else { println("Cannot set mode of a TimeOfDay object"); } } else if ("s12".equals(comm0)) { if (time instanceof TimeOfDay1224) { ((TimeOfDay1224)time).setTo12Mode(); reportTime(time); } else { println("Cannot set mode of a TimeOfDay object"); } } else { reportUnknown(); } } else if (commandTokens.length == 2) { String comm0 = commandTokens[0]; int num = Integer.parseInt(commandTokens[1]); if ("cm".equals(comm0)) { time.changeByMinutes(num); reportTime(time); } else if ("ch".equals(comm0)) { time.changeByHours(num); reportTime(time); } else { reportUnknown(); } } else if (commandTokens.length == 3) { String comm0 = commandTokens[0]; int num1 = Integer.parseInt(commandTokens[1]); int num2 = Integer.parseInt(commandTokens[2]); if ("s".equals(comm0)) { time.setTo(num1, num2); reportTime(time); } else if ("sa".equals(comm0)) { if (time instanceof TimeOfDay1224) { ((TimeOfDay1224)time).setTo(num1, num2, false); reportTime(time); } else { println("Not a legal command for a TimeOfDay object"); } } else if ("sp".equals(comm0)) { if (time instanceof TimeOfDay1224) { ((TimeOfDay1224)time).setTo(num1, num2, true); reportTime(time); } else { println("Not a legal command for a TimeOfDay object"); } } else if ("e".equals(comm0)) { TimeOfDay t = new TimeOfDay(num1, num2); print("Is " + time + " earlier than " + t + "? "); String answer = time.isEarlierThan(t) ? YES : NO; println(answer); } else { reportUnknown(); } } else if (commandTokens.length == 5) { String comm0 = commandTokens[0]; int num1 = Integer.parseInt(commandTokens[1]); int num2 = Integer.parseInt(commandTokens[2]); int num3 = Integer.parseInt(commandTokens[3]); int num4 = Integer.parseInt(commandTokens[4]); if ("b".equals(comm0)) { TimeOfDay t1 = new TimeOfDay(num1, num2); TimeOfDay t2 = new TimeOfDay(num3, num4); print("Is " + time + " between " + t1 + " and " + t2 + "? "); String answer = time.isBetween(t1,t2) ? YES : NO; println(answer); } else { reportUnknown(); } } else { reportUnknown(); } } private static void reportTime(TimeOfDay time) { println("Time is " + time); } /* Displays an example of each type of command that can be issued. */ private static void help() { println("q (quit)"); println("p (print time)"); println("h (display list of example commands)"); println("s 17 9 (set time to (h:17, m:9))"); println("sa 3 47 (set time to 3:47AM)"); println("sp 3 47 (set time to 3:47PM)"); println("s24 (set time to 24-hour mode)"); println("s12 (set time to 12-hour mode)"); println("cm 531 (change time by +531 minutes)"); println("cm -98 (change time by -98 minutes)"); println("ch 57 (change time by +57 hours)"); println("ch -98 (change time by -98 hours)"); println("e 9 27 (is time earlier than (h:9, m:27)?)"); println("b 9 27 14 45 (is time between (h:9, m:27) and (h:14, m:45)?)"); } private static void reportUnknown() { println("Unknown command"); } /* Sets the values of 'input' and 'echo' according to whether or not ** 'args' has length zero. If it does, input is to be read from the ** keyboard and input need not be echoed. Otherwise, input is to be ** read from the file named by args[0] and needs to be echoed. */ private static void establishInputSource(String[] args) throws FileNotFoundException { if (args.length != 0) { input = new Scanner(new File(args[0])); echo = true; } else { input = new Scanner(System.in); echo = false; } } /* Surrogate for System.out.println(). */ private static void println(String s) { System.out.println(s); } /* Surrogate for System.out.print(). */ private static void print(String s) { System.out.print(s); } }