瀏覽代碼

should've done this B4 assessment

rjsmall90 6 年之前
父節點
當前提交
10534df2f6

+ 1
- 0
src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java 查看文件

@@ -4,4 +4,5 @@ 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
+
7 8
 }

+ 28
- 6
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java 查看文件

@@ -2,6 +2,8 @@ package com.zipcodewilmington.phone;
2 2
 
3 3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4 4
 
5
+import java.lang.reflect.Array;
6
+import java.util.ArrayList;
5 7
 import java.util.logging.Logger;
6 8
 
7 9
 /**
@@ -20,14 +22,23 @@ public final class PhoneNumberFactory {
20 22
      * @return array of randomly generated PhoneNumber objects
21 23
      */ //TODO - Implement logic
22 24
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
25
+        ArrayList<PhoneNumber> phone = new ArrayList();
26
+        phone.add(createRandomPhoneNumber(phoneNumberCount));
27
+        PhoneNumber [] number = (PhoneNumber[]) phone.toArray();
28
+
29
+
30
+        return number;
24 31
     }
25 32
 
26 33
     /**
27 34
      * @return an instance of PhoneNumber with randomly generated phone number value
28 35
      */ //TODO - Implement logic
29
-    public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
36
+    public static PhoneNumber createRandomPhoneNumber(int numbers) {
37
+        double number  = Math.floor(100+Math.random()*900);
38
+         double number2 =  Math.floor(1000+Math.random()*9000);
39
+
40
+
41
+        return createPhoneNumberSafely((int)number, (int)number, (int)number2);
31 42
     }
32 43
 
33 44
 
@@ -38,7 +49,16 @@ public final class PhoneNumberFactory {
38 49
      * @return a new phone number object
39 50
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40 51
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
41
-        return createPhoneNumber(null);
52
+        try {
53
+            String phoneNumber = "(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode;
54
+            return createPhoneNumber(phoneNumber);
55
+        }catch (InvalidPhoneNumberFormatException e) {
56
+            logger.finest("(###)-###-#### is not a valid phone number");
57
+            e.printStackTrace();
58
+        }
59
+
60
+        return null;
61
+
42 62
     }
43 63
 
44 64
     /**
@@ -46,7 +66,9 @@ public final class PhoneNumberFactory {
46 66
      * @return a new phone number object
47 67
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48 68
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
69
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
70
+        PhoneNumber number = new PhoneNumber(phoneNumberString);
71
+        logger.finest("Attempting to create a new PhoneNumber object with a value of (###)-###-####");
72
+        return number;
51 73
     }
52 74
 }

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

@@ -79,10 +79,11 @@ public class PhoneNumberFactoryTest {
79 79
 
80 80
     @Test
81 81
     public void testCreateRandomPhoneNumber() {
82
+        int x = 0;
82 83
         for (int i = 0; i < 999; i++) {
83 84
             // : Given
84 85
             // : When
85
-            PhoneNumber phoneNumber = PhoneNumberFactory.createRandomPhoneNumber();
86
+            PhoneNumber phoneNumber = PhoneNumberFactory.createRandomPhoneNumber(x);
86 87
 
87 88
             // : Then
88 89
             Assert.assertTrue(phoneNumber != null);