import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application for purpose of testing the behavior of objects from the ** TimeOfDay class. ** ** Author: R. McCloskey, August 2025 */ public class TimeOfDayTester { private static Scanner input; private static boolean echo; private static boolean keepGoing; private static TimeOfDay time; private static TimeOfDay oneTime = new TimeOfDay(8, 23, true); private static TimeOfDay anotherTime = new TimeOfDay(5, 17, false); public static void main(String[] args) throws FileNotFoundException { System.out.println("Welcome to the TimeOfDayTester program.\n"); // Establish the source of input to which the Scanner is attached. // If a command line argument is provided, it is taken to be the // name of a file from which to read input. Otherwise input is // read from the keyboard. establishInputSource(args); // Establish the initial "current" time of day time = makeTimeOfDay(); System.out.printf("After initialization, time is %s\n\n", time); printHelp(); keepGoing = true; while (keepGoing) { try { performCommand(); } catch (Exception e) { e.printStackTrace(System.out); } } System.out.println("Goodbye."); } private static void performCommand() { String spacesPattern = " +"; System.out.print("\n> "); // prompt for input String commandLine = input.nextLine().toUpperCase(); // read line of input if (echo) { System.out.println(commandLine); } Scanner comLineScanner = new Scanner(commandLine); if (!comLineScanner.hasNext()) { } // empty command else { String command = comLineScanner.next(); if (command.equals("Q")) { keepGoing = false; } else if (command.equals("H")) { printHelp(); } else if (command.equals("FM")) { testGoForwardByMinute(); } else if (command.equals("BM")) { testGoBackwardsByMinute(); } else if (command.equals("FH")) { testGoForwardByHour(); } else if (command.equals("BH")) { testGoBackwardsByHour(); } else if (command.equals("E")) { testIsEarlierThan(oneTime); testIsEarlierThan(anotherTime); } else if (command.equals("24")) { testToString24Format(); } else { System.out.println("Unknown command; ignoring."); } } } private static void printHelp() { System.out.println("Available commands:"); System.out.println("-------------------"); System.out.println(" Q (Quit)"); System.out.println(" H (Help)"); System.out.println(" FM (test goForwardByMinute())"); System.out.println(" FH (test goForwardByHour())"); System.out.println(" BM (test goBackwardsByMinute())"); System.out.println(" BH (test goBackwardsByHour())"); System.out.println(" E (test isEarlierThan())"); System.out.println(" 24 (test toString24Format())"); } /* Compares 'time' with the specified time of day and reports the result. */ private static void testIsEarlierThan(TimeOfDay otherTime) { boolean result = time.isEarlierThan(otherTime); String emptyOrNOT = result ? "" : " NOT"; System.out.printf("isEarlierThan() says that %s is%s earlier than %s", time, emptyOrNOT, otherTime); System.out.println(); } private static void testGoForwardByMinute() { time.goForwardByMinute(); System.out.printf("After calling goForwardByMinute(), the time is %s\n", time); } private static void testGoBackwardsByMinute() { time.goBackwardsByMinute(); System.out.printf("After calling goBackwardsByMinute(), the time is %s\n", time); } private static void testGoForwardByHour() { time.goForwardByHour(); System.out.printf("After calling goForwardByHour(), the time is %s\n", time); } private static void testGoBackwardsByHour() { time.goBackwardsByHour(); System.out.printf("After calling goBackwardsByHour(), the time is %s\n", time); } private static void testToString24Format() { System.out.printf("The 24-hour format for %s is %s\n", time, time.toString24Format()); } /* Returns a new TimeOfDay object set to the time of day specified by ** the three inputs obtained via the 'input' Scanner. */ private static TimeOfDay makeTimeOfDay() { TimeOfDay result = null; String timeStr = getInput("Enter time of day (e.g., 5:46AM, 12:07PM): ").trim(); try { int posOfColon = timeStr.indexOf(':'); int h = Integer.parseInt(timeStr.substring(0,posOfColon)); int m = Integer.parseInt(timeStr.substring(posOfColon+1,posOfColon+3)); String amOrPm = timeStr.substring(posOfColon+3).toUpperCase(); if (amOrPm.equals("AM")) { result = new TimeOfDay(h,m,true); } else if (amOrPm.equals("PM")) { result = new TimeOfDay(h,m,false); } else { result = new TimeOfDay(); System.out.println("Invalid string; initializing to noon"); } } catch (Exception e) { e.printStackTrace(System.out); System.out.println("\nInvalid input: Program aborting."); System.exit(0); } return result; } private static String getInput(String prompt) { System.out.print(prompt); String result = input.nextLine(); if (echo) { System.out.println(result); } return result; } /* Establishes the source of input from which the Scanner ('input') ** will read. If args[] is not empty, the file whose name is args[0] ** will be the input source. Otherwise it will be the keyboard. */ 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; } } }