Tennessee Gibbs 6 yıl önce
ebeveyn
işleme
b0f3ac11b3

+ 2
- 0
src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java Dosyayı Görüntüle

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

+ 7
- 1
src/main/java/com/zipcodewilmington/phone/PhoneNumber.java Dosyayı Görüntüle

@@ -8,8 +8,14 @@ import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
8 8
 public final class PhoneNumber {
9 9
     private final String phoneNumberString;
10 10
 
11
+//    String stringRepresentation = "(302)-312-5555";
12
+//    PhoneNumber phoneNumber = new PhoneNumber(stringRepresentation);
13
+//    String areaCode = phoneNumber.getAreaCode();
14
+//    String centralOfficeCode = phoneNumber.getCentralOfficeCode();
15
+//    String phoneLineCode = phoneNumber.getPhoneLineCode();
16
+
11 17
     // default constructor is uncallable
12
-    private PhoneNumber() throws InvalidPhoneNumberFormatException {
18
+    public PhoneNumber() throws InvalidPhoneNumberFormatException {
13 19
         this(null);
14 20
     }
15 21
 

+ 60
- 25
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Dosyayı Görüntüle

@@ -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
 
@@ -10,43 +11,77 @@ import java.util.logging.Logger;
10 11
 public final class PhoneNumberFactory {
11 12
     private static final Logger logger = Logger.getGlobal();
12 13
 
13
-    private PhoneNumberFactory() {
14
+    private PhoneNumberFactory() throws InvalidPhoneNumberFormatException {
14 15
         /** This constructor is private
15 16
          *  This class is uninstantiable */
16 17
     }
18
+//    String stringRepresentation = "(302)-312-5555";
19
+//    PhoneNumber phoneNumber = new PhoneNumber(stringRepresentation);
20
+//    String areaCode = phoneNumber.getAreaCode();
21
+//    String centralOfficeCode = phoneNumber.getCentralOfficeCode();
22
+//    String phoneLineCode = phoneNumber.getPhoneLineCode();
23
+
17 24
 
18 25
     /**
19 26
      * @param phoneNumberCount - number of PhoneNumber objects to instantiate
20 27
      * @return array of randomly generated PhoneNumber objects
21 28
      */ //TODO - Implement logic
22 29
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
24
-    }
30
+        PhoneNumber[] phoneBook = new PhoneNumber[phoneNumberCount];
25 31
 
26
-    /**
27
-     * @return an instance of PhoneNumber with randomly generated phone number value
28
-     */ //TODO - Implement logic
29
-    public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
31
-    }
32
+        for (int i = 1; i <= phoneNumberCount; i++) {
32 33
 
34
+            phoneBook[i - 1] = createRandomPhoneNumber( );
33 35
 
34
-    /**
35
-     * @param areaCode          - 3 digit code
36
-     * @param centralOfficeCode - 3 digit code
37
-     * @param phoneLineCode     - 4 digit code
38
-     * @return a new phone number object
39
-     */ //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);
36
+        }
37
+        return phoneBook;
42 38
     }
43 39
 
44
-    /**
45
-     * @param phoneNumberString - some String corresponding to a phone number whose format is `(###)-###-####`
46
-     * @return a new phone number object
47
-     * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48
-     */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
40
+
41
+        /**
42
+         * @return an instance of PhoneNumber with randomly generated phone number value
43
+         */ //TODO - Implement logic
44
+        public static PhoneNumber createRandomPhoneNumber ( ) {
45
+
46
+
47
+            int aC = RandomNumberFactory.createInteger( 100, 999 );
48
+            int cOc = RandomNumberFactory.createInteger( 100, 999 );
49
+            int pLc = RandomNumberFactory.createInteger( 1000, 9999 );
50
+
51
+            return createPhoneNumberSafely( aC, cOc, pLc );
52
+        }
53
+
54
+
55
+        /**
56
+         * @param areaCode          - 3 digit code
57
+         * @param centralOfficeCode - 3 digit code
58
+         * @param phoneLineCode     - 4 digit code
59
+         * @return a new phone number object
60
+         */ //TODO - if input is valid, return respective PhoneNumber object, else return null
61
+        public static PhoneNumber createPhoneNumberSafely ( int areaCode, int centralOfficeCode, int phoneLineCode){
62
+            String ph = "(" + areaCode + ")" + "-" + centralOfficeCode + "-" + phoneLineCode;
63
+
64
+            try {
65
+                return createPhoneNumber( ph );
66
+
67
+            } catch
68
+                    (InvalidPhoneNumberFormatException e) {
69
+                logger.info(ph + " is not a valid phone number!");
70
+                return null;
71
+            }
72
+        }
73
+
74
+        /**
75
+         * @param phoneNumberString - some String corresponding to a phone number whose format is `(###)-###-####`
76
+         * @return a new phone number object
77
+         * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
78
+         */ // TODO - Add throws statement to method signature
79
+        public static PhoneNumber createPhoneNumber ( String ph) throws InvalidPhoneNumberFormatException {
80
+            PhoneNumber phoneNumber;
81
+            logger.finest("Attempting to create a new PhoneNumber object with a value of " + ph );
82
+            return phoneNumber = new PhoneNumber( ph );
83
+        }
84
+
85
+
51 86
     }
52
-}
87
+

+ 15
- 10
src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java Dosyayı Görüntüle

@@ -21,42 +21,45 @@ 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;
28 28
         int phoneLineCode = 0;
29 29
 
30 30
         // : When
31
-        PhoneNumber phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
31
+        PhoneNumber phoneNumber = null;
32
+        phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
32 33
 
33 34
         // : Then
34 35
         Assert.assertEquals(null, phoneNumber);
35 36
     }
36 37
 
37 38
     @Test
38
-    public void testGetAreaCode() {
39
+    public void testGetAreaCode() throws InvalidPhoneNumberFormatException {
39 40
         // : Given
40 41
         Integer areaCode = 302;
41 42
         int centralOfficeCode = 312;
42 43
         int phoneLineCode = 5555;
43 44
 
44 45
         // : When
45
-        PhoneNumber phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
46
+        PhoneNumber phoneNumber = null;
47
+        phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
46 48
 
47 49
         // : Then
48 50
         Assert.assertEquals(phoneNumber.getAreaCode(), areaCode.toString());
49 51
     }
50 52
 
51 53
     @Test
52
-    public void testGetCentralOfficeCode() {
54
+    public void testGetCentralOfficeCode() throws InvalidPhoneNumberFormatException {
53 55
         // : Given
54 56
         int areaCode = 302;
55 57
         Integer centralOfficeCode = 312;
56 58
         int phoneLineCode = 5555;
57 59
 
58 60
         // : When
59
-        PhoneNumber phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
61
+        PhoneNumber phoneNumber = null;
62
+        phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
60 63
 
61 64
         // : Then
62 65
         Assert.assertEquals(phoneNumber.getCentralOfficeCode(), centralOfficeCode.toString());
@@ -64,25 +67,27 @@ public class PhoneNumberFactoryTest {
64 67
 
65 68
 
66 69
     @Test
67
-    public void testPhoneLineCode() {
70
+    public void testPhoneLineCode() throws InvalidPhoneNumberFormatException {
68 71
         // : Given
69 72
         int areaCode = 302;
70 73
         int centralOfficeCode = 312;
71 74
         Integer phoneLineCode = 5555;
72 75
 
73 76
         // : When
74
-        PhoneNumber phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
77
+        PhoneNumber phoneNumber = null;
78
+        phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
75 79
 
76 80
         // : Then
77 81
         Assert.assertEquals(phoneNumber.getPhoneLineCode(), phoneLineCode.toString());
78 82
     }
79 83
 
80 84
     @Test
81
-    public void testCreateRandomPhoneNumber() {
85
+    public void testCreateRandomPhoneNumber() throws InvalidPhoneNumberFormatException {
82 86
         for (int i = 0; i < 999; i++) {
83 87
             // : Given
84 88
             // : When
85
-            PhoneNumber phoneNumber = PhoneNumberFactory.createRandomPhoneNumber();
89
+            PhoneNumber phoneNumber = null;
90
+            phoneNumber = PhoneNumberFactory.createRandomPhoneNumber();
86 91
 
87 92
             // : Then
88 93
             Assert.assertTrue(phoneNumber != null);