-
Purpose - to demonstrate basic exception handling and logging.
-
Objective - to implement a
PhoneNumberFactory
class that generatesPhoneNumber
objects. -
The
PhoneNumber
class is a container for aString
representation of a respective phone number. -
Note: Phone numbers are a composite of 3 affixes;
Area Code
,Central Office Code
, andPhone Line Code
.Area Code
- the first 3 numeric valuesCentral Office Code
- the 4th, 5th, and 6th numeric values.Phone Line Code
- the last 4 numeric values.
-
Below is a sample instantation of and invokation on
PhoneNumber
.
String stringRepresentation = "(302)-312-5555";
PhoneNumber phoneNumber = new PhoneNumber(stringRepresentation);
String areaCode = phoneNumber.getAreaCode();
String centralOfficeCode = phoneNumber.getCentralOfficeCode();
String phoneLineCode = phoneNumber.getPhoneLineCode();
- Upon instantiating a new
PhoneNumber
object, it is possible to receive aInvalidPhoneNumberFormatException
if theString
passed into thePhoneNumber
constructor does not fit the format(###)-###-####
. InvalidPhoneNumberFormatException
extendsIOException
, which is achecked exception
.- Modify the
createPhoneNumber
method so that it throws any resultingInvalidPhoneNumberFormatException
.- This will ensure that any method calling
createPhoneNumber
will have to handle the exception.
- This will ensure that any method calling
- Using the
createPhoneNumber
method fromPart 1
, define thecreatePhoneNumberSafely
method such that the input parameters,areaCode
,centralOfficeCode
,phoneLineCode
are concatenated to create aString
representation of the respective phone number. - Use this
String
object to construct a new instance ofPhoneNumber
and return it. - If the concatentation of the input parameters yields a
String
whose value does not match the format(###)-###-####
, then ourPhoneNumber
will throw aInvalidPhoneNumberFormatException
. - If a
InvalidPhoneNumberFormatException
is thrown within this method, catch it and returnnull
.
- Using the
RandomNumberFactory
, generate a randomArea Code
,Central Office Code
, andPhone Line Code
. Pass these values as arguments of thecreatePhoneNumberSafely
method fromPart 2
and return the resultingPhoneNumber
object.
- Using the
createRandomPhoneNumber
fromPart 3
, generate an array ofPhoneNumber
objects, whose length reflects the input argument.- For example
createRandomPhoneNumber(5)
should return an array of 5PhoneNumber
objects.
- For example
-
Add logging to the
createPhoneNumber
method fromPart 1
, which logs the message"Attempting to create a new PhoneNumber object with a value of (###)-###-####
- where
(###)-###-####
will be replaced with the respective input parameter.
-
Add logging to the
createPhoneNumberSafely
method fromPart 2
, which logs the message(###)-###-#### is not a valid phone number
- Where
(###)-###-####
will be replaced with the respective input parameter.
- Yeah this header says all that is needed...