|
|
@@ -3,6 +3,7 @@ package com.zipcodewilmington.phone;
|
|
3
|
3
|
import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
|
|
4
|
4
|
|
|
5
|
5
|
import java.util.logging.Logger;
|
|
|
6
|
+import java.util.Random;
|
|
6
|
7
|
|
|
7
|
8
|
/**
|
|
8
|
9
|
* Created by leon on 5/1/17.
|
|
|
@@ -20,17 +21,23 @@ public final class PhoneNumberFactory {
|
|
20
|
21
|
* @return array of randomly generated PhoneNumber objects
|
|
21
|
22
|
*/ //TODO - Implement logic
|
|
22
|
23
|
public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
|
|
23
|
|
- return null;
|
|
|
24
|
+ PhoneNumber[] randomPhoneNumberArray = new PhoneNumber[phoneNumberCount];
|
|
|
25
|
+ for (int i = 0; i < phoneNumberCount; i++) {
|
|
|
26
|
+ randomPhoneNumberArray[i] = createRandomPhoneNumber();
|
|
|
27
|
+ }
|
|
|
28
|
+ return randomPhoneNumberArray;
|
|
24
|
29
|
}
|
|
25
|
|
-
|
|
26
|
30
|
/**
|
|
27
|
31
|
* @return an instance of PhoneNumber with randomly generated phone number value
|
|
28
|
32
|
*/ //TODO - Implement logic
|
|
29
|
33
|
public static PhoneNumber createRandomPhoneNumber() {
|
|
30
|
|
- return createPhoneNumberSafely(-1, -1, -1);
|
|
|
34
|
+ Random rand = new Random();
|
|
|
35
|
+ int areaCode = rand.nextInt(899) + 100;
|
|
|
36
|
+ int centralOfficeCode = rand.nextInt(899) + 100;
|
|
|
37
|
+ int phoneLineCode = rand.nextInt(8999) + 1000;
|
|
|
38
|
+ return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
|
|
31
|
39
|
}
|
|
32
|
40
|
|
|
33
|
|
-
|
|
34
|
41
|
/**
|
|
35
|
42
|
* @param areaCode - 3 digit code
|
|
36
|
43
|
* @param centralOfficeCode - 3 digit code
|
|
|
@@ -38,7 +45,14 @@ public final class PhoneNumberFactory {
|
|
38
|
45
|
* @return a new phone number object
|
|
39
|
46
|
*/ //TODO - if input is valid, return respective PhoneNumber object, else return null
|
|
40
|
47
|
public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
|
|
41
|
|
- return createPhoneNumber(null);
|
|
|
48
|
+ String phoneNumberRepresentation = "(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode;
|
|
|
49
|
+ logger.info("Create phonenumber");
|
|
|
50
|
+ try {
|
|
|
51
|
+ return createPhoneNumber(phoneNumberRepresentation);
|
|
|
52
|
+ } catch (InvalidPhoneNumberFormatException e) {
|
|
|
53
|
+ logger.warning("" + phoneNumberRepresentation + " is not a valid phone number" );
|
|
|
54
|
+ return null;
|
|
|
55
|
+ }
|
|
42
|
56
|
}
|
|
43
|
57
|
|
|
44
|
58
|
/**
|
|
|
@@ -46,7 +60,8 @@ public final class PhoneNumberFactory {
|
|
46
|
60
|
* @return a new phone number object
|
|
47
|
61
|
* @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
|
|
48
|
62
|
*/ // TODO - Add throws statement to method signature
|
|
49
|
|
- public static PhoneNumber createPhoneNumber(String phoneNumberString) {
|
|
50
|
|
- return null;
|
|
|
63
|
+ public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
|
|
|
64
|
+ logger.info("Attempting to create a new phoneNumber object with a value of " + phoneNumberString);
|
|
|
65
|
+ return new PhoneNumber(phoneNumberString);
|
|
51
|
66
|
}
|
|
52
|
67
|
}
|