import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application for the purpose of testing the behavior of ** instances of the ReversibleCounter class. ** Author: R. McCloskey, Sept. 2025 */ public class RevCounterTester { // User commands private static final char QUIT = 'Q'; private static final char HELP = 'H'; private static final char INCREMENT = 'I'; private static final char DECREMENT = 'D'; private static final char TOGGLE = 'T'; private static Scanner input; private static boolean echo; private static boolean keepGoing; private static ReversibleCounter revCounter; public static void main(String[] args) throws FileNotFoundException { System.out.println("Welcome to the RevCounterTester 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 will // be read from the standard input (i.e., the keyboard). establishInputSource(args); // Make a new instance of ReversibleCounter revCounter = makeNewRevCounter(); System.out.printf("After initialization, %s\n\n", revCounter); printHelp(); keepGoing = true; while (keepGoing) { try { performCommand(); System.out.println(revCounter); } catch (Exception e) { e.printStackTrace(System.out); } } System.out.println("Goodbye."); } private static void performCommand() { // prompt for and get input String response = getInput("\n> ").trim().toUpperCase(); char command = response.charAt(0); if (command == QUIT) { keepGoing = false; } else if (command == HELP) { printHelp(); } else if (command == INCREMENT) { revCounter.increment(); } else if (command == DECREMENT) { revCounter.decrement(); } else if (command == TOGGLE) { revCounter.toggleMode(); } else { System.out.println("Unknown command; ignoring;"); } } private static void printHelp() { System.out.println("Available commands:"); System.out.println("-------------------"); System.out.println(QUIT + " (Quit)"); System.out.println(HELP + " (Help)"); System.out.println(INCREMENT + " (Increment)"); System.out.println(DECREMENT + " (Decrement)"); System.out.println(TOGGLE + " (Toggle the mode)"); } /* Prompts for an initial count value to be entered and then returns ** a new ReversibleCounter object with that initial value. */ private static ReversibleCounter makeNewRevCounter() { ReversibleCounter result = null; String response = getInput("Enter initial count value: ").trim(); try { if (response.length() == 0) { result = new ReversibleCounter(); } else { int val = Integer.parseInt(response); result = new ReversibleCounter(val); } } catch (Exception e) { e.printStackTrace(System.out); System.out.println("\nInvalid input: Program aborting."); System.exit(0); } return result; } /* Prints a prompt and returns a line of input read from the input Scanner. */ 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) { if (args.length != 0) { try { input = new Scanner(new File(args[0])); echo = true; } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("File named " + args[0] + " not found"); System.exit(0); } } else { input = new Scanner(System.in); echo = false; } } }