import java.io.*; class DriversLicenseMenu { private BufferedReader keyboard; private static final int QUIT_CHOICE = 4; public DriversLicenseMenu() { keyboard = new BufferedReader (new InputStreamReader (System.in)); } public void display() { System.out.println (".......Menu........"); System.out.println ("1. Create a license"); System.out.println ("2. Print the license"); System.out.println ("3. Check equality of two licenses"); System.out.println ("4. Exit"); } public int getChoice() throws IOException, NumberFormatException, MenuException { String choiceString = ""; System.out.println(""); System.out.print("Enter your choice: "); try { choiceString = keyboard.readLine(); int choice = Integer.parseInt (choiceString); if ( (choice < 1) || (choice > QUIT_CHOICE) ) throw new MenuException ("Invalid selection. Was " + choice + " but should be between 1 and 6."); return choice; } catch (IOException ioe) { throw new IOException ("Couldn't get selection. Input Stream dead."); } catch (NumberFormatException nfe) { throw new NumberFormatException ("Selection should be an int, but was " + choiceString); } } public DriversLicense createDriversLicense() throws IOException { try { DriversLicense dl = null; System.out.print ("Full name: "); String fullName = keyboard.readLine(); System.out.print ("Birthdate: "); String birthDate = keyboard.readLine(); System.out.print ("State: "); String state = keyboard.readLine(); System.out.print ("Number: "); String number = keyboard.readLine(); System.out.print ("Parts: "); String donorString = keyboard.readLine(); boolean donor = false; if (donorString.equals ("yes")) donor = true; dl = new DriversLicense (state, number, fullName, birthDate, donor); return dl; } catch (IOException ioe) { throw new IOException ("Couldn't read from keyboard, so culdn't create the license."); } } public static void main (String[] args) { // Drivers license #1 DriversLicense license1 = null; // Create the menu DriversLicenseMenu menu = new DriversLicenseMenu(); int choice = -1; while (choice != QUIT_CHOICE) { try { // Display the menu menu.display(); // Read in the user's choice choice = menu.getChoice(); // Take the action switch (choice) { case 1: // Create a new DriversLicense license1 = menu.createDriversLicense(); break; case 2: // Display a new DriversLicense System.out.println (license1); break; case 3: // Check equality of two licenses DriversLicense license2 = menu.createDriversLicense(); DriversLicense license3 = menu.createDriversLicense(); if (license2.equals(license3)) System.out.println ("They are equal."); else System.out.println ("They are NOT equal."); break; } // Pick up here after the break; } catch (IOException ioe) { System.out.println (ioe); choice = QUIT_CHOICE; } catch (NumberFormatException ioe) { System.out.println (ioe); } catch (MenuException ioe) { System.out.println (ioe); } } // end of while } }