#23 jdot2791

Открыто
jdot2791 хочет смерджить 1 коммит(ов) из jdot2791/CR-MicroLabs-ExceptionsAndLogging:master в master

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

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

+ 21
- 5
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
 
6
+import java.util.ArrayList;
7
+import java.util.Random;
5
 import java.util.logging.Logger;
8
 import java.util.logging.Logger;
6
 
9
 
7
 /**
10
 /**
20
      * @return array of randomly generated PhoneNumber objects
23
      * @return array of randomly generated PhoneNumber objects
21
      */ //TODO - Implement logic
24
      */ //TODO - Implement logic
22
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
25
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
26
+        PhoneNumber[] phoneNumbers = new PhoneNumber[phoneNumberCount];
27
+        for(int i = 0; i <phoneNumberCount; i++)
28
+            phoneNumbers[i] = createRandomPhoneNumber();
29
+        return phoneNumbers;
24
     }
30
     }
25
 
31
 
26
     /**
32
     /**
27
      * @return an instance of PhoneNumber with randomly generated phone number value
33
      * @return an instance of PhoneNumber with randomly generated phone number value
28
      */ //TODO - Implement logic
34
      */ //TODO - Implement logic
29
     public static PhoneNumber createRandomPhoneNumber() {
35
     public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
36
+        Integer areaCode = RandomNumberFactory.lengthOfNumbers(3);
37
+        Integer centralOfficeCode = RandomNumberFactory.lengthOfNumbers(3);
38
+        Integer phoneLineCode = RandomNumberFactory.lengthOfNumbers(4);
39
+
40
+        return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
31
     }
41
     }
32
 
42
 
33
 
43
 
38
      * @return a new phone number object
48
      * @return a new phone number object
39
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
49
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
50
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
41
-        return createPhoneNumber(null);
51
+        try{
52
+        return createPhoneNumber("(" + areaCode +")-" + centralOfficeCode + "-" + phoneLineCode);
53
+        } catch (InvalidPhoneNumberFormatException e) {
54
+            logger.info("Invalid Format. Try again.");
55
+            return null;
56
+        }
42
     }
57
     }
43
 
58
 
44
     /**
59
     /**
46
      * @return a new phone number object
61
      * @return a new phone number object
47
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
62
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48
      */ // TODO - Add throws statement to method signature
63
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
64
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
65
+            logger.info("Trying to create phoneNumber");
66
+            return new PhoneNumber(phoneNumberString);
51
     }
67
     }
52
 }
68
 }

+ 9
- 0
src/main/java/com/zipcodewilmington/tools/RandomNumberFactory.java Просмотреть файл

18
     public static Integer createInteger(Integer min, Integer max) {
18
     public static Integer createInteger(Integer min, Integer max) {
19
         return createFloat(min, max).intValue();
19
         return createFloat(min, max).intValue();
20
     }
20
     }
21
+
22
+    public static Integer lengthOfNumbers(int num) {
23
+        StringBuilder numbers = new StringBuilder();
24
+
25
+        for(int i = 1; i <= num; i++) {
26
+            numbers.append(i == 1 ? createInteger(1, 9) + "" : createInteger(0, 9) + "");
27
+        }
28
+        return Integer.valueOf(numbers.toString());
29
+    }
21
 }
30
 }