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
5 changes: 1 addition & 4 deletions src/main/java/com/zipcodewilmington/phone/PhoneNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ public final class PhoneNumber {
private final String phoneNumberString;

// default constructor is uncallable
private PhoneNumber() throws InvalidPhoneNumberFormatException {
this(null);
}
private PhoneNumber() throws InvalidPhoneNumberFormatException { this(null); }

// non-default constructor is package-protected
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
23 changes: 18 additions & 5 deletions src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;

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

/**
Expand All @@ -20,14 +22,18 @@ private PhoneNumberFactory() {
* @return array of randomly generated PhoneNumber objects
*/ //TODO - Implement logic
public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
return null;
return new PhoneNumber[phoneNumberCount];
}

/**
* @return an instance of PhoneNumber with randomly generated phone number value
*/ //TODO - Implement logic
public static PhoneNumber createRandomPhoneNumber() {
return createPhoneNumberSafely(-1, -1, -1);
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 @@ -38,15 +44,22 @@ 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 createdPhoneNumber = "(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode;
try {
return createPhoneNumber(createdPhoneNumber);
} catch (InvalidPhoneNumberFormatException e){
logger.log(Level.WARNING, createdPhoneNumber + " is not a valid input. Try again.");
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.log(Level.INFO, "Created phone number is " + phoneNumberString);
return new PhoneNumber(phoneNumberString);
}
}