import java.io.*; import java.util.*; class PhoneBook { /** * Add a new person to the phone book. * The names are required to be added in alphabetical order. * * @throws InvalidArgumentException if the name comes in the * wrong order. */ public void add(Person person) { // TODO } /** * How many people are in the phone book? */ public int size() { // TODO return 0; } /** * Look up a person in the phone book. * * @param name The person's name. * @return The person, or null if the person is not in the phone * book. */ public Person find(String name) { // TODO return null; } } class Person { /** The person's name. */ final public String name; /** The person's phone number. */ final public String phone; public Person(String name, String phone) { this.name = name; this.phone = phone; } public String toString() { return "(" + name + "," + phone + ")"; } } public class Lab1 { public static void main(String[] args) { if (args.length != 1) { System.err.println("Please give the phone book file as a parameter"); System.exit(1); } try { PhoneBook phoneBook = new PhoneBook(); BufferedReader names = new BufferedReader(new FileReader(args[0])); String line; System.out.println("Reading phone book..."); while((line = names.readLine()) != null) { int index = line.indexOf("/"); String name = line.substring(0, index); String number = line.substring(index+1); phoneBook.add(new Person(name, number)); } names.close(); System.out.println("Finished reading phone book."); System.out.println(phoneBook.size() + " entries."); System.out.println("Type in a name, or 'quit' to quit."); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while(true) { System.out.print("> "); System.out.flush(); line = in.readLine(); if (line == null || line.equals("quit")) break; Person person = phoneBook.find(line); if (person == null) System.out.println("Person not found."); else System.out.println("Phone number: " + person.phone); } } catch (IOException e) { System.err.println(e); } } }