diff --git a/src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java b/src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java index 5d3fb58..faf169c 100644 --- a/src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java +++ b/src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java @@ -1,7 +1,8 @@ package com.zipcodewilmington.exceptions; +import java.io.IOException; /** * Created by leon on 5/10/17. */ // Checked Exception -public final class InvalidPhoneNumberFormatException extends Exception { +public final class InvalidPhoneNumberFormatException extends IOException { } diff --git a/src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java b/src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java index 36d323b..e23f25d 100644 --- a/src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java +++ b/src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java @@ -1,8 +1,10 @@ package com.zipcodewilmington.phone; - +import com.zipcodewilmington.tools.RandomNumberFactory; import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException; import java.util.logging.Logger; +import java.util.logging.Level; +import java.util.Random; /** * Created by leon on 5/1/17. @@ -19,15 +21,23 @@ private PhoneNumberFactory() { * @param phoneNumberCount - number of PhoneNumber objects to instantiate * @return array of randomly generated PhoneNumber objects */ //TODO - Implement logic - public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) { - return null; + public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) throws InvalidPhoneNumberFormatException { + PhoneNumber[] numbers = new PhoneNumber[phoneNumberCount]; + for (int i = 0; i < phoneNumberCount; i++) { + numbers[i] = createRandomPhoneNumber(); + } + return numbers; } /** * @return an instance of PhoneNumber with randomly generated phone number value */ //TODO - Implement logic - public static PhoneNumber createRandomPhoneNumber() { - return createPhoneNumberSafely(-1, -1, -1); + public static PhoneNumber createRandomPhoneNumber() throws InvalidPhoneNumberFormatException { + Random randomNum = new Random(); + int areaCode = 100 + randomNum.nextInt(900); + int centOfficeCode = 100 + randomNum.nextInt(900); + int phoneLineCode = 1000 + randomNum.nextInt(9000); + return createPhoneNumberSafely(areaCode, centOfficeCode, phoneLineCode); } @@ -37,16 +47,28 @@ public static PhoneNumber createRandomPhoneNumber() { * @param phoneLineCode - 4 digit code * @return a new phone number object */ //TODO - if input is valid, return respective PhoneNumber object, else return null + public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) { - return createPhoneNumber(null); - } + String phoneNumberString = "(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode; + try {// if valid return phoneNumber object + return createPhoneNumber(phoneNumberString); + } catch (InvalidPhoneNumberFormatException i ){ + logger.log(Level.WARNING, phoneNumberString + " is not a valid phone number. "); + return null; + } + } + + /** + * @param phoneNumberString - some String corresponding to a phone number whose format is `(###)-###-####` + * @return a new phone number object + * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format + */ // TODO - Add throws statement to method signature + + public static PhoneNumber createPhoneNumber (String phoneNumberString) throws InvalidPhoneNumberFormatException { + logger.log(Level.INFO, "Attempting to create a new PhoneNumber object with a value of " + phoneNumberString); + + return new PhoneNumber(phoneNumberString); + } - /** - * @param phoneNumberString - some String corresponding to a phone number whose format is `(###)-###-####` - * @return a new phone number object - * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format - */ // TODO - Add throws statement to method signature - public static PhoneNumber createPhoneNumber(String phoneNumberString) { - return null; - } } + diff --git a/src/main/java/com/zipcodewilmington/tools/RandomNumberFactory.java b/src/main/java/com/zipcodewilmington/tools/RandomNumberFactory.java index 269dcfc..ab8489e 100644 --- a/src/main/java/com/zipcodewilmington/tools/RandomNumberFactory.java +++ b/src/main/java/com/zipcodewilmington/tools/RandomNumberFactory.java @@ -14,6 +14,10 @@ public static Float createFloat(float min, float max) { return random.nextFloat() * (max - min) + min; } + + + + /** @return a random integer between the specified min and max numeric range */ public static Integer createInteger(Integer min, Integer max) { return createFloat(min, max).intValue(); diff --git a/src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java b/src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java index a607c50..670a396 100644 --- a/src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java +++ b/src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java @@ -3,6 +3,7 @@ import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException; import com.zipcodewilmington.phone.PhoneNumber; import com.zipcodewilmington.phone.PhoneNumberFactory; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -82,7 +83,12 @@ public void testCreateRandomPhoneNumber() { for (int i = 0; i < 999; i++) { // : Given // : When - PhoneNumber phoneNumber = PhoneNumberFactory.createRandomPhoneNumber(); + PhoneNumber phoneNumber = null; + try { + phoneNumber = PhoneNumberFactory.createRandomPhoneNumber(); + } catch (InvalidPhoneNumberFormatException e) { + e.printStackTrace(); + } // : Then Assert.assertTrue(phoneNumber != null);