Просмотр исходного кода

Merge 8ae7904b20d7f5f1fe13e1847ed171f995fdc7e7 into 0665dd6f3de2bb92a7f856bde34efac87df0531f

Joshua 8 лет назад
Родитель
Сommit
e3651ef392
Аккаунт пользователя с таким Email не найден

+ 7
- 0
src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java Просмотреть файл

@@ -4,4 +4,11 @@ package com.zipcodewilmington.exceptions;
4 4
  * Created by leon on 5/10/17.
5 5
  */ // Checked Exception
6 6
 public final class InvalidPhoneNumberFormatException extends Exception {
7
+    public InvalidPhoneNumberFormatException(){
8
+        super();
9
+    }
10
+
11
+    public InvalidPhoneNumberFormatException(String message){
12
+        super(message);
13
+    }
7 14
 }

+ 1
- 1
src/main/java/com/zipcodewilmington/phone/PhoneNumber.java Просмотреть файл

@@ -17,7 +17,7 @@ public final class PhoneNumber {
17 17
     protected PhoneNumber(String phoneNumber) throws InvalidPhoneNumberFormatException {
18 18
         //validate phone number with format `(###)-###-####`
19 19
         if (!phoneNumber.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}")) {
20
-            throw new InvalidPhoneNumberFormatException();
20
+            throw new InvalidPhoneNumberFormatException("invalid phone number format");
21 21
         }
22 22
         this.phoneNumberString = phoneNumber;
23 23
     }

+ 38
- 5
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Просмотреть файл

@@ -1,7 +1,10 @@
1 1
 package com.zipcodewilmington.phone;
2 2
 
3
+import com.sun.javafx.binding.StringFormatter;
3 4
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
5
+import com.zipcodewilmington.tools.RandomNumberFactory;
4 6
 
7
+import java.util.Random;
5 8
 import java.util.logging.Logger;
6 9
 
7 10
 /**
@@ -20,14 +23,23 @@ public final class PhoneNumberFactory {
20 23
      * @return array of randomly generated PhoneNumber objects
21 24
      */ //TODO - Implement logic
22 25
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
26
+        PhoneNumber[] number = new PhoneNumber[phoneNumberCount];
27
+        for (int i = 0; i < number.length; i++) {
28
+            number[i] = createRandomPhoneNumber();
29
+        }
30
+        return number;
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
+
38
+        int areaCode = RandomNumberFactory.createInteger(100, 999);
39
+        int centralOfficeCode = RandomNumberFactory.createInteger(100, 999);
40
+        int phoneLine = RandomNumberFactory.createInteger(1000, 9999);
41
+
42
+        return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLine);
31 43
     }
32 44
 
33 45
 
@@ -38,7 +50,27 @@ public final class PhoneNumberFactory {
38 50
      * @return a new phone number object
39 51
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40 52
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
41
-        return createPhoneNumber(null);
53
+        //StringFormatter.format(%2)
54
+
55
+        StringBuilder sb = new StringBuilder();
56
+    //        sb.append("(");
57
+    //        sb.append(String.format("%02d", areaCode));
58
+    //        sb.append(")");
59
+    //        sb.append("-");
60
+    //        sb.append(String.format("%02d", centralOfficeCode));
61
+    //        sb.append("-");
62
+    //        sb.append(String.format("%03d", phoneLineCode));
63
+
64
+           sb.append("(" + areaCode + ")" + "-" + centralOfficeCode + "-" + phoneLineCode);
65
+        String s = sb.toString();
66
+        try {
67
+            return createPhoneNumber(s);
68
+        } catch (InvalidPhoneNumberFormatException e) {
69
+            logger.info("invalid phoneNumber");
70
+
71
+        }
72
+
73
+        return null;
42 74
     }
43 75
 
44 76
     /**
@@ -46,7 +78,8 @@ public final class PhoneNumberFactory {
46 78
      * @return a new phone number object
47 79
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48 80
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
81
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
82
+        PhoneNumber phoneNumber = new PhoneNumber(phoneNumberString);
83
+        return phoneNumber;
51 84
     }
52 85
 }

+ 1
- 1
src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java Просмотреть файл

@@ -83,7 +83,7 @@ public class PhoneNumberFactoryTest {
83 83
             // : Given
84 84
             // : When
85 85
             PhoneNumber phoneNumber = PhoneNumberFactory.createRandomPhoneNumber();
86
-
86
+            
87 87
             // : Then
88 88
             Assert.assertTrue(phoneNumber != null);
89 89
         }