#31 KennethT867 ExceptionsAndLogging

Open
KennethT867 wil 1 commits van KennethT867/CR-MicroLabs-ExceptionsAndLogging:master samenvoegen met master

+ 22
- 8
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Bestand weergeven

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
 
7
 /**
8
 /**
8
  * Created by leon on 5/1/17.
9
  * Created by leon on 5/1/17.
9
  */
10
  */
10
-public final class PhoneNumberFactory {
11
+public final class PhoneNumberFactory extends RandomNumberFactory {
11
     private static final Logger logger = Logger.getGlobal();
12
     private static final Logger logger = Logger.getGlobal();
12
 
13
 
13
     private PhoneNumberFactory() {
14
     private PhoneNumberFactory() {
20
      * @return array of randomly generated PhoneNumber objects
21
      * @return array of randomly generated PhoneNumber objects
21
      */ //TODO - Implement logic
22
      */ //TODO - Implement logic
22
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
24
+        PhoneNumber[] phoneNumbers = new PhoneNumber[phoneNumberCount];
25
+        return phoneNumbers;
24
     }
26
     }
25
 
27
 
26
     /**
28
     /**
27
      * @return an instance of PhoneNumber with randomly generated phone number value
29
      * @return an instance of PhoneNumber with randomly generated phone number value
28
      */ //TODO - Implement logic
30
      */ //TODO - Implement logic
29
-    public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
31
+    public static PhoneNumber createRandomPhoneNumber() throws InvalidPhoneNumberFormatException {
32
+        return createPhoneNumberSafely(createInteger(100, 800), createInteger(100, 899), createInteger(1000, 8888));
31
     }
33
     }
32
 
34
 
33
 
35
 
37
      * @param phoneLineCode     - 4 digit code
39
      * @param phoneLineCode     - 4 digit code
38
      * @return a new phone number object
40
      * @return a new phone number object
39
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
41
      */ //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);
42
+    public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) throws InvalidPhoneNumberFormatException {
43
+        PhoneNumber x;
44
+        try {
45
+            x = createPhoneNumber(String.format("(%s)-%s-%s", areaCode, centralOfficeCode, phoneLineCode));
46
+           // return x;
47
+
48
+        } catch(InvalidPhoneNumberFormatException e){
49
+            e.printStackTrace();
50
+            logger.info("("+areaCode+")-"+centralOfficeCode+"-"+phoneLineCode+" is not a valid phone number");
51
+            x = null;
52
+        }
53
+        return x;
42
     }
54
     }
43
 
55
 
44
     /**
56
     /**
46
      * @return a new phone number object
58
      * @return a new phone number object
47
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
59
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48
      */ // TODO - Add throws statement to method signature
60
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
61
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
62
+        PhoneNumber phoneNumber = new PhoneNumber(phoneNumberString);
63
+        logger.info("Attempting to create a new PhoneNumber object with a value of "+ phoneNumberString);
64
+        return phoneNumber;
51
     }
65
     }
52
 }
66
 }

+ 5
- 5
src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java Bestand weergeven

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;
35
     }
35
     }
36
 
36
 
37
     @Test
37
     @Test
38
-    public void testGetAreaCode() {
38
+    public void testGetAreaCode() throws InvalidPhoneNumberFormatException {
39
         // : Given
39
         // : Given
40
         Integer areaCode = 302;
40
         Integer areaCode = 302;
41
         int centralOfficeCode = 312;
41
         int centralOfficeCode = 312;
49
     }
49
     }
50
 
50
 
51
     @Test
51
     @Test
52
-    public void testGetCentralOfficeCode() {
52
+    public void testGetCentralOfficeCode() throws InvalidPhoneNumberFormatException {
53
         // : Given
53
         // : Given
54
         int areaCode = 302;
54
         int areaCode = 302;
55
         Integer centralOfficeCode = 312;
55
         Integer centralOfficeCode = 312;
64
 
64
 
65
 
65
 
66
     @Test
66
     @Test
67
-    public void testPhoneLineCode() {
67
+    public void testPhoneLineCode() throws InvalidPhoneNumberFormatException {
68
         // : Given
68
         // : Given
69
         int areaCode = 302;
69
         int areaCode = 302;
70
         int centralOfficeCode = 312;
70
         int centralOfficeCode = 312;
78
     }
78
     }
79
 
79
 
80
     @Test
80
     @Test
81
-    public void testCreateRandomPhoneNumber() {
81
+    public void testCreateRandomPhoneNumber() throws InvalidPhoneNumberFormatException {
82
         for (int i = 0; i < 999; i++) {
82
         for (int i = 0; i < 999; i++) {
83
             // : Given
83
             // : Given
84
             // : When
84
             // : When