|
@@ -1,13 +1,17 @@
|
1
|
1
|
package com.zipcodewilmington.phone;
|
2
|
2
|
|
|
3
|
+
|
3
|
4
|
import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
|
|
5
|
+import com.zipcodewilmington.tools.RandomNumberFactory;
|
|
6
|
+
|
4
|
7
|
|
|
8
|
+import java.util.logging.Level;
|
5
|
9
|
import java.util.logging.Logger;
|
6
|
10
|
|
7
|
11
|
/**
|
8
|
12
|
* Created by leon on 5/1/17.
|
9
|
13
|
*/
|
10
|
|
-public final class PhoneNumberFactory {
|
|
14
|
+public final class PhoneNumberFactory extends RandomNumberFactory{
|
11
|
15
|
private static final Logger logger = Logger.getGlobal();
|
12
|
16
|
|
13
|
17
|
private PhoneNumberFactory() {
|
|
@@ -19,15 +23,24 @@ public final class PhoneNumberFactory {
|
19
|
23
|
* @param phoneNumberCount - number of PhoneNumber objects to instantiate
|
20
|
24
|
* @return array of randomly generated PhoneNumber objects
|
21
|
25
|
*/ //TODO - Implement logic
|
22
|
|
- public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
|
23
|
|
- return null;
|
|
26
|
+ public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) throws InvalidPhoneNumberFormatException {
|
|
27
|
+
|
|
28
|
+ PhoneNumber[] phn = new PhoneNumber[phoneNumberCount];
|
|
29
|
+ for(int i = 0; i < phoneNumberCount; i++) {
|
|
30
|
+ phn[i] = createRandomPhoneNumber();
|
|
31
|
+ }
|
|
32
|
+ return phn;
|
24
|
33
|
}
|
25
|
34
|
|
26
|
35
|
/**
|
27
|
36
|
* @return an instance of PhoneNumber with randomly generated phone number value
|
28
|
37
|
*/ //TODO - Implement logic
|
29
|
|
- public static PhoneNumber createRandomPhoneNumber() {
|
30
|
|
- return createPhoneNumberSafely(-1, -1, -1);
|
|
38
|
+ public static PhoneNumber createRandomPhoneNumber() throws InvalidPhoneNumberFormatException {
|
|
39
|
+ int areaCode = RandomNumberFactory.createInteger(100,999);
|
|
40
|
+ int centralOfficeCode = RandomNumberFactory.createInteger(100,999);
|
|
41
|
+ int phoneLineCode = RandomNumberFactory.createInteger(1000,9999);
|
|
42
|
+ return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
|
|
43
|
+
|
31
|
44
|
}
|
32
|
45
|
|
33
|
46
|
|
|
@@ -37,8 +50,18 @@ public final class PhoneNumberFactory {
|
37
|
50
|
* @param phoneLineCode - 4 digit code
|
38
|
51
|
* @return a new phone number object
|
39
|
52
|
*/ //TODO - if input is valid, return respective PhoneNumber object, else return null
|
40
|
|
- public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
|
41
|
|
- return createPhoneNumber(null);
|
|
53
|
+ public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) throws InvalidPhoneNumberFormatException{
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+ try{
|
|
57
|
+ String num = "(" + areaCode + ")" + centralOfficeCode + "-" + phoneLineCode;
|
|
58
|
+ return createPhoneNumber(num);
|
|
59
|
+
|
|
60
|
+ } catch (InvalidPhoneNumberFormatException e) {
|
|
61
|
+ logger.log(Level.INFO, "(" + areaCode + ")" + centralOfficeCode + "-" + phoneLineCode + " is not a valid phone number");
|
|
62
|
+
|
|
63
|
+ return null;
|
|
64
|
+ }
|
42
|
65
|
}
|
43
|
66
|
|
44
|
67
|
/**
|
|
@@ -46,7 +69,11 @@ public final class PhoneNumberFactory {
|
46
|
69
|
* @return a new phone number object
|
47
|
70
|
* @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
|
48
|
71
|
*/ // TODO - Add throws statement to method signature
|
49
|
|
- public static PhoneNumber createPhoneNumber(String phoneNumberString) {
|
50
|
|
- return null;
|
|
72
|
+ public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
|
|
73
|
+
|
|
74
|
+ logger.log(Level.INFO,"Attempting to create a new PhoneNumber object with a value of " + phoneNumberString);
|
|
75
|
+ return new PhoneNumber(phoneNumberString);
|
|
76
|
+
|
|
77
|
+
|
51
|
78
|
}
|
52
|
79
|
}
|