|
@@ -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
|
|
|
6
|
+import java.util.logging.Level;
|
5
|
7
|
import java.util.logging.Logger;
|
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() {
|
|
@@ -20,14 +22,19 @@ public final class PhoneNumberFactory {
|
20
|
22
|
* @return array of randomly generated PhoneNumber objects
|
21
|
23
|
*/ //TODO - Implement logic
|
22
|
24
|
public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
|
23
|
|
- return null;
|
|
25
|
+ PhoneNumber[] phoneBook = new PhoneNumber[phoneNumberCount];
|
|
26
|
+ return phoneBook;
|
24
|
27
|
}
|
25
|
28
|
|
26
|
29
|
/**
|
27
|
30
|
* @return an instance of PhoneNumber with randomly generated phone number value
|
28
|
31
|
*/ //TODO - Implement logic
|
29
|
|
- public static PhoneNumber createRandomPhoneNumber() {
|
30
|
|
- return createPhoneNumberSafely(-1, -1, -1);
|
|
32
|
+ public static PhoneNumber createRandomPhoneNumber () throws InvalidPhoneNumberFormatException {
|
|
33
|
+ int areaCode = createInteger(100,999);
|
|
34
|
+ int centralOfficeCode = createInteger(100, 999);
|
|
35
|
+ int phoneLineCode = createInteger(1000, 9999);
|
|
36
|
+
|
|
37
|
+ return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
|
31
|
38
|
}
|
32
|
39
|
|
33
|
40
|
|
|
@@ -37,8 +44,15 @@ public final class PhoneNumberFactory {
|
37
|
44
|
* @param phoneLineCode - 4 digit code
|
38
|
45
|
* @return a new phone number object
|
39
|
46
|
*/ //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);
|
|
47
|
+ public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) throws InvalidPhoneNumberFormatException {
|
|
48
|
+ try {
|
|
49
|
+ return createPhoneNumber("(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode);
|
|
50
|
+ }
|
|
51
|
+ catch (InvalidPhoneNumberFormatException e)
|
|
52
|
+ { logger.log(Level.WARNING,
|
|
53
|
+ areaCode + centralOfficeCode + phoneLineCode + " is not a valid phone number");
|
|
54
|
+ return null;
|
|
55
|
+ }
|
42
|
56
|
}
|
43
|
57
|
|
44
|
58
|
/**
|
|
@@ -46,7 +60,10 @@ 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.log(Level.INFO,
|
|
65
|
+ "Attempting to create a new PhoneNumber object with a value of " + phoneNumberString);
|
|
66
|
+ PhoneNumber newNumber = new PhoneNumber(phoneNumberString);
|
|
67
|
+ return newNumber;
|
51
|
68
|
}
|
52
|
69
|
}
|