donnaj před 6 roky
rodič
revize
b482e52b88

+ 20
- 5
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Zobrazit soubor

@@ -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,21 @@ 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[] phoneBook = new PhoneNumber[phoneNumberCount];
25
+        for(int i = 1; i <= phoneNumberCount; i++) {
26
+            phoneBook[i-1] = createRandomPhoneNumber();
27
+        }
28
+        return phoneBook;
24 29
     }
25 30
 
26 31
     /**
27 32
      * @return an instance of PhoneNumber with randomly generated phone number value
28 33
      */ //TODO - Implement logic
29 34
     public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
35
+        int areaCd = RandomNumberFactory.createInteger(100,999);
36
+        int centralOCd = RandomNumberFactory.createInteger(100, 999);
37
+        int phoneLineCd = RandomNumberFactory.createInteger(100, 999);
38
+        return createPhoneNumberSafely(areaCd, centralOCd, phoneLineCd);
31 39
     }
32 40
 
33 41
 
@@ -38,7 +46,12 @@ public final class PhoneNumberFactory {
38 46
      * @return a new phone number object
39 47
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40 48
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
41
-        return createPhoneNumber(null);
49
+        String phoneNumberStr = "(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode;
50
+        try {return createPhoneNumber(phoneNumberStr);}
51
+        catch(InvalidPhoneNumberFormatException e) {
52
+            logger.info(phoneNumberStr + "is not a valid phone number");
53
+            return null;
54
+        }
42 55
     }
43 56
 
44 57
     /**
@@ -46,7 +59,9 @@ public final class PhoneNumberFactory {
46 59
      * @return a new phone number object
47 60
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48 61
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
62
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
63
+        PhoneNumber phoneNum = new PhoneNumber(phoneNumberString);
64
+        logger.info("Attempting to create a new PhoneNumber object with a value of " + phoneNumberString);
65
+        return phoneNum;
51 66
     }
52 67
 }