From 82a9f9c5588a7dc1f2c0cf54816dbf9be449f4f9 Mon Sep 17 00:00:00 2001 From: John Tokhi <56596400+tokhij@users.noreply.github.com> Date: Sun, 10 Nov 2019 17:56:34 -0500 Subject: [PATCH] completed --- .../phone/PhoneNumberFactory.java | 29 ++++++++++++++----- .../PhoneNumberFactoryTest.java | 10 +++---- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java b/src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java index 36d323b..16c8c8e 100644 --- a/src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java +++ b/src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java @@ -1,6 +1,7 @@ package com.zipcodewilmington.phone; import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException; +import com.zipcodewilmington.tools.RandomNumberFactory; import java.util.logging.Logger; @@ -19,15 +20,20 @@ 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[] newNum = new PhoneNumber[phoneNumberCount]; + for (int i = 0; i < newNum.length; i++) { + newNum[i] = createRandomPhoneNumber(); + } + return newNum; } /** * @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 { + + return createPhoneNumberSafely(RandomNumberFactory.createInteger(100,999), RandomNumberFactory.createInteger(100,999), RandomNumberFactory.createInteger(1000,9999)); } @@ -38,7 +44,14 @@ public static PhoneNumber createRandomPhoneNumber() { * @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 safeNum = "(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode; + + try { + return createPhoneNumber(safeNum); + } catch (InvalidPhoneNumberFormatException e) { + logger.info(safeNum + " is not a valid number"); + return null; + } } /** @@ -46,7 +59,9 @@ public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOffic * @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; + public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException { + PhoneNumber myNum = new PhoneNumber(phoneNumberString); + logger.info("Create a new phone number: " + phoneNumberString); + return myNum; } } diff --git a/src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java b/src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java index a607c50..1c3f22f 100644 --- a/src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java +++ b/src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java @@ -21,7 +21,7 @@ public void testInvalidPhoneNumberFormatException() throws InvalidPhoneNumberFor } @Test - public void testCreatePhoneNumberSafely() { + public void testCreatePhoneNumberSafely() throws InvalidPhoneNumberFormatException { // : Given int areaCode = 0; int centralOfficeCode = 0; @@ -35,7 +35,7 @@ public void testCreatePhoneNumberSafely() { } @Test - public void testGetAreaCode() { + public void testGetAreaCode() throws InvalidPhoneNumberFormatException { // : Given Integer areaCode = 302; int centralOfficeCode = 312; @@ -49,7 +49,7 @@ public void testGetAreaCode() { } @Test - public void testGetCentralOfficeCode() { + public void testGetCentralOfficeCode() throws InvalidPhoneNumberFormatException { // : Given int areaCode = 302; Integer centralOfficeCode = 312; @@ -64,7 +64,7 @@ public void testGetCentralOfficeCode() { @Test - public void testPhoneLineCode() { + public void testPhoneLineCode() throws InvalidPhoneNumberFormatException { // : Given int areaCode = 302; int centralOfficeCode = 312; @@ -78,7 +78,7 @@ public void testPhoneLineCode() { } @Test - public void testCreateRandomPhoneNumber() { + public void testCreateRandomPhoneNumber() throws InvalidPhoneNumberFormatException { for (int i = 0; i < 999; i++) { // : Given // : When