import java.util.Scanner; /** * Program 2 driver program. * * @author John Paxton * @version October 2, 2008 */ public class Driver { public static void main (String [] args) { Scanner in = new Scanner(System.in); // read input from terminal window CaesarCipher code; // an instance of a cipher String text; // user input to convert String result; // result of conversion int key; // key for Caesar cipher int menuSelection; // user's menu selection System.out.println("CS 160, Program 2: Caesar Cipher"); System.out.println("--------------------------------\n"); System.out.print("Enter the cipher key [0..25]: "); key = in.nextInt(); code = new CaesarCipher(key); displayMenu(); menuSelection = getMenuChoice(in); while (menuSelection != QUIT) { if (menuSelection == ENCODE) { System.out.print("Enter a sentence to encode: "); text = in.nextLine(); result = code.encode(text); System.out.println("Plain: " + text); System.out.println("Cipher: " + result); } else if (menuSelection == DECODE) { System.out.print("Enter numbers to decode: "); text = in.nextLine(); result = code.decode(text); System.out.println("Cipher: " + text); System.out.println("Plain: " + result); } else { System.out.println("Invalid option, please try again."); } displayMenu(); menuSelection = getMenuChoice(in); } System.out.println("\nGoodbye."); } private static void displayMenu() { System.out.println(); System.out.println("1. Encode a message."); System.out.println("2. Decode a message."); System.out.println("3. Quit."); } private static int getMenuChoice(Scanner in) { int menuChoice; // user's menu selection System.out.print("Please enter your menu choice: "); menuChoice = in.nextInt(); in.nextLine(); // clear the user input line return menuChoice; } public static final int ENCODE = 1; // user wants to encode text public static final int DECODE = 2; // user wants to decode numbers public static final int QUIT = 3; // user wants to quit }