Lewis Dominguez 6 jaren geleden
bovenliggende
commit
e0b92df4ba

+ 4
- 1
src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java Bestand weergeven

@@ -1,7 +1,10 @@
1 1
 package com.zipcodewilmington.exceptions;
2 2
 
3
+
4
+import java.io.IOException;
5
+
3 6
 /**
4 7
  * Created by leon on 5/10/17.
5 8
  */ // Checked Exception
6
-public final class InvalidPhoneNumberFormatException extends Exception {
9
+public final class InvalidPhoneNumberFormatException extends IOException {
7 10
 }

+ 12
- 8
src/main/java/com/zipcodewilmington/phone/PhoneNumber.java Bestand weergeven

@@ -16,26 +16,30 @@ public final class PhoneNumber {
16 16
     // non-default constructor is package-protected
17 17
     protected PhoneNumber(String phoneNumber) throws InvalidPhoneNumberFormatException {
18 18
         //validate phone number with format `(###)-###-####`
19
-        if (!phoneNumber.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}")) {
20
-            throw new InvalidPhoneNumberFormatException();
21
-        }
22
-        this.phoneNumberString = phoneNumber;
19
+
20
+            if (!phoneNumber.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}")) {
21
+                throw new InvalidPhoneNumberFormatException();
22
+            }
23
+            this.phoneNumberString = phoneNumber;
24
+
25
+
26
+
23 27
     }
24 28
 
25
-    public String getAreaCode() {
29
+    public String getAreaCode() throws InvalidPhoneNumberFormatException{
26 30
         return toString().substring(1, 4);
27 31
     }
28 32
 
29
-    public String getCentralOfficeCode() {
33
+    public String getCentralOfficeCode() throws InvalidPhoneNumberFormatException  {
30 34
         return toString().substring(6, 9);
31 35
     }
32 36
 
33
-    public String getPhoneLineCode() {
37
+    public String getPhoneLineCode() throws InvalidPhoneNumberFormatException{
34 38
         return toString().substring(10, 14);
35 39
 }
36 40
 
37 41
     @Override
38
-    public String toString() {
42
+    public String toString(){
39 43
         return phoneNumberString;
40 44
     }
41 45
 }

+ 36
- 9
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Bestand weergeven

@@ -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
 }

+ 5
- 5
src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java Bestand weergeven

@@ -21,7 +21,7 @@ public class PhoneNumberFactoryTest {
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