-
Purpose - to demonstrate basic exception handling and logging.
-
Objective - to implement a
PhoneNumberFactoryclass that generatesPhoneNumberobjects. -
The
PhoneNumberclass is a container for aStringrepresentation 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
PhoneNumberobject, it is possible to receive aInvalidPhoneNumberFormatExceptionif theStringpassed into thePhoneNumberconstructor does not fit the format(###)-###-####. InvalidPhoneNumberFormatExceptionextendsIOException, which is achecked exception.- Modify the
createPhoneNumbermethod so that it throws any resultingInvalidPhoneNumberFormatException.- This will ensure that any method calling
createPhoneNumberwill have to handle the exception.
- This will ensure that any method calling
- Using the
createPhoneNumbermethod fromPart 1, define thecreatePhoneNumberSafelymethod such that the input parameters,areaCode,centralOfficeCode,phoneLineCodeare concatenated to create aStringrepresentation of the respective phone number. - Use this
Stringobject to construct a new instance ofPhoneNumberand return it. - If the concatentation of the input parameters yields a
Stringwhose value does not match the format(###)-###-####, then ourPhoneNumberwill throw aInvalidPhoneNumberFormatException. - If a
InvalidPhoneNumberFormatExceptionis 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 thecreatePhoneNumberSafelymethod fromPart 2and return the resultingPhoneNumberobject.
- Using the
createRandomPhoneNumberfromPart 3, generate an array ofPhoneNumberobjects, whose length reflects the input argument.- For example
createRandomPhoneNumber(5)should return an array of 5PhoneNumberobjects.
- For example
-
Add logging to the
createPhoneNumbermethod 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
createPhoneNumberSafelymethod 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...