Question: This is in the language Ruby. you will be implementing a PhoneBook class. It is up to you to decide how to store the data
This is in the language Ruby.
you will be implementing a PhoneBook class. It is up to you to decide how to store the data ie how to define a phonebook instance's fields so that you can correctly implement all the required methods.
Even though the data structure you will implement is up to you, keep the following in mind:
The Phonebook will hold names and phone numbers, as well as a note on whether each name and phone number pair is listed or not.
By listed, we mean that they are marked as so not simply stored You might want to use a boolean to determine it so
The Phonebook might contain listed and unlisted entries. How you should add them will be specified in the add method below.
initialize
Description: This is the constructor. You should initialize the fields that you think your class needs for successful evaluation of the remaining methods. We do not test the constructor directly, but we do test it indirectly by testing the remaining methods.
addname number, islisted
Description: This method attempts to add a new entry to the PhoneBook. name is the name of the person and number is that person's phone number. The islisted parameter identifies if this entry should be listed or unlisted in the PhoneBook true if listed, false if unlisted Return true if the operation was successful, and false otherwise. Here are the requirements for the add method:
If the person already exists, then the entry cannot be added to the PhoneBook.
If number is not in the format NNNNNNNNNN the entry cannot be added to the PhoneBook.
A number can be added as unlisted any number of times, but can only be added as listed once. This means that if number already exists and is listed in the PhoneBook, the new entry can only be added if the entry will be unlisted.
Type: String String, Bool Bool
Assumptions: No phone number will start with
Examples:
@phonebook PhoneBook.new
@phonebook.addJohn false true
@phonebook.addJane false true
@phonebook.addJohn false false
@phonebook.addRavi true false
Here is another example:
@phonebook PhoneBook.new
@phonebookaddAlice false true
@phonebookaddBob false true
@phonebookaddEve true true
@phonebookaddRob true false
@phonebookaddJohnny B Good", false true
lookupname
Description: Looks up name in the PhoneBook and returns the corresponding phone number in the format NNNNNNNNNN ONLY if the entry is listed. Otherwise, return nil.
Type: String String or nil
Examples:
@phonebook PhoneBook.new
@phonebook.addJohn false true
@phonebook.addJane true true
@phonebook.addJack false true
@phonebook.addJessie true true
@phonebook.addRavi true false
@phonebook.lookupJohn nil
@phonebook.lookupJack nil
@phonebook.lookupJane
@phonebook.lookupJessie
@phonebook.lookupRavi nil
lookupByNumnum
Description: Returns the name associated with a given number only if the entry is listed. Otherwise, return nil.
Type: String String or nil
Examples:
@phonebook PhoneBook.new
@phonebook.addJohn false true
@phonebook.addJane true true
@phonebook.addJack false true
@phonebook.addJessie true true
@phonebook.lookupByNum nil
@phonebook.lookupByNum nil
@phonebook.lookupByNum "Jane"
@phonebook.lookupByNum "Jessie"
namesByAcareacode
Description: Returns an array of all names in the PhoneBook who have phone numbers beginning with areacode, including unlisted names.
Type: String Array
Examples:
@phonebook PhoneBook.new
@phonebook.addJohn false true
@phonebook.addJane true true
@phonebook.addJack false true
@phonebook.addJessie true true
# Note that Jessie's number here is a little different than in the other examples!
@phonebook.namesByAcJohn "Jessie"
@phonebook.namesByAcJack
@phonebook.namesByAc
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
