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