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
Original file line number Diff line number Diff line change
@@ -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 {
}
52 changes: 37 additions & 15 deletions src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
}


Expand All @@ -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;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down