/* Java class having as its purpose to test instances of any class that ** implements the KeyPadLock interface. Specifically, it has a method ** that, given a KeyPadLock object and a Scanner, reads one-character ** command/operation codes using the Scanner and applies the indicated ** operations upon the given KeyPadLock object. Each command code ** corresponds to a press of a key on the keypad of the lock, except ** for the QUIT command. ** ** The method produces a narration of events, describing each operation ** as it is applied and reporting the resulting state of the lock (open ** or closed) afterwards. ** ** The command codes are as follows: ** ** q : quit (i.e., terminate program) ** o : press OPEN key on the lock's keypad ** c : press CLOSE key on the lock's keypad ** 0 : enter 0 on the lock's keypad ** 1 : enter 1 on the lock's keypad ** . ** . ** 9 : enter 9 on the lock's keypad ** ** @version October 2019. ** @author R. McCloskey */ import java.util.Scanner; public class KeyPadLockTester { private static final char SPACE = ' '; private static final char QUIT = 'q'; private static final char OPEN = 'o'; private static final char CLOSE = 'c'; private static final String DIGITS = "0123456789"; private static final String ALL_COMMANDS = DIGITS + QUIT + OPEN + CLOSE; /* Reads command codes using the given Scanner and applies them to ** the given KeyPadLock object, producing a narration of events. */ public static void processCommands(KeyPadLock kpl, Scanner input) { boolean keepGoing = true; while ( keepGoing ) { System.out.print("Enter one or more commands: "); String commandStr = input.nextLine(); //System.out.println(commandStr); int i = 0; while (keepGoing && i != commandStr.length()) { char command = commandStr.charAt(i); if (command == SPACE) { i = i+1; } // ignore spaces else if (ALL_COMMANDS.indexOf(command) == -1) { System.out.print(" Command " + command + " not recognized."); i = i+1; } else { System.out.print(" Performing command " + command); if (command == QUIT) { keepGoing = false; } else { try { performCommand(kpl, command); i = i+1; } catch (Exception e) { System.out.println(); e.printStackTrace(System.out); System.out.println(); } } String state = kpl.isOpen() ? "OPEN" : "CLOSED"; System.out.print("; afterwards, lock is " + state); } System.out.println(); } } } /* Performs the specified command upon the specified KeyPadLock. ** pre: command is either OPEN, CLOSE, or a digit. */ private static void performCommand(KeyPadLock kpLock, char command) { if (command == OPEN) { kpLock.open(); } else if (command == CLOSE) { kpLock.close(); } else if (Character.isDigit(command)) { kpLock.enterDigit( charToDig(command) ); } else { // command is not recognized System.out.println("This should never be printed!"); } } /* pre: c is in the range '0'..'9' ** post: value returned is the integer value corresponding to c. ** E.g., if c is (the char) '4', value returned will be (the int) 4. */ private static int charToDig(char c) { return c - '0'; } }