|
@@ -1,6 +1,7 @@
|
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;
|
6
|
7
|
|
|
@@ -10,43 +11,77 @@ import java.util.logging.Logger;
|
10
|
11
|
public final class PhoneNumberFactory {
|
11
|
12
|
private static final Logger logger = Logger.getGlobal();
|
12
|
13
|
|
13
|
|
- private PhoneNumberFactory() {
|
|
14
|
+ private PhoneNumberFactory() throws InvalidPhoneNumberFormatException {
|
14
|
15
|
/** This constructor is private
|
15
|
16
|
* This class is uninstantiable */
|
16
|
17
|
}
|
|
18
|
+// String stringRepresentation = "(302)-312-5555";
|
|
19
|
+// PhoneNumber phoneNumber = new PhoneNumber(stringRepresentation);
|
|
20
|
+// String areaCode = phoneNumber.getAreaCode();
|
|
21
|
+// String centralOfficeCode = phoneNumber.getCentralOfficeCode();
|
|
22
|
+// String phoneLineCode = phoneNumber.getPhoneLineCode();
|
|
23
|
+
|
17
|
24
|
|
18
|
25
|
/**
|
19
|
26
|
* @param phoneNumberCount - number of PhoneNumber objects to instantiate
|
20
|
27
|
* @return array of randomly generated PhoneNumber objects
|
21
|
28
|
*/ //TODO - Implement logic
|
22
|
29
|
public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
|
23
|
|
- return null;
|
24
|
|
- }
|
|
30
|
+ PhoneNumber[] phoneBook = new PhoneNumber[phoneNumberCount];
|
25
|
31
|
|
26
|
|
- /**
|
27
|
|
- * @return an instance of PhoneNumber with randomly generated phone number value
|
28
|
|
- */ //TODO - Implement logic
|
29
|
|
- public static PhoneNumber createRandomPhoneNumber() {
|
30
|
|
- return createPhoneNumberSafely(-1, -1, -1);
|
31
|
|
- }
|
|
32
|
+ for (int i = 1; i <= phoneNumberCount; i++) {
|
32
|
33
|
|
|
34
|
+ phoneBook[i - 1] = createRandomPhoneNumber( );
|
33
|
35
|
|
34
|
|
- /**
|
35
|
|
- * @param areaCode - 3 digit code
|
36
|
|
- * @param centralOfficeCode - 3 digit code
|
37
|
|
- * @param phoneLineCode - 4 digit code
|
38
|
|
- * @return a new phone number object
|
39
|
|
- */ //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);
|
|
36
|
+ }
|
|
37
|
+ return phoneBook;
|
42
|
38
|
}
|
43
|
39
|
|
44
|
|
- /**
|
45
|
|
- * @param phoneNumberString - some String corresponding to a phone number whose format is `(###)-###-####`
|
46
|
|
- * @return a new phone number object
|
47
|
|
- * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
|
48
|
|
- */ // TODO - Add throws statement to method signature
|
49
|
|
- public static PhoneNumber createPhoneNumber(String phoneNumberString) {
|
50
|
|
- return null;
|
|
40
|
+
|
|
41
|
+ /**
|
|
42
|
+ * @return an instance of PhoneNumber with randomly generated phone number value
|
|
43
|
+ */ //TODO - Implement logic
|
|
44
|
+ public static PhoneNumber createRandomPhoneNumber ( ) {
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+ int aC = RandomNumberFactory.createInteger( 100, 999 );
|
|
48
|
+ int cOc = RandomNumberFactory.createInteger( 100, 999 );
|
|
49
|
+ int pLc = RandomNumberFactory.createInteger( 1000, 9999 );
|
|
50
|
+
|
|
51
|
+ return createPhoneNumberSafely( aC, cOc, pLc );
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+ /**
|
|
56
|
+ * @param areaCode - 3 digit code
|
|
57
|
+ * @param centralOfficeCode - 3 digit code
|
|
58
|
+ * @param phoneLineCode - 4 digit code
|
|
59
|
+ * @return a new phone number object
|
|
60
|
+ */ //TODO - if input is valid, return respective PhoneNumber object, else return null
|
|
61
|
+ public static PhoneNumber createPhoneNumberSafely ( int areaCode, int centralOfficeCode, int phoneLineCode){
|
|
62
|
+ String ph = "(" + areaCode + ")" + "-" + centralOfficeCode + "-" + phoneLineCode;
|
|
63
|
+
|
|
64
|
+ try {
|
|
65
|
+ return createPhoneNumber( ph );
|
|
66
|
+
|
|
67
|
+ } catch
|
|
68
|
+ (InvalidPhoneNumberFormatException e) {
|
|
69
|
+ logger.info(ph + " is not a valid phone number!");
|
|
70
|
+ return null;
|
|
71
|
+ }
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ /**
|
|
75
|
+ * @param phoneNumberString - some String corresponding to a phone number whose format is `(###)-###-####`
|
|
76
|
+ * @return a new phone number object
|
|
77
|
+ * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
|
|
78
|
+ */ // TODO - Add throws statement to method signature
|
|
79
|
+ public static PhoneNumber createPhoneNumber ( String ph) throws InvalidPhoneNumberFormatException {
|
|
80
|
+ PhoneNumber phoneNumber;
|
|
81
|
+ logger.finest("Attempting to create a new PhoneNumber object with a value of " + ph );
|
|
82
|
+ return phoneNumber = new PhoneNumber( ph );
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+
|
51
|
86
|
}
|
52
|
|
-}
|
|
87
|
+
|