Quellcode durchsuchen

Merge 21ad63b114cf0798a3783f3c3adc94faf2890254 into 0665dd6f3de2bb92a7f856bde34efac87df0531f

Madeline Bowe vor 8 Jahren
Ursprung
Commit
b96caac985
Es ist kein Account mit dieser Commiter-Email verbunden

+ 5
- 0
src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java Datei anzeigen

@@ -4,4 +4,9 @@ package com.zipcodewilmington.exceptions;
4 4
  * Created by leon on 5/10/17.
5 5
  */ // Checked Exception
6 6
 public final class InvalidPhoneNumberFormatException extends Exception {
7
+    public InvalidPhoneNumberFormatException() {}
8
+
9
+    public InvalidPhoneNumberFormatException(String phoneNumberString) {
10
+
11
+    }
7 12
 }

+ 3
- 0
src/main/java/com/zipcodewilmington/phone/PhoneNumber.java Datei anzeigen

@@ -23,14 +23,17 @@ public final class PhoneNumber {
23 23
     }
24 24
 
25 25
     public String getAreaCode() {
26
+
26 27
         return toString().substring(1, 4);
27 28
     }
28 29
 
29 30
     public String getCentralOfficeCode() {
31
+
30 32
         return toString().substring(6, 9);
31 33
     }
32 34
 
33 35
     public String getPhoneLineCode() {
36
+
34 37
         return toString().substring(10, 14);
35 38
 }
36 39
 

+ 32
- 8
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Datei anzeigen

@@ -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
 
@@ -19,15 +20,28 @@ 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
+        int count = 0;
25
+        int index = 0;
26
+        PhoneNumber[] phoneNumbers = new PhoneNumber[phoneNumberCount];
27
+
28
+        while (count < phoneNumberCount) {
29
+            phoneNumbers[index] = createRandomPhoneNumber();
30
+            count++;
31
+            index++;
32
+        }
33
+        return phoneNumbers;
24 34
     }
25 35
 
26 36
     /**
27 37
      * @return an instance of PhoneNumber with randomly generated phone number value
28 38
      */ //TODO - Implement logic
29
-    public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
39
+    public static PhoneNumber createRandomPhoneNumber() throws InvalidPhoneNumberFormatException {
40
+
41
+        int areaCode = RandomNumberFactory.createInteger(100, 999);
42
+        int centralOfficeCode = RandomNumberFactory.createInteger(100, 999);
43
+        int phoneLineCode = RandomNumberFactory.createInteger(1000, 9999);
44
+        return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
31 45
     }
32 46
 
33 47
 
@@ -37,8 +51,16 @@ public final class PhoneNumberFactory {
37 51
      * @param phoneLineCode     - 4 digit code
38 52
      * @return a new phone number object
39 53
      */ //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);
54
+    public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) throws InvalidPhoneNumberFormatException {
55
+        StringBuilder builder = new StringBuilder("(" + areaCode + ")");
56
+        builder.append("-" + centralOfficeCode).append("-" + phoneLineCode);
57
+        String phoneNumberString = builder.toString();
58
+        try {
59
+            return createPhoneNumber(phoneNumberString);
60
+        }
61
+        catch (InvalidPhoneNumberFormatException ipfe) {
62
+            return null;
63
+        }
42 64
     }
43 65
 
44 66
     /**
@@ -46,7 +68,9 @@ public final class PhoneNumberFactory {
46 68
      * @return a new phone number object
47 69
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48 70
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
71
+    public static PhoneNumber createPhoneNumber (String phoneNumberString) throws InvalidPhoneNumberFormatException  {
72
+        logger.info("Attempting to create a new PhoneNumber object with a value of " + phoneNumberString);
73
+        return new PhoneNumber(phoneNumberString);
74
+
51 75
     }
52 76
 }

+ 2
- 0
src/main/java/com/zipcodewilmington/tools/RandomNumberFactory.java Datei anzeigen

@@ -11,11 +11,13 @@ public abstract class RandomNumberFactory {
11 11
 
12 12
     /** @return a random float between the specified min and max numeric range */
13 13
     public static Float createFloat(float min, float max) {
14
+
14 15
         return random.nextFloat() * (max - min) + min;
15 16
     }
16 17
 
17 18
     /** @return a random integer between the specified min and max numeric range */
18 19
     public static Integer createInteger(Integer min, Integer max) {
20
+
19 21
         return createFloat(min, max).intValue();
20 22
     }
21 23
 }

+ 5
- 5
src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java Datei anzeigen

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