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
Expand Up @@ -3,5 +3,7 @@
/**
* Created by leon on 5/10/17.
*/ // Checked Exception
public final class InvalidPhoneNumberFormatException extends Exception {
public final class InvalidPhoneNumberFormatException extends Exception
{

}
3 changes: 2 additions & 1 deletion src/main/java/com/zipcodewilmington/phone/PhoneNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ private PhoneNumber() throws InvalidPhoneNumberFormatException {
}

// non-default constructor is package-protected
protected PhoneNumber(String phoneNumber) throws InvalidPhoneNumberFormatException {
protected PhoneNumber(String phoneNumber) throws InvalidPhoneNumberFormatException
{
//validate phone number with format `(###)-###-####`
if (!phoneNumber.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}")) {
throw new InvalidPhoneNumberFormatException();
Expand Down
55 changes: 45 additions & 10 deletions src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.zipcodewilmington.phone;

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

import java.util.logging.Logger;

import java.util.logging.Logger;
/**
* Created by leon on 5/1/17.
*/
public final class PhoneNumberFactory {
public final class PhoneNumberFactory
{
private static final Logger logger = Logger.getGlobal();

private PhoneNumberFactory() {
Expand All @@ -19,15 +21,28 @@ 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)
{
//Initializing an array with the size of phoneNumberCount
PhoneNumber [] phoneNumberArray = new PhoneNumber[phoneNumberCount];

for(int i =0; i<phoneNumberCount; i++)
{
phoneNumberArray[i]=createRandomPhoneNumber();
}
return phoneNumberArray;
}

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

return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
}


Expand All @@ -37,16 +52,36 @@ 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)
{
StringBuilder sb = new StringBuilder();
sb.append("("+areaCode+")" + "-" + centralOfficeCode + "-" + phoneLineCode);
String phoneNumberString = sb.toString();

try
{
return createPhoneNumber(phoneNumberString);
}

catch (InvalidPhoneNumberFormatException e)
{
logger.info(phoneNumberString + " is not a valid phone number entry");
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);
return new PhoneNumber(phoneNumberString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ public abstract class RandomNumberFactory {
private static final Random random = new Random();

/** @return a random float between the specified min and max numeric range */
public static Float createFloat(float min, float max) {
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) {
public static Integer createInteger(Integer min, Integer max)
{
return createFloat(min, max).intValue();
}
}