import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application for purpose of testing the methods of the CalendarDate ** class. ** ** Author: R. McCloskey, November 2022 */ public class CalendarDateTester { private static Scanner input; private static boolean echo; private static boolean keepGoing; private static CalendarDate current, prev; public static void main(String[] args) throws FileNotFoundException { System.out.println("Welcome to the CalendarDateTester 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" calendar date current = makeDate(); // Establish the "previous" calendar date to be the default date. prev = new CalendarDate(); 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(":> "); // 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")) { testEquals(); } else if (command.equals("C")) { testCompareTo(); } else if (command.equals("S")) { testSetDate(comLineScanner); } else if (command.equals("RD")) { testRetreatDay(comLineScanner); } else if (command.equals("AD")) { testAdvanceDay(comLineScanner); } else if (command.equals("RW")) { testRetreatWeek(comLineScanner); } else if (command.equals("AW")) { testAdvanceWeek(comLineScanner); } else if (command.equals("RM")) { testRetreatMonth(comLineScanner); } else if (command.equals("AM")) { testAdvanceMonth(comLineScanner); } else if (command.equals("RY")) { testRetreatYear(comLineScanner); } else if (command.equals("AY")) { testAdvanceYear(comLineScanner); } else { System.out.println("Unknown command; ignoring."); } } } private static void printHelp() { System.out.println("Available commands ([n] means optional natural number):"); System.out.println("-------------------------------------------------------"); System.out.println(" Q (Quit)"); System.out.println(" H (Help)"); System.out.println(" E (test Equals())"); System.out.println(" C (test compareTo())"); System.out.println(" S date (test setDate(date))"); System.out.println(" RD [n] (test retreatDay())"); System.out.println(" AD [n] (test advanceDay())"); System.out.println(" RW [n] (test retreatWeek())"); System.out.println(" AW [n] (test advanceWeek())"); System.out.println(" RM [n] (test retreatMonth())"); System.out.println(" AM [n] (test advanceMonth())"); System.out.println(" RY [n] (test retreatYear())"); System.out.println(" AY [n] (test advanceYear())"); } /* Compares 'current' and 'prev' using the equals() method, and reports the ** result. */ private static void testEquals() { System.out.printf("equals() says that %s and %s are ", current, prev); if (!current.equals(prev)) { System.out.print("NOT "); } System.out.println("equal."); } /* 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("comes before %s", prev); } else if (compareResult > 0) { System.out.printf("comes after %s", prev); } else { System.out.printf("is equal to %s", prev); } System.out.println(); } /* Calls the setDate() method and reports the result. */ private static void testSetDate(Scanner comLineScanner) { prev.setDate(current); String arg = "";; if (comLineScanner.hasNext()) { arg = comLineScanner.next(); current.setDate(arg); } System.out.printf("After calling setDate(%s), current is %s and prev is %s\n", arg, current, prev); } private static void testRetreatDay(Scanner comLineScanner) { prev.setDate(current); String arg; if (comLineScanner.hasNextInt()) { int days = comLineScanner.nextInt(); current.retreatDay(days); arg = days + ""; } else { current.retreatDay(); arg = ""; } System.out.printf("After calling retreatDay(%s), current is %s and prev is %s\n", arg, current, prev); } private static void testAdvanceDay(Scanner comLineScanner) { prev.setDate(current); String arg; if (comLineScanner.hasNextInt()) { int days = comLineScanner.nextInt(); current.advanceDay(days); arg = days + ""; } else { current.advanceDay(); arg = ""; } System.out.printf("After calling advanceDay(%s), current is %s and prev is %s\n", arg, current, prev); } private static void testRetreatWeek(Scanner comLineScanner) { prev.setDate(current); String arg; if (comLineScanner.hasNextInt()) { int weeks = comLineScanner.nextInt(); current.retreatWeek(weeks); arg = weeks + ""; } else { current.retreatWeek(); arg = ""; } System.out.printf("After calling retreatWeek(%s), current is %s and prev is %s\n", arg, current, prev); } private static void testAdvanceWeek(Scanner comLineScanner) { prev.setDate(current); String arg; if (comLineScanner.hasNextInt()) { int weeks = comLineScanner.nextInt(); current.advanceWeek(weeks); arg = weeks + ""; } else { current.advanceWeek(); arg = ""; } System.out.printf("After calling advanceWeek(%s), current is %s and prev is %s\n", arg, current, prev); } private static void testRetreatMonth(Scanner comLineScanner) { prev.setDate(current); String arg; if (comLineScanner.hasNextInt()) { int months = comLineScanner.nextInt(); current.retreatMonth(months); arg = months + ""; } else { current.retreatMonth(); arg = ""; } System.out.printf("After calling retreatMonth(%s), current is %s and prev is %s\n", arg, current, prev); } private static void testAdvanceMonth(Scanner comLineScanner) { prev.setDate(current); String arg; if (comLineScanner.hasNextInt()) { int months = comLineScanner.nextInt(); current.advanceMonth(months); arg = months + ""; } else { current.advanceMonth(); arg = ""; } System.out.printf("After calling advanceMonth(%s), current is %s and prev is %s\n", arg, current, prev); } private static void testRetreatYear(Scanner comLineScanner) { prev.setDate(current); String arg; if (comLineScanner.hasNextInt()) { int years = comLineScanner.nextInt(); current.retreatYear(years); arg = years + ""; } else { current.retreatYear(); arg = ""; } System.out.printf("After calling retreatYear(%s), current is %s and prev is %s\n", arg, current, prev); } private static void testAdvanceYear(Scanner comLineScanner) { prev.setDate(current); String arg; if (comLineScanner.hasNextInt()) { int years = comLineScanner.nextInt(); current.advanceYear(years); arg = years + ""; } else { current.advanceYear(); arg = ""; } System.out.printf("After calling advanceYear(%s), current is %s and prev is %s\n", arg, current, prev); } /* Returns a new CalendarDate object set to the date specified by ** an input string. */ private static CalendarDate makeDate() { CalendarDate result = null; String dateStr = getInput("Enter date in mm/dd/yyyy format:>"); try { result = new CalendarDate(dateStr); } catch (Exception e) { e.printStackTrace(System.out); System.out.println("\nProgram 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; } } }