浏览代码

Exceptions DONE

Nathan Hall 6 年前
父节点
当前提交
788d1e3042
共有 1 个文件被更改,包括 25 次插入5 次删除
  1. 25
    5
      src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java

+ 25
- 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
 
5
 import java.util.logging.Logger;
6
 import java.util.logging.Logger;
6
 
7
 
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[] pnArray = new PhoneNumber[phoneNumberCount];
25
+        for(int i = 0; i < phoneNumberCount; i++){
26
+            pnArray[i] = createRandomPhoneNumber();
27
+        }
28
+
29
+
30
+        return pnArray;
24
     }
31
     }
25
 
32
 
26
     /**
33
     /**
27
      * @return an instance of PhoneNumber with randomly generated phone number value
34
      * @return an instance of PhoneNumber with randomly generated phone number value
28
      */ //TODO - Implement logic
35
      */ //TODO - Implement logic
29
     public static PhoneNumber createRandomPhoneNumber() {
36
     public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
37
+        Integer areaCode = RandomNumberFactory.createInteger(100, 999);
38
+        Integer centralOfficeCode = RandomNumberFactory.createInteger(100, 999);
39
+        Integer phoneLineCode = RandomNumberFactory.createInteger(1000, 9999);
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
+        StringBuilder builder = new StringBuilder();
52
+        builder.append("(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode);
53
+
54
+        try {
55
+            return createPhoneNumber(builder.toString());
56
+        } catch (InvalidPhoneNumberFormatException e) {
57
+            logger.info(builder + " is not a valid phone number");
58
+            return null;
59
+        }
42
     }
60
     }
43
 
61
 
44
     /**
62
     /**
46
      * @return a new phone number object
64
      * @return a new phone number object
47
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
65
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48
      */ // TODO - Add throws statement to method signature
66
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
67
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
68
+        logger.info("Attempting to create a new PhoneNumber object with a value of " + phoneNumberString);
69
+
70
+        return new PhoneNumber(phoneNumberString);
51
     }
71
     }
52
 }
72
 }