ソースを参照

Create phone number good, create safely experimenting

Mitch Taylor 8 年 前
コミット
2f7aab71b3
共有1 個のファイルを変更した13 個の追加5 個の削除を含む
  1. 13
    5
      src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java

+ 13
- 5
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java ファイルの表示

@@ -26,7 +26,7 @@ public final class PhoneNumberFactory {
26 26
     /**
27 27
      * @return an instance of PhoneNumber with randomly generated phone number value
28 28
      */ //TODO - Implement logic
29
-    public static PhoneNumber createRandomPhoneNumber() {
29
+    public static PhoneNumber createRandomPhoneNumber() throws InvalidPhoneNumberFormatException {
30 30
         return createPhoneNumberSafely(-1, -1, -1);
31 31
     }
32 32
 
@@ -37,8 +37,16 @@ public final class PhoneNumberFactory {
37 37
      * @param phoneLineCode     - 4 digit code
38 38
      * @return a new phone number object
39 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);
40
+    public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) throws InvalidPhoneNumberFormatException{
41
+        try {
42
+            // if (3 == String.valueOf(areaCode).length() && 3 == String.valueOf(centralOfficeCode).length() && 4 == String.valueOf(phoneLineCode).length()) {
43
+            String tempString = String.valueOf(areaCode) + String.valueOf(centralOfficeCode) + String.valueOf(phoneLineCode);
44
+            return createPhoneNumber(tempString);
45
+            //     return createPhoneNumber(tempString);
46
+            // }
47
+        } catch (InvalidPhoneNumberFormatException inv) {
48
+            return null;
49
+        }
42 50
     }
43 51
 
44 52
     /**
@@ -46,7 +54,7 @@ public final class PhoneNumberFactory {
46 54
      * @return a new phone number object
47 55
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48 56
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
57
+    public static PhoneNumber createPhoneNumber (String phoneNumberString) throws InvalidPhoneNumberFormatException {
58
+        return new PhoneNumber(phoneNumberString);
51 59
     }
52 60
 }