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. In this version, you will be adding some inheritance, as well as introducing some collection classes ArrayList and Map. The basic operation output is similar to the previous versions, with the following changes

2. Multiple phone numbers are allowed for a phone book entry

3. Each phone numbers is accompanied by a description (e.g. 'mobile', 'home', etc.)

4. The phonebook array is replaced by a map (which supplies the lookup functionality directly).

5. Reverse lookup goes away

6. A command line argument is used to determine the phonebook file to be used

The name of your application class should be PhonebookApp. Also, you should submit ALL your classes (i.e., Name, Strip off public from all your class definintions

Sample Run #1

For example if the file phonebook.text contains:

Arnow David 1 mobile (123)456-7890 Harrow Keith 2 home (234)567-8901 work (243)123-6574 Jones Jackie 3 cell (345)678-9012 work (324)564-0987 fax (987)234-9823 

here is a sample execution of the program:

lookup, quit (l/q)? l last name? Jones first name? Jackie Jackie Jones's phone numbers: [cell: (345)678-9012, work: (324)564-0987, fax: (987)234-9823] lookup, quit (l/q)? l last name? Doe first name? John -- Name not found lookup, quit (l/q)? q  

Sample Run #2

If the application is run without a command line argument, the output should be:

Usage: PhonebookApp 'phonebook-filename'

PREVIOUS CODE for previous lab:

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!