Source of Driver.java


  1: import java.util.Iterator;
  2: import java.util.Scanner;
  3: import java.io.File;
  4: import java.io.FileNotFoundException;
  5: /**
  6:    A driver that demonstrates the class TelephoneDirectory.
  7:    
  8:    @author Frank M. Carrano
  9:    @author Timothy M. Henry
 10:    @version 5.0
 11: */
 12: public class Driver
 13: {
 14:    private static final Name INPUT_ERROR = new Name("error", "error");
 15:    private static final Name QUIT = new Name("quit", "quit");

 17:    public static void main(String[] args)
 18:    {
 19:       TelephoneDirectory directory = new TelephoneDirectory();    
 20:       String fileName = "data.txt"; // Or file name could be read

 22:       try
 23:       {
 24:          Scanner data = new Scanner(new File(fileName));
 25:          directory.readFile(data);
 26:       }
 27:       catch (FileNotFoundException e)
 28:       {
 29:          System.out.println("File not found: " + e.getMessage());
 30:       }
 31:       
 32:       Name nextName = getName();    // Get name for search from user
 33:       while (!nextName.equals(QUIT))
 34:       {
 35:          if (nextName.equals(INPUT_ERROR))
 36:             System.out.println("Error in entering name. Try again.");
 37:          else
 38:          {
 39:             String phoneNumber = directory.getPhoneNumber(nextName);
 40:             if (phoneNumber == null)
 41:                System.out.println(nextName + " is not in the directory.");
 42:             else
 43:                System.out.println("The phone number for " + nextName + 
 44:                                   " is " + phoneNumber);
 45:          } // end if

 47:          nextName = getName();
 48:       } // end while

 50:       System.out.println("Bye!");
 51:    } // end main

 53:    // Returns either the name read from user, INPUT_ERROR, or QUIT.
 54:    private static Name getName()
 55:    {
 56:       Name result = null;
 57:       Scanner keyboard = new Scanner(System.in);

 59:       System.out.print("Enter first name and last name, " +
 60:                        "or quit to end: ");
 61:       String line = keyboard.nextLine();

 63:       if (line.trim().toLowerCase().equals("quit"))
 64:          result = QUIT;
 65:       else
 66:       {
 67:          String firstName = null;
 68:          String lastName = null;
 69:          Scanner scan = new Scanner(line);

 71:          if (scan.hasNext())
 72:          {
 73:             firstName = scan.next();
 74:             if (scan.hasNext())
 75:                lastName = scan.next();
 76:             else
 77:                result = INPUT_ERROR;
 78:          }
 79:          else
 80:             result = INPUT_ERROR;
 81:             
 82:          if (result == null)
 83:          {
 84:             // First and last names have been read
 85:             result = new Name(firstName, lastName);
 86:          } // end if
 87:       } // end if

 89:       return result;
 90:    } // end getName
 91: } // end Driver