Elliott Stansbury 6 gadus atpakaļ
vecāks
revīzija
133b84ee69

+ 2
- 0
src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java Parādīt failu

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

+ 47
- 5
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Parādīt failu

@@ -2,6 +2,8 @@ package com.zipcodewilmington.phone;
2 2
 
3 3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4 4
 
5
+import java.util.Random;
6
+import java.util.logging.Level;
5 7
 import java.util.logging.Logger;
6 8
 
7 9
 /**
@@ -10,6 +12,7 @@ import java.util.logging.Logger;
10 12
 public final class PhoneNumberFactory {
11 13
     private static final Logger logger = Logger.getGlobal();
12 14
 
15
+
13 16
     private PhoneNumberFactory() {
14 17
         /** This constructor is private
15 18
          *  This class is uninstantiable */
@@ -20,14 +23,33 @@ public final class PhoneNumberFactory {
20 23
      * @return array of randomly generated PhoneNumber objects
21 24
      */ //TODO - Implement logic
22 25
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
26
+
27
+        PhoneNumber[] arr = new PhoneNumber[phoneNumberCount];
28
+
29
+        for(int i = 0; i < phoneNumberCount;i++){
30
+
31
+                arr[i] = createRandomPhoneNumber();
32
+            }
33
+
34
+
35
+        return arr;
24 36
     }
25 37
 
26 38
     /**
27 39
      * @return an instance of PhoneNumber with randomly generated phone number value
28 40
      */ //TODO - Implement logic
29 41
     public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
42
+
43
+        Random random = new Random();
44
+
45
+
46
+        int areaCodeRandom = random.nextInt(900) + 100;
47
+        int centralOfficeCodeRandom = random.nextInt(900) + 100;
48
+        int phoneLineCodeRandom = random.nextInt(9000)+ 1000;
49
+
50
+
51
+
52
+        return createPhoneNumberSafely(areaCodeRandom, centralOfficeCodeRandom, phoneLineCodeRandom);
31 53
     }
32 54
 
33 55
 
@@ -38,7 +60,25 @@ public final class PhoneNumberFactory {
38 60
      * @return a new phone number object
39 61
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40 62
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
41
-        return createPhoneNumber(null);
63
+
64
+        try {
65
+
66
+            String phoneNumberString;
67
+            String first3 = Integer.toString(areaCode);
68
+            String second3 = Integer.toString(centralOfficeCode);
69
+            String last4 = Integer.toString(phoneLineCode);
70
+
71
+            phoneNumberString = "(" + first3 + ")-" + second3 + "-" + last4;
72
+
73
+
74
+            return createPhoneNumber(phoneNumberString);
75
+        }catch(InvalidPhoneNumberFormatException ex){
76
+            logger.setLevel(Level.WARNING);
77
+            logger.warning("(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode + " is not a valid phone number\n");
78
+            return null;
79
+        }
80
+
81
+
42 82
     }
43 83
 
44 84
     /**
@@ -46,7 +86,9 @@ public final class PhoneNumberFactory {
46 86
      * @return a new phone number object
47 87
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48 88
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
89
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
90
+        logger.setLevel(Level.INFO);
91
+        logger.info("Attempting to create a new PhoneNumber object with a value of " + phoneNumberString);
92
+    return new PhoneNumber(phoneNumberString);
51 93
     }
52 94
 }

+ 18
- 0
src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java Parādīt failu

@@ -88,4 +88,22 @@ public class PhoneNumberFactoryTest {
88 88
             Assert.assertTrue(phoneNumber != null);
89 89
         }
90 90
     }
91
+
92
+    @Test
93
+    public void testCreateRandomPhoneNumberArray(){
94
+        PhoneNumber[] phoneNumber = PhoneNumberFactory.createRandomPhoneNumberArray(5);
95
+
96
+        int expected = 5;
97
+        int actual = phoneNumber.length;
98
+
99
+        Assert.assertEquals(expected,actual,.01);
100
+
101
+        PhoneNumber[] phoneNumber1 = PhoneNumberFactory.createRandomPhoneNumberArray(10);
102
+
103
+
104
+        int expected1 = 10;
105
+        int actual1 = phoneNumber1.length;
106
+
107
+        Assert.assertEquals(expected1,actual1,.01);
108
+    }
91 109
 }