Ahmad Rusdi před 8 roky
rodič
revize
f1a967394d

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

@@ -2,6 +2,7 @@ package com.zipcodewilmington.phone;
2 2
 
3 3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4 4
 
5
+import java.util.logging.Level;
5 6
 import java.util.logging.Logger;
6 7
 
7 8
 /**
@@ -20,14 +21,38 @@ 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
+
26
+        for (int i = 0; i < phoneNumberCount; i++) {
27
+            try {
28
+                phoneNumbers[i] = createRandomPhoneNumber();
29
+            } catch (Exception e) {
30
+
31
+            }
32
+        }
33
+        return phoneNumbers;
24 34
     }
25 35
 
26 36
     /**
27 37
      * @return an instance of PhoneNumber with randomly generated phone number value
28 38
      */ //TODO - Implement logic
29 39
     public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
40
+        StringBuilder sb = new StringBuilder("(");
41
+        for (int j = 0; j < 12; j++) {
42
+            if (j == 3) {
43
+                sb.append(")-");
44
+                continue;
45
+            } if (j == 7) {
46
+                sb.append("-");
47
+                continue;
48
+            }
49
+            sb.append((int) (Math.random() * 9));
50
+        }
51
+        try {
52
+            return new PhoneNumber(sb.toString());
53
+        } catch (Exception e) {
54
+            return null;
55
+        }
31 56
     }
32 57
 
33 58
 
@@ -38,7 +63,12 @@ public final class PhoneNumberFactory {
38 63
      * @return a new phone number object
39 64
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40 65
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
41
-        return createPhoneNumber(null);
66
+        try {
67
+            logger.log(Level.FINE, "Attempting to create a new PhoneNumber object with a value of");
68
+            return createPhoneNumber("(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode);
69
+        } catch (Exception e) {
70
+            return null;
71
+        }
42 72
     }
43 73
 
44 74
     /**
@@ -46,7 +76,7 @@ public final class PhoneNumberFactory {
46 76
      * @return a new phone number object
47 77
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48 78
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
79
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
80
+        return new PhoneNumber(phoneNumberString);
51 81
     }
52 82
 }

+ 2
- 0
src/main/java/com/zipcodewilmington/tools/RandomNumberFactory.java Zobrazit soubor

@@ -1,4 +1,6 @@
1 1
 package com.zipcodewilmington.tools;
2
+import com.zipcodewilmington.phone.PhoneNumber;
3
+
2 4
 import java.awt.*;
3 5
 import java.util.*;
4 6