Tennessee Gibbs 6 лет назад
Родитель
Сommit
b0f3ac11b3

+ 2
- 0
src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java Просмотреть файл

4
  * Created by leon on 5/10/17.
4
  * Created by leon on 5/10/17.
5
  */ // Checked Exception
5
  */ // Checked Exception
6
 public final class InvalidPhoneNumberFormatException extends Exception {
6
 public final class InvalidPhoneNumberFormatException extends Exception {
7
+
8
+
7
 }
9
 }

+ 7
- 1
src/main/java/com/zipcodewilmington/phone/PhoneNumber.java Просмотреть файл

8
 public final class PhoneNumber {
8
 public final class PhoneNumber {
9
     private final String phoneNumberString;
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
     // default constructor is uncallable
17
     // default constructor is uncallable
12
-    private PhoneNumber() throws InvalidPhoneNumberFormatException {
18
+    public PhoneNumber() throws InvalidPhoneNumberFormatException {
13
         this(null);
19
         this(null);
14
     }
20
     }
15
 
21
 

+ 60
- 25
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Просмотреть файл

1
 package com.zipcodewilmington.phone;
1
 package com.zipcodewilmington.phone;
2
 
2
 
3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4
+import com.zipcodewilmington.tools.RandomNumberFactory;
4
 
5
 
5
 import java.util.logging.Logger;
6
 import java.util.logging.Logger;
6
 
7
 
10
 public final class PhoneNumberFactory {
11
 public final class PhoneNumberFactory {
11
     private static final Logger logger = Logger.getGlobal();
12
     private static final Logger logger = Logger.getGlobal();
12
 
13
 
13
-    private PhoneNumberFactory() {
14
+    private PhoneNumberFactory() throws InvalidPhoneNumberFormatException {
14
         /** This constructor is private
15
         /** This constructor is private
15
          *  This class is uninstantiable */
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
      * @param phoneNumberCount - number of PhoneNumber objects to instantiate
26
      * @param phoneNumberCount - number of PhoneNumber objects to instantiate
20
      * @return array of randomly generated PhoneNumber objects
27
      * @return array of randomly generated PhoneNumber objects
21
      */ //TODO - Implement logic
28
      */ //TODO - Implement logic
22
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
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 Просмотреть файл

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