/* MMR_Tester.java ** Java application program that interacts with the user (at the keyboard) for ** the purpose of testing the MinMaxRecallCounter class, which is a child ** of Counter. */ import java.util.Scanner; public class MMR_Tester { private static final char QUIT = 'q'; private static final char INCREMENT = 'i'; private static final char DECREMENT = 'd'; private static final char SET_TO = 's'; private static final char SET_TO_MIN = 'n'; private static final char SET_TO_MAX = 'x'; private static final char HELP = 'h'; private static final String COMMANDS = "" + QUIT + INCREMENT + DECREMENT + SET_TO + SET_TO_MIN + SET_TO_MAX + HELP; private static Scanner keyBoard = new Scanner(System.in); public static void main(String[] args) { // Create a MinMaxRecallCounter object with initial value zero. MinMaxRecallCounter c = new MinMaxRecallCounter(0); char command = ' '; println("Initial counter value = " + c.countVal()); printHelp(); // Repeatedly apply operations to the counter in accord with // the commands entered by the user. while (command != QUIT) { try { print("\n>"); command = keyBoard.next().charAt(0); test(c, command); } catch (Exception e) { e.printStackTrace(System.out); System.out.println(); } } } /* Applies to the given MinMaxRecallCounter object the method ** indicated by the given command code. */ private static void test(MinMaxRecallCounter cntr, char command) { command = Character.toLowerCase(command); if (COMMANDS.indexOf(command) == -1) { println("Unrecognized command; try again:"); printHelp(); } else if (command == QUIT) { // do nothing } else if (command == HELP) { printHelp(); } else { // a valid command to modify the counter has been entered if (command == INCREMENT) { println("After incrementing:"); cntr.increment(); } else if (command == DECREMENT) { println("After decrementing:"); cntr.decrement(); } else if (command == SET_TO) { println("After setting to:"); int k = keyBoard.nextInt(); cntr.setTo(k); } else if (command == SET_TO_MIN) { println("After setting to historical min:"); cntr.setToMin(); } else if (command == SET_TO_MAX) { println("After setting to historical max:"); cntr.setToMax(); } System.out.printf(" Current value: %d\n", cntr.countVal()); System.out.printf(" Historical min: %d\n", cntr.minReached()); System.out.printf(" Historical max: %d\n", cntr.maxReached()); } } /* Prints instructions for the user. */ private static void printHelp() { System.out.printf(" Type %c to quit, ", QUIT); System.out.printf("%c to increment, ", INCREMENT); System.out.printf("%c to decrement,\n", DECREMENT); System.out.printf("%c to setTo, ", SET_TO); System.out.printf("%c to setToMin, ", SET_TO_MIN); System.out.printf("%c to setToMax,\n", SET_TO_MAX); System.out.printf("%c for this help\n", 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); } }