#30 lewis lab

Avoinna
LewisDominguez haluaa yhdistää 1 committia lähteestä LewisDominguez/CR-MicroLabs-ExceptionsAndLogging:master kohteeseen master

+ 4
- 1
src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java Näytä tiedosto

1
 package com.zipcodewilmington.exceptions;
1
 package com.zipcodewilmington.exceptions;
2
 
2
 
3
+
4
+import java.io.IOException;
5
+
3
 /**
6
 /**
4
  * Created by leon on 5/10/17.
7
  * Created by leon on 5/10/17.
5
  */ // Checked Exception
8
  */ // Checked Exception
6
-public final class InvalidPhoneNumberFormatException extends Exception {
9
+public final class InvalidPhoneNumberFormatException extends IOException {
7
 }
10
 }

+ 12
- 8
src/main/java/com/zipcodewilmington/phone/PhoneNumber.java Näytä tiedosto

16
     // non-default constructor is package-protected
16
     // non-default constructor is package-protected
17
     protected PhoneNumber(String phoneNumber) throws InvalidPhoneNumberFormatException {
17
     protected PhoneNumber(String phoneNumber) throws InvalidPhoneNumberFormatException {
18
         //validate phone number with format `(###)-###-####`
18
         //validate phone number with format `(###)-###-####`
19
-        if (!phoneNumber.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}")) {
20
-            throw new InvalidPhoneNumberFormatException();
21
-        }
22
-        this.phoneNumberString = phoneNumber;
19
+
20
+            if (!phoneNumber.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}")) {
21
+                throw new InvalidPhoneNumberFormatException();
22
+            }
23
+            this.phoneNumberString = phoneNumber;
24
+
25
+
26
+
23
     }
27
     }
24
 
28
 
25
-    public String getAreaCode() {
29
+    public String getAreaCode() throws InvalidPhoneNumberFormatException{
26
         return toString().substring(1, 4);
30
         return toString().substring(1, 4);
27
     }
31
     }
28
 
32
 
29
-    public String getCentralOfficeCode() {
33
+    public String getCentralOfficeCode() throws InvalidPhoneNumberFormatException  {
30
         return toString().substring(6, 9);
34
         return toString().substring(6, 9);
31
     }
35
     }
32
 
36
 
33
-    public String getPhoneLineCode() {
37
+    public String getPhoneLineCode() throws InvalidPhoneNumberFormatException{
34
         return toString().substring(10, 14);
38
         return toString().substring(10, 14);
35
 }
39
 }
36
 
40
 
37
     @Override
41
     @Override
38
-    public String toString() {
42
+    public String toString(){
39
         return phoneNumberString;
43
         return phoneNumberString;
40
     }
44
     }
41
 }
45
 }

+ 36
- 9
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Näytä tiedosto

1
 package com.zipcodewilmington.phone;
1
 package com.zipcodewilmington.phone;
2
 
2
 
3
+
3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
5
+import com.zipcodewilmington.tools.RandomNumberFactory;
6
+
4
 
7
 
8
+import java.util.logging.Level;
5
 import java.util.logging.Logger;
9
 import java.util.logging.Logger;
6
 
10
 
7
 /**
11
 /**
8
  * Created by leon on 5/1/17.
12
  * Created by leon on 5/1/17.
9
  */
13
  */
10
-public final class PhoneNumberFactory {
14
+public final class PhoneNumberFactory  extends RandomNumberFactory{
11
     private static final Logger logger = Logger.getGlobal();
15
     private static final Logger logger = Logger.getGlobal();
12
 
16
 
13
     private PhoneNumberFactory() {
17
     private PhoneNumberFactory() {
19
      * @param phoneNumberCount - number of PhoneNumber objects to instantiate
23
      * @param phoneNumberCount - number of PhoneNumber objects to instantiate
20
      * @return array of randomly generated PhoneNumber objects
24
      * @return array of randomly generated PhoneNumber objects
21
      */ //TODO - Implement logic
25
      */ //TODO - Implement logic
22
-    public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
26
+    public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) throws InvalidPhoneNumberFormatException {
27
+
28
+        PhoneNumber[] phn = new PhoneNumber[phoneNumberCount];
29
+        for(int i = 0; i < phoneNumberCount; i++) {
30
+            phn[i] = createRandomPhoneNumber();
31
+        }
32
+        return phn;
24
     }
33
     }
25
 
34
 
26
     /**
35
     /**
27
      * @return an instance of PhoneNumber with randomly generated phone number value
36
      * @return an instance of PhoneNumber with randomly generated phone number value
28
      */ //TODO - Implement logic
37
      */ //TODO - Implement logic
29
-    public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
38
+    public static PhoneNumber createRandomPhoneNumber() throws InvalidPhoneNumberFormatException {
39
+        int areaCode =  RandomNumberFactory.createInteger(100,999);
40
+        int centralOfficeCode = RandomNumberFactory.createInteger(100,999);
41
+        int phoneLineCode = RandomNumberFactory.createInteger(1000,9999);
42
+        return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
43
+
31
     }
44
     }
32
 
45
 
33
 
46
 
37
      * @param phoneLineCode     - 4 digit code
50
      * @param phoneLineCode     - 4 digit code
38
      * @return a new phone number object
51
      * @return a new phone number object
39
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
52
      */ //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);
53
+    public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) throws InvalidPhoneNumberFormatException{
54
+
55
+
56
+        try{
57
+            String num = "(" + areaCode + ")" + centralOfficeCode + "-" + phoneLineCode;
58
+            return createPhoneNumber(num);
59
+
60
+        } catch (InvalidPhoneNumberFormatException e) {
61
+            logger.log(Level.INFO, "(" + areaCode + ")" + centralOfficeCode + "-" + phoneLineCode + " is not a valid phone number");
62
+
63
+            return null;
64
+        }
42
     }
65
     }
43
 
66
 
44
     /**
67
     /**
46
      * @return a new phone number object
69
      * @return a new phone number object
47
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
70
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48
      */ // TODO - Add throws statement to method signature
71
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
72
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
73
+
74
+        logger.log(Level.INFO,"Attempting to create a new PhoneNumber object with a value of " + phoneNumberString);
75
+         return new PhoneNumber(phoneNumberString);
76
+
77
+
51
     }
78
     }
52
 }
79
 }

+ 5
- 5
src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java Näytä tiedosto

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