Bläddra i källkod

Exceptions DONE

Nathan Hall 6 år sedan
förälder
incheckning
788d1e3042
1 ändrade filer med 25 tillägg och 5 borttagningar
  1. 25
    5
      src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java

+ 25
- 5
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Visa fil

@@ -1,6 +1,7 @@
1 1
 package com.zipcodewilmington.phone;
2 2
 
3 3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4
+import com.zipcodewilmington.tools.RandomNumberFactory;
4 5
 
5 6
 import java.util.logging.Logger;
6 7
 
@@ -20,14 +21,23 @@ public final class PhoneNumberFactory {
20 21
      * @return array of randomly generated PhoneNumber objects
21 22
      */ //TODO - Implement logic
22 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 34
      * @return an instance of PhoneNumber with randomly generated phone number value
28 35
      */ //TODO - Implement logic
29 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,7 +48,15 @@ public final class PhoneNumberFactory {
38 48
      * @return a new phone number object
39 49
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40 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,7 +64,9 @@ public final class PhoneNumberFactory {
46 64
      * @return a new phone number object
47 65
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48 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
 }