Jennifer Chao 6 年前
父节点
当前提交
89839791ad
共有 1 个文件被更改,包括 21 次插入5 次删除
  1. 21
    5
      src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java

+ 21
- 5
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java 查看文件

@@ -2,6 +2,7 @@ package com.zipcodewilmington.phone;
2 2
 
3 3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4 4
 
5
+import java.util.Random;
5 6
 import java.util.logging.Logger;
6 7
 
7 8
 /**
@@ -20,14 +21,22 @@ 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[] phoneNumbers = new PhoneNumber[phoneNumberCount];
25
+        for (int i = 0; i < phoneNumberCount; i++) {
26
+            phoneNumbers[i] = createRandomPhoneNumber();
27
+        }
28
+        return phoneNumbers;
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
+        Random random = new Random();
36
+        int areaCode = random.nextInt(101) + 899;
37
+        int centralOfficeCode = random.nextInt(101) + 899;
38
+        int phoneLineCode = random.nextInt(1001) + 8999;
39
+        return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
31 40
     }
32 41
 
33 42
 
@@ -38,7 +47,13 @@ public final class PhoneNumberFactory {
38 47
      * @return a new phone number object
39 48
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40 49
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
41
-        return createPhoneNumber(null);
50
+        String randomNumber = String.join("-", "(" + Integer.toString(areaCode) + ")", Integer.toString(centralOfficeCode), Integer.toString(phoneLineCode));
51
+        try {
52
+            return createPhoneNumber(randomNumber);
53
+        } catch (InvalidPhoneNumberFormatException exception) {
54
+            logger.info(randomNumber + " is not a valid phone number");
55
+            return null;
56
+        }
42 57
     }
43 58
 
44 59
     /**
@@ -46,7 +61,8 @@ public final class PhoneNumberFactory {
46 61
      * @return a new phone number object
47 62
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48 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("Attempting to create a new PhoneNumber object with a value of " + phoneNumberString);
66
+        return new PhoneNumber(phoneNumberString);
51 67
     }
52 68
 }