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, January 2025 */ public class TimeOfDayTester { private static Scanner input; private static boolean echo; private static boolean keepGoing; private static TimeOfDay current, prev; 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 current = makeTimeOfDay(); // Establish the "previous" time of day to be midnight. prev = new TimeOfDay(0,0); System.out.printf("After initialization, current is %s and prev is %s\n\n", current, prev); 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("E")) { testIsEarlierThan(); } else if (command.equals("L")) { testIsLaterThan(); } else if (command.equals("C")) { testCompareTo(); } else if (command.equals("A1")) { testAdvanceByOneMinute(); } else if (command.equals("A")) { testAdvanceBy(comLineScanner); } else if (command.equals("D1")) { testDecreaseByOneMinute(); } else if (command.equals("D")) { testDecreaseBy(comLineScanner); } else if (command.equals("12")) { testSetTo12HourMode(); } else if (command.equals("24")) { testSetTo24HourMode(); } // else if (command.equals("S")) { testSetTo(comLineScanner); } else { System.out.println("Unknown command; ignoring."); } } } private static void printHelp() { System.out.println("Available commands (h, and r stand for natural numbers):"); System.out.println("-----------------------------------------------------------"); System.out.println(" Q (Quit)"); System.out.println(" H (Help)"); System.out.println(" C (test compareTo(prev))"); System.out.println(" E (test isEarlierThan(prev))"); System.out.println(" L (test isLaterThan(prev))"); System.out.println(" A1 (test advanceByOneMinute())"); System.out.println(" A (test advanceBy(h,r))"); System.out.println(" D1 (test decreaseByOneMinute())"); System.out.println(" D (test decreaseBy(h,r))"); System.out.println(" 12 (test setTo12HourMode())"); System.out.println(" 24 (test setTo24HourMode())"); //System.out.println(" S h r (test setTo(h,r))"); } /* Compares 'current' and 'prev' using the compareTo() method, and reports the ** result. */ private static void testCompareTo() { int compareResult = current.compareTo(prev); System.out.printf("compareTo() says that %s ", current); if (compareResult < 0) { System.out.printf("is less than %s", prev); } else if (compareResult > 0) { System.out.printf("is greater than %s", prev); } else { System.out.printf("is equal to %s", prev); } System.out.println(); } /* Compares 'current' and 'prev' using the isEarlierThan() method, and ** reports the result. */ private static void testIsEarlierThan() { boolean compareResult = current.isEarlierThan(prev); String emptyOrNOT = compareResult ? "" : " NOT"; System.out.printf("isEarlierThan() says that %s is%s earlier than %s", current, emptyOrNOT, prev); System.out.println(); } /* Compares 'current' and 'prev' using the isLaterThan() method, and ** reports the result. */ private static void testIsLaterThan() { boolean compareResult = current.isLaterThan(prev); String emptyOrNOT = compareResult ? "" : " NOT"; System.out.printf("isLaterThan() says that %s is%s later than %s", current, emptyOrNOT, prev); System.out.println(); } /* Calls the setTo() method and reports the result. */ /* Not applicable in Spring 2025 private static void testSetTo(Scanner comLineScanner) { try { // Read the hour and minute arguments int hourArg = comLineScanner.nextInt(); int minuteArg = comLineScanner.nextInt(); // Modify 'prev' to make it identical to 'current' prev.setTo(current.hourOf(), current.minuteOf()); current.setTo(hourArg, minuteArg); System.out.printf("After calling setTo(%d,%d), current is %s and prev is %s\n", hourArg, minuteArg, current, prev); } catch (Exception e) { e.printStackTrace(System.out); } } */ private static void testAdvanceByOneMinute() { prev.setTo(current); current.advanceByOneMinute(); System.out.printf("After calling advanceByOneMinute(), current is %s and prev is %s\n", current, prev); } private static void testAdvanceBy(Scanner comLineScanner) { try { int h = comLineScanner.nextInt(); int m = comLineScanner.nextInt(); if (h < 0 || m < 0) { throw new Exception(); } prev.setTo(current); current.advanceBy(h,m); System.out.printf("After calling advanceBy(%d,%d), current is %s and prev is %s\n", h, m, current, prev); } catch (Exception e) { System.out.println("Command aborted; two nonnegative integers were expected"); } } private static void testDecreaseByOneMinute() { prev.setTo(current); current.decreaseByOneMinute(); System.out.printf("After calling decreaseByOneMinute(), current is %s and prev is %s\n", current, prev); } private static void testDecreaseBy(Scanner comLineScanner) { try { int h = comLineScanner.nextInt(); int m = comLineScanner.nextInt(); if (h < 0 || m < 0) { throw new Exception(); } prev.setTo(current); current.decreaseBy(h,m); System.out.printf("After calling decreaseBy(%d,%d), current is %s and prev is %s\n", h, m, current, prev); } catch (Exception e) { System.out.println("Command aborted; two nonnegative integers were expected"); } } private static void testSetTo12HourMode() { current.setTo12HourMode(); System.out.printf("After calling setTo12HourMode(), current is %s and prev is %s\n", current, prev); } private static void testSetTo24HourMode() { current.setTo24HourMode(); System.out.printf("After calling setTo24HourMode(), current is %s and prev is %s\n", current, prev); } /* Returns a new TimeOfDay object set to the time of day specified by ** two integer inputs obtained via the 'input' Scanner. */ private static TimeOfDay makeTimeOfDay() { TimeOfDay result = null; String hourMinuteStr = getInput("Enter initial hour and minute values: "); try { Scanner scanner = new Scanner(hourMinuteStr); int h = scanner.nextInt(); int m = scanner.nextInt(); result = new TimeOfDay(h,m); } 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; } } }