浏览代码

Merge 7586165ece89485b1a2be0a1612a0ff86458ab95 into 0665dd6f3de2bb92a7f856bde34efac87df0531f

Mitch Taylor 8 年前
父节点
当前提交
77facdab9b
没有帐户链接到提交者的电子邮件

+ 43
- 8
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java 查看文件

@@ -1,7 +1,9 @@
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
 /**
@@ -19,15 +21,24 @@ public final class PhoneNumberFactory {
19 21
      * @param phoneNumberCount - number of PhoneNumber objects to instantiate
20 22
      * @return array of randomly generated PhoneNumber objects
21 23
      */ //TODO - Implement logic
22
-    public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
24
+    public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) throws InvalidPhoneNumberFormatException{
25
+        PhoneNumber[] phoneNumbers = new PhoneNumber[phoneNumberCount];
26
+        for (int i = 0; i < phoneNumberCount; i++) {
27
+            phoneNumbers[i] = createRandomPhoneNumber();
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
+        try {
37
+            int areaCode = RandomNumberFactory.createInteger(100, 999);
38
+            int officeCode = RandomNumberFactory.createInteger(100, 999);
39
+            int phoneLine = RandomNumberFactory.createInteger(1000, 9999);
40
+            return createPhoneNumberSafely(areaCode, officeCode, phoneLine);
41
+        } catch (InvalidPhoneNumberFormatException e) {return null;}
31 42
     }
32 43
 
33 44
 
@@ -37,8 +48,18 @@ public final class PhoneNumberFactory {
37 48
      * @param phoneLineCode     - 4 digit code
38 49
      * @return a new phone number object
39 50
      */ //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);
51
+    public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) throws InvalidPhoneNumberFormatException{
52
+        String tempString = "";
53
+        try {
54
+            // if (3 == String.valueOf(areaCode).length() && 3 == String.valueOf(centralOfficeCode).length() && 4 == String.valueOf(phoneLineCode).length()) {
55
+            tempString = String.valueOf(areaCode) + String.valueOf(centralOfficeCode) + String.valueOf(phoneLineCode);
56
+            return createPhoneNumber(tempString);
57
+            //     return createPhoneNumber(tempString);
58
+            // }
59
+        } catch (InvalidPhoneNumberFormatException inv) {
60
+            logger.log(Level.FINE, (tempString + "is not a valid phone number"));
61
+            return null;
62
+        }
42 63
     }
43 64
 
44 65
     /**
@@ -46,7 +67,21 @@ public final class PhoneNumberFactory {
46 67
      * @return a new phone number object
47 68
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48 69
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
70
+    public static PhoneNumber createPhoneNumber (String phoneNumberString) throws InvalidPhoneNumberFormatException {
71
+        StringBuilder sb = new StringBuilder();
72
+        try {
73
+            sb.append("(");
74
+            sb.append(phoneNumberString.substring(0, 3));
75
+            sb.append(")-");
76
+            sb.append(phoneNumberString.substring(3, 6));
77
+            sb.append("-");
78
+            sb.append(phoneNumberString.substring(6));
79
+            return new PhoneNumber(sb.toString());
80
+        } catch (InvalidPhoneNumberFormatException e) {
81
+            logger.log(Level.FINE, ("Attempting to create a new PhoneNumber object with a value of " + phoneNumberString));
82
+            return null;
83
+        }
51 84
     }
52 85
 }
86
+
87
+// 012-345-6789

+ 1
- 1
src/main/java/com/zipcodewilmington/tools/RandomNumberFactory.java 查看文件

@@ -11,7 +11,7 @@ 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
-        return random.nextFloat() * (max - min) + min;
14
+        return random.nextFloat() * (max - min) + 1;
15 15
     }
16 16
 
17 17
     /** @return a random integer between the specified min and max numeric range */

+ 9
- 9
src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java 查看文件

@@ -21,21 +21,21 @@ public class PhoneNumberFactoryTest {
21 21
     }
22 22
 
23 23
     @Test
24
-    public void testCreatePhoneNumberSafely() {
24
+    public void testCreatePhoneNumberSafely() throws InvalidPhoneNumberFormatException {
25 25
         // : Given
26
-        int areaCode = 0;
27
-        int centralOfficeCode = 0;
28
-        int phoneLineCode = 0;
26
+        int areaCode = 302;
27
+        int centralOfficeCode = 228;
28
+        int phoneLineCode = 5151;
29 29
 
30 30
         // : When
31 31
         PhoneNumber phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
32 32
 
33 33
         // : Then
34
-        Assert.assertEquals(null, phoneNumber);
34
+        Assert.assertEquals("(302)-228-5151", phoneNumber.toString());
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