#28 jpsp91 (Jae Park)

Aberto
jpsp91 quer mesclar 1 commits de jpsp91/CR-MicroLabs-ExceptionsAndLogging:master em master

+ 4
- 0
src/main/java/com/zipcodewilmington/phone/PhoneNumber.java Ver arquivo

@@ -8,6 +8,10 @@ import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
8 8
 public final class PhoneNumber {
9 9
     private final String phoneNumberString;
10 10
 
11
+
12
+
13
+
14
+
11 15
     // default constructor is uncallable
12 16
     private PhoneNumber() throws InvalidPhoneNumberFormatException {
13 17
         this(null);

+ 51
- 6
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Ver arquivo

@@ -1,15 +1,20 @@
1 1
 package com.zipcodewilmington.phone;
2 2
 
3 3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4
+import com.zipcodewilmington.tools.RandomNumberFactory;
4 5
 
6
+import javax.xml.soap.SOAPPart;
7
+import java.util.ArrayList;
5 8
 import java.util.logging.Logger;
6 9
 
7 10
 /**
8 11
  * Created by leon on 5/1/17.
9 12
  */
10
-public final class PhoneNumberFactory {
13
+public final class PhoneNumberFactory{
11 14
     private static final Logger logger = Logger.getGlobal();
12 15
 
16
+
17
+
13 18
     private PhoneNumberFactory() {
14 19
         /** This constructor is private
15 20
          *  This class is uninstantiable */
@@ -20,14 +25,43 @@ public final class PhoneNumberFactory {
20 25
      * @return array of randomly generated PhoneNumber objects
21 26
      */ //TODO - Implement logic
22 27
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
28
+
29
+        PhoneNumber[] phoneNumberList = new PhoneNumber[phoneNumberCount];
30
+
31
+        for(int i = 0; i < phoneNumberCount; i++){
32
+            phoneNumberList[i] = createRandomPhoneNumber();
33
+        }
34
+
35
+        return phoneNumberList;
24 36
     }
25 37
 
26 38
     /**
27 39
      * @return an instance of PhoneNumber with randomly generated phone number value
28 40
      */ //TODO - Implement logic
29 41
     public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
42
+
43
+        StringBuilder threeDigitsArea = new StringBuilder();
44
+        StringBuilder threeDigitsCentral = new StringBuilder();
45
+        StringBuilder fourDigits = new StringBuilder();
46
+
47
+        for(int i = 0; i < 3; i++){
48
+            threeDigitsArea.append(String.valueOf(RandomNumberFactory.createInteger(1,9)));
49
+        }
50
+
51
+        for(int i = 0; i < 3; i++){
52
+            threeDigitsCentral.append(String.valueOf(RandomNumberFactory.createInteger(1,9)));
53
+        }
54
+
55
+        for(int i = 0; i < 4; i++){
56
+            fourDigits.append(String.valueOf(RandomNumberFactory.createInteger(1,9)));
57
+        }
58
+
59
+        int threeA = Integer.valueOf(threeDigitsArea.toString());
60
+        int threeC = Integer.valueOf(threeDigitsCentral.toString());
61
+        int four = Integer.valueOf(fourDigits.toString());
62
+
63
+
64
+        return createPhoneNumberSafely(threeA, threeC, four);
31 65
     }
32 66
 
33 67
 
@@ -38,7 +72,17 @@ public final class PhoneNumberFactory {
38 72
      * @return a new phone number object
39 73
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40 74
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
41
-        return createPhoneNumber(null);
75
+
76
+        String returnValue;
77
+
78
+        try {
79
+            returnValue = "(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode;
80
+            return createPhoneNumber(returnValue);
81
+        }
82
+        catch (InvalidPhoneNumberFormatException e){
83
+            System.out.println("Attempting to create a new PhoneNumber object with a value of " + createRandomPhoneNumber());
84
+            return null;
85
+        }
42 86
     }
43 87
 
44 88
     /**
@@ -46,7 +90,8 @@ public final class PhoneNumberFactory {
46 90
      * @return a new phone number object
47 91
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48 92
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
93
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
94
+
95
+        return new PhoneNumber(phoneNumberString);
51 96
     }
52 97
 }