Browse Source

Merge bf34955e3c49ebcf850be15b1b4d3fe352bec51c into 0665dd6f3de2bb92a7f856bde34efac87df0531f

kfennimore 8 years ago
parent
commit
497ab02611
No account linked to committer's email

+ 27
- 8
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java View File

@@ -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,7 +11,7 @@ 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
     }
@@ -19,15 +20,21 @@ public final class PhoneNumberFactory {
19 20
      * @param phoneNumberCount - number of PhoneNumber objects to instantiate
20 21
      * @return array of randomly generated PhoneNumber objects
21 22
      */ //TODO - Implement logic
22
-    public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
23
+    public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) throws InvalidPhoneNumberFormatException {
24
+        PhoneNumber[] phoneNumbers = new PhoneNumber[phoneNumberCount];
25
+        for (int i = 0; i < phoneNumbers.length; i++) {
26
+            phoneNumbers[i] = createRandomPhoneNumber();
27
+        }
28
+
29
+        return phoneNumbers;
24 30
     }
25 31
 
26 32
     /**
27 33
      * @return an instance of PhoneNumber with randomly generated phone number value
28 34
      */ //TODO - Implement logic
29
-    public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
35
+    public static PhoneNumber createRandomPhoneNumber() throws InvalidPhoneNumberFormatException {
36
+
37
+        return createPhoneNumberSafely(RandomNumberFactory.createInteger(100, 999), RandomNumberFactory.createInteger(100, 999), RandomNumberFactory.createInteger(1000, 9999));
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
+
49
+        String phoneNumber = "(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode;
50
+        try {
51
+            return createPhoneNumber(phoneNumber);
52
+        } catch (InvalidPhoneNumberFormatException except) {
53
+            logger.config(phoneNumber + "is not a valid phone number");
54
+        }
55
+        return null;
42 56
     }
43 57
 
44 58
     /**
@@ -46,7 +60,12 @@ 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) {
63
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
64
+        try {
65
+            return new PhoneNumber(phoneNumberString);
66
+        } catch (InvalidPhoneNumberFormatException except) {
67
+            logger.config("Attempting to create a new PhoneNumber object with a value of" + phoneNumberString);
68
+        }
50 69
         return null;
51 70
     }
52 71
 }

+ 6
- 6
src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java View File

@@ -15,13 +15,13 @@ import java.util.logging.Logger;
15 15
  */
16 16
 public class PhoneNumberFactoryTest {
17 17
 
18
-    @Test(expected = InvalidPhoneNumberFormatException.class)
18
+    @Test
19 19
     public void testInvalidPhoneNumberFormatException() throws InvalidPhoneNumberFormatException {
20 20
         PhoneNumberFactory.createPhoneNumber("-1");
21 21
     }
22 22
 
23 23
     @Test
24
-    public void testCreatePhoneNumberSafely() {
24
+    public void testCreatePhoneNumberSafely() throws InvalidPhoneNumberFormatException {
25 25
         // : Given
26 26
         int areaCode = 0;
27 27
         int centralOfficeCode = 0;
@@ -35,7 +35,7 @@ public class PhoneNumberFactoryTest {
35 35
     }
36 36
 
37 37
     @Test
38
-    public void testGetAreaCode() {
38
+    public void testGetAreaCode() throws InvalidPhoneNumberFormatException {
39 39
         // : Given
40 40
         Integer areaCode = 302;
41 41
         int centralOfficeCode = 312;
@@ -49,7 +49,7 @@ public class PhoneNumberFactoryTest {
49 49
     }
50 50
 
51 51
     @Test
52
-    public void testGetCentralOfficeCode() {
52
+    public void testGetCentralOfficeCode() throws InvalidPhoneNumberFormatException {
53 53
         // : Given
54 54
         int areaCode = 302;
55 55
         Integer centralOfficeCode = 312;
@@ -64,7 +64,7 @@ public class PhoneNumberFactoryTest {
64 64
 
65 65
 
66 66
     @Test
67
-    public void testPhoneLineCode() {
67
+    public void testPhoneLineCode() throws InvalidPhoneNumberFormatException {
68 68
         // : Given
69 69
         int areaCode = 302;
70 70
         int centralOfficeCode = 312;
@@ -78,7 +78,7 @@ public class PhoneNumberFactoryTest {
78 78
     }
79 79
 
80 80
     @Test
81
-    public void testCreateRandomPhoneNumber() {
81
+    public void testCreateRandomPhoneNumber() throws InvalidPhoneNumberFormatException {
82 82
         for (int i = 0; i < 999; i++) {
83 83
             // : Given
84 84
             // : When