// Note: An improved version of this program is CounterTester.java. /* CounterTester_F18.java ** Java application program that interacts with the user (at the keyboard) for ** the purpose of testing the various classes in the Counter class hierarchy. */ import java.util.Scanner; import java.io.IOException; public class CounterTester_F18 { private static final char QUIT = 'q'; private static final char INCREMENT = 'i'; private static final char DECREMENT = 'd'; private static final char RESET = 'r'; private static final char SET_TO = 's'; private static final char HELP = 'h'; private static Scanner keyBoard = new Scanner(System.in); public static void main(String[] args) throws IOException { Counter c = null; String responseStr; int response; int counterKind, init = 0, min = 0, max = 0; println("Welcome to the Counter Tester program!"); println(""); println("Enter the kind of Counter to test:"); println(" (1) (plain) Counter"); println(" (2) Resetable Counter"); println(" (3) RollOver Counter"); println(" (4) Stopping Counter"); println(" (5) Warning Counter"); counterKind = keyBoard.nextInt(); if (3 <= counterKind && counterKind <= 5) { print("Enter minimum bound: "); min = keyBoard.nextInt(); print("Enter maximum bound: "); max = keyBoard.nextInt(); } print("Enter initial count value: "); init = keyBoard.nextInt(); if (counterKind == 1) { c = new Counter(init); } else if (counterKind == 2) { c = new ResetableCounter(init); } else if (counterKind == 3) { c = new RollOverCounter(init, min, max); } else if (counterKind == 4) { println("A line of code in CounterTester_F18 program must be\n" + "un-commented to allow testing of a StoppingCounter."); //c = new StoppingCounter(init, min, max); } else if (counterKind == 5) { println("A line of code in CounterTester_F18 program must be\n" + "un-commented to allow testing of a WarningCounter."); //c = new WarningCounter(init, min, max); } else { println("Illegal response"); } testLoop(c); } /* Repeatedly applies operations to the given Counter object, ** according to the commands entered by the user. */ private static void testLoop(Counter c) // throws IOException { char command = ' '; println("Initial counter value = " + c.countVal()); printHelp(); while (command != QUIT) { try { print(">"); command = keyBoard.next().charAt(0); test(c, command); } catch (Exception e) { e.printStackTrace(System.out); System.out.println(); } } } /* Applies to the given Counter object the method indicated by ** the given command code. */ private static void test(Counter cntr, char command) { command = Character.toLowerCase(command); if (command == QUIT) { // do nothing } else if (command == INCREMENT) { print("Incrementing to "); cntr.increment(); println( "" + cntr.countVal()); } else if (command == DECREMENT) { print("Decrementing to "); cntr.decrement(); println( "" + cntr.countVal()); } else if (command == RESET) { if (cntr instanceof ResetableCounter) { print("Resetting to "); ((ResetableCounter) cntr).reset(); println( "" + cntr.countVal()); } else { println("This Counter does not support the reset operation!"); } } else if (command == SET_TO) { print("Setting to "); int k = keyBoard.nextInt(); cntr.setTo(k); println( "" + cntr.countVal()); } else if (command == HELP) { printHelp(); } else { println("Unrecognized command; try again:"); printHelp(); } } /* Prints instructions for the user. */ private static void printHelp() { println(" Type i to increment, d to decrement, r to reset,\n" + " s to setTo, q to quit, and h for this help"); } /* Surrogate for the System.out.println() method. */ private static void println(String s) { System.out.println(s); } /* Surrogate for the System.out.print() method. */ private static void print(String s) { System.out.print(s); } }