Question: In this version, you are to reimplement the functionality of version 1, but this time using the classes you coded in Lab 3. The basic
In this version, you are to reimplement the functionality of version 1, but this time using the classes you coded in Lab 3. The basic output is identical to that of version 1, but now:
1. The three parallel arrays are gone replaced with a single array of type PhonebookEntry.
2. You should be reading in the entries using the read method of your PhonebookEntry class (which in turn uses the read methods of the Name and PhoneNumber classes).
3. Use the equals methods of the Name and PhoneNumber classes in your lookup and reverseLookup methods.
4. Use the toString methods to print out information.
5. Make 100 the capacity of your Phonebook array
6. Throw an exception (of class Exception) if the capacity of the Phonebook array is exceeded.
7. Place a try/catch around your entire main and catch both FileNotFoundExceptions and Exceptions (remember, the order of appearance of the exception types in the catch blocks can make a difference).
The name of your application class should be Phonebook. Also, you should submit ALL your classes (i.e., Name, Strip off the public from all your class definitions
Sample Run #1
For example, if the file phonebook.text contains:
Arnow David (123)456-7890 Harrow Keith (234)567-8901 Jones Jackie (345)678-9012 Augenstein Moshe (456)789-0123 Sokol Dina (567)890-1234 Tenenbaum Aaron (678)901-2345 Weiss Gerald (789)012-3456 Cox Jim (890)123-4567 Langsam Yedidyah (901)234-5678 Thurm Joseph (012)345-6789
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
lookup, reverse-lookup, quit (l/r/q)? l last name? Arnow first name? David David Arnow's phone number is (123)456-7890 lookup, reverse-lookup, quit (l/r/q)? r phone number (nnn-nnn-nnnn)? (456)789-0123 (456)789-0123 belongs to Moshe Augenstein lookup, reverse-lookup, quit (l/r/q)? l last name? Weiss first name? Jerrold -- Name not found lookup, reverse-lookup, quit (l/r/q)? l last name? Weiss first name? Gerald Gerald Weiss's phone number is (789)012-3456 lookup, reverse-lookup, quit (l/r/q)? r phone number (nnn-nnn-nnnn)? (111)123-4567 -- Phone number not found lookup, reverse-lookup, quit (l/r/q)? q 3 lookups performed 2 reverse lookups performed
Sample Run #2
If the file phonebook.text contains:
more than 100 names
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
*** Exception *** Phonebook capacity exceeded - increase the size of the underlying array
Sample Run #3
If the file phonebook.text is missing:
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
*** IOException *** phonebook.text (No such file or directory)
Instructor Notes:
[notes toc="Phonebook 02: Classes" nonum="yes"]
PREVIOUS CODE
import java.io.*; import java.util.Scanner;
public class Phonebook { public static void main(String[] args) { int lookupCount = 0; int revLookupCount = 0; int phoneEntryCount = 0;
Scanner inputReader = new Scanner(System.in);
try { File file = new File("phonebook.text"); BufferedReader phonebookReader; PhonebookEntry phonebookEntry[] = new PhonebookEntry[100]; phonebookReader = new BufferedReader(new FileReader(file));
String line = phonebookReader.readLine();
while (line != null) { if (phoneEntryCount >= 100) { throw new Exception("Phonebook capacity exceeded - increase size of underlying array"); } phonebookEntry[phoneEntryCount++] = new PhonebookEntry(line.replaceAll("( +)"," ").trim()); line = phonebookReader.readLine(); }
mainLoop: while (true) { System.out.print("lookup, reverse-lookup, quit (l/r/q)? "); String option = inputReader.nextLine(); if (option.equalsIgnoreCase("l")) { lookupCount++; System.out.print("last name? "); String lName = inputReader.nextLine(); System.out.print("first name? "); String fName = inputReader.nextLine();
Name name = new Name(lName, fName); for (int i = 0; i < phoneEntryCount; i++) { PhonebookEntry inputPhoneBook = phonebookEntry[i]; if (inputPhoneBook.lookup(name) != null) { System.out.println(inputPhoneBook.name+"'s phone number is "+inputPhoneBook.phoneNumber+" "); continue mainLoop; } } System.out.println("-- Name not found "); } else if (option.equalsIgnoreCase("r")) { revLookupCount++; System.out.print("phone number (nnn-nnn-nnnn)? "); String phone = inputReader.nextLine();
PhoneNumber phoneNumber; try { phoneNumber = new PhoneNumber(phone); }catch (Exception ex){ System.out.println("-- Phone number not found "); continue; }
for (int i = 0; i < phoneEntryCount; i++) { PhonebookEntry inputPhoneBook = phonebookEntry[i]; if (inputPhoneBook.reverseLookup(phoneNumber) != null) { System.out.println(phoneNumber+" belongs to "+inputPhoneBook.name+ " "); continue mainLoop; } } System.out.println(" -- Phone number not found"); } else if (option.equalsIgnoreCase("q")) { System.out.print(lookupCount + " lookups performed. "); System.out.print(revLookupCount + " reverse lookups performed. "); break; } } } catch (FileNotFoundException e) { System.out.println("*** IOException *** phonebook.text (No such file or directory)"); } catch (Exception ex) { System.out.println("*** Exception *** " + ex); ex.printStackTrace(); } } }
class Name { private String lastName; private String firstName;
public Name(String lastName, String firstName) { this.lastName = lastName; this.firstName = firstName; }
public String toString(){ return firstName + " " + lastName; }
@Override public boolean equals(Object obj) { Name otherName = (Name) obj; return otherName.lastName.equalsIgnoreCase(lastName) && otherName.firstName.equalsIgnoreCase(firstName); } }
class PhoneNumber { private int countryCode; private int areaCode; private int subscriberNumber;
PhoneNumber(String phoneNumber) { String temp = phoneNumber.replace(")","-"); temp = temp.replace("(","");
String[] arr1 = temp.split("-"); countryCode = Integer.parseInt(arr1[0]); areaCode = Integer.parseInt(arr1[1]); subscriberNumber = Integer.parseInt(arr1[2]); }
@Override public String toString() { return String.format("(%03d)%03d-%04d",countryCode,areaCode,subscriberNumber); }
@Override public boolean equals(Object obj) { PhoneNumber phoneNumber = (PhoneNumber) obj; return countryCode == phoneNumber.countryCode && areaCode == phoneNumber.areaCode && subscriberNumber == phoneNumber.subscriberNumber; } }
class PhonebookEntry { Name name; PhoneNumber phoneNumber;
PhonebookEntry(String entry){ String[] entrySplit = entry.split(" "); name = new Name(entrySplit[0],entrySplit[1]); phoneNumber = new PhoneNumber(entrySplit[2]); }
String lookup(Name name1){ if(name.equals(name1)) return phoneNumber.toString(); return null; }
String reverseLookup(PhoneNumber phoneNumber1){ if(phoneNumber1.equals(phoneNumber)) return name.toString(); return null; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
