Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.zipcodewilmington.phone;

import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
import com.zipcodewilmington.tools.RandomNumberFactory;

import java.util.logging.Logger;

/**
* Created by leon on 5/1/17.
*/
public final class PhoneNumberFactory {
private static final Logger logger = Logger.getGlobal();
private static final Logger LOGGER = Logger.getLogger("com.zipcodewilmington.phone");

private PhoneNumberFactory() {
/** This constructor is private
Expand All @@ -19,15 +20,29 @@ 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 [] arrayOfPhoneNumbers = new PhoneNumber[phoneNumberCount];

for (int i = 0; i < phoneNumberCount; i++){
int areadCode = RandomNumberFactory.createInteger(100,999);
int centralOfficeCode = RandomNumberFactory.createInteger(100,999);
int phoneCOde = RandomNumberFactory.createInteger(1000,9999);

arrayOfPhoneNumbers[i] = createPhoneNumberSafely(areadCode,centralOfficeCode,phoneCOde);
}

return arrayOfPhoneNumbers;
}

/**
* @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 {
int areaCode = RandomNumberFactory.createInteger(100,999);
int centralOfficeCode = RandomNumberFactory.createInteger(100,999);
int phoneCode = RandomNumberFactory.createInteger(1000,9999);

return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneCode);
}


Expand All @@ -37,16 +52,29 @@ 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);
public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) throws InvalidPhoneNumberFormatException {
String newPhoneNumber = "";

newPhoneNumber = "(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode;

try {
return createPhoneNumber(newPhoneNumber);
} catch (InvalidPhoneNumberFormatException e){
LOGGER.info(newPhoneNumber + " 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) {
return null;
public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
LOGGER.info("Attempting to create a new PhoneNumber object with a value of " + phoneNumberString);
PhoneNumber newPhoneNumber = new PhoneNumber(phoneNumberString);
return newPhoneNumber;

}
}
25 changes: 20 additions & 5 deletions src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -21,7 +22,7 @@ public void testInvalidPhoneNumberFormatException() throws InvalidPhoneNumberFor
}

@Test
public void testCreatePhoneNumberSafely() {
public void testCreatePhoneNumberSafely() throws InvalidPhoneNumberFormatException {
// : Given
int areaCode = 0;
int centralOfficeCode = 0;
Expand All @@ -35,7 +36,7 @@ public void testCreatePhoneNumberSafely() {
}

@Test
public void testGetAreaCode() {
public void testGetAreaCode() throws InvalidPhoneNumberFormatException {
// : Given
Integer areaCode = 302;
int centralOfficeCode = 312;
Expand All @@ -49,7 +50,7 @@ public void testGetAreaCode() {
}

@Test
public void testGetCentralOfficeCode() {
public void testGetCentralOfficeCode() throws InvalidPhoneNumberFormatException {
// : Given
int areaCode = 302;
Integer centralOfficeCode = 312;
Expand All @@ -64,7 +65,7 @@ public void testGetCentralOfficeCode() {


@Test
public void testPhoneLineCode() {
public void testPhoneLineCode() throws InvalidPhoneNumberFormatException {
// : Given
int areaCode = 302;
int centralOfficeCode = 312;
Expand All @@ -78,7 +79,7 @@ public void testPhoneLineCode() {
}

@Test
public void testCreateRandomPhoneNumber() {
public void testCreateRandomPhoneNumber() throws InvalidPhoneNumberFormatException {
for (int i = 0; i < 999; i++) {
// : Given
// : When
Expand All @@ -88,4 +89,18 @@ public void testCreateRandomPhoneNumber() {
Assert.assertTrue(phoneNumber != null);
}
}

@Test
public void testCreateRandomPhoneNumberArray() throws InvalidPhoneNumberFormatException {
for (int i = 0; i < 10; i++) {
// : Given
// : When
PhoneNumber [] phoneNumber = PhoneNumberFactory.createRandomPhoneNumberArray(5);

// : Then
Assert.assertTrue(phoneNumber != null);
String output = Arrays.toString(phoneNumber);
System.out.println(output);
}
}
}