/* 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 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"); } test(c); } private static void test(Counter c) throws IOException { String response = ""; println("Initial counter value = " + c.countVal()); printHelp(); while (!response.equals("q")) { print(">"); response = keyBoard.next(); if (response.equals("i")) { print("Incrementing to "); c.increment(); println( "" + c.countVal()); } else if (response.equals("d")) { print("Decrementing to "); c.decrement(); println( "" + c.countVal()); } else if (response.equals("r")) { if (c instanceof ResetableCounter) { print("Resetting to "); ((ResetableCounter) c).reset(); println( "" + c.countVal()); } else { println("This Counter does not support the reset operation!"); } } else if (response.equals("s")) { print("Setting to "); int k = keyBoard.nextInt(); c.setTo(k); println( "" + c.countVal()); } else if (response.equals("q")) { // do nothing } else if (response.equals("h")) { printHelp(); } else { println("Illegal input; try again:"); printHelp(); } } // end while } /* 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); } }