/** Class whose purpose is simply to exercise/test the KeyPadLock class. * @version October 2001, updated September 2003, September 2008 * @author R. McCloskey * Description: A Java program that exercises the class KeyPadLock. * The program creates an instance of KeyPadLock, using the first * command line argument as the code by which to open the lock. * The user may then enter "commands" for the lock at the computer * keyboard. Commands to the lock are as follows: * * q : quit (i.e., terminate program) * o : (attempt to) open lock * c : close lock * 0 : enter 0 on the lock's keypad * 1 : enter 1 on the lock's keypad * . * . * 9 : enter 9 on the lock's keypad * * For each command entered by the user (and (s)he may enter any number * of commands on a single line in response to the program's prompt), the * program responds by reporting the current state of the lock (i.e., * open or closed). * * If an invalid command is entered, the program is not responsible * for its behavior! * * In jGRASP, to feed command line arguments to an application, click on * the "Build" menu, then click "Run Arguments" to turn it on (check mark * means on). * Then fill in the text box (labeled "Run Arguments") with the desired * arguments. Here that would be a sequence of one or more digits (with * no intervening spaces), which serves to provide the "secret" code for * the keypad lock on which the program will operate. */ import java.io.*; public class KeyPadLockDriver { /** @pre The first command line argument (i.e., args[0]) contains a * sequence of digits. */ public static void main(String[] args) throws IOException { String commandLine = args[0]; int[] code = new int[commandLine.length()]; for (int i=0; i != code.length; i = i+1) { code[i] = charToDig( commandLine.charAt(i) ); } KeyPadLock kpl = new KeyPadLock( code ); System.out.println("A keypad lock with code " + args[0] + " has been created."); processUserCommands(kpl); System.out.println("****** Program terminating normally *****"); } private static void processUserCommands(KeyPadLock kpl) throws IOException { BufferedReader keyBoard = new BufferedReader( new InputStreamReader(System.in) ); boolean keepGoing = true; String commandStr = new String(""); char command; while ( keepGoing ) { System.out.print("Enter one or more commands:"); commandStr = keyBoard.readLine(); for (int i=0; i != commandStr.length(); i = i+1) { command = commandStr.charAt(i); System.out.println("Performing command " + command); if (command == 'q') { keepGoing = false; } else { performCommand(kpl, command); System.out.print(" Afterwards, lock is "); if (kpl.isOpen()) { System.out.print("OPEN"); } else { System.out.print("CLOSED"); } } System.out.println(); } } } private static void performCommand(KeyPadLock k, char command) { if (command == 'o') { k.open(); } else if (command == 'c') { k.close(); } else if (command == ' ') { } else /* command assumed to be a digit */ { k.enterDigit( charToDig(command) ); } } /* 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 (int) c - (int) '0'; } }