Bladeren bron

Merge 566a944cc21e767dab4d2502b55a9c9bebe791a0 into 0665dd6f3de2bb92a7f856bde34efac87df0531f

ahsonali 8 jaren geleden
bovenliggende
commit
36e1d155f6
Geen account gekoppeld aan de committers e-mail

+ 3
- 1
src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java Bestand weergeven

@@ -3,5 +3,7 @@ package com.zipcodewilmington.exceptions;
3 3
 /**
4 4
  * Created by leon on 5/10/17.
5 5
  */ // Checked Exception
6
-public final class InvalidPhoneNumberFormatException extends Exception {
6
+public final class InvalidPhoneNumberFormatException extends Exception
7
+{
8
+
7 9
 }

+ 2
- 1
src/main/java/com/zipcodewilmington/phone/PhoneNumber.java Bestand weergeven

@@ -14,7 +14,8 @@ public final class PhoneNumber {
14 14
     }
15 15
 
16 16
     // non-default constructor is package-protected
17
-    protected PhoneNumber(String phoneNumber) throws InvalidPhoneNumberFormatException {
17
+    protected PhoneNumber(String phoneNumber) throws InvalidPhoneNumberFormatException
18
+    {
18 19
         //validate phone number with format `(###)-###-####`
19 20
         if (!phoneNumber.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}")) {
20 21
             throw new InvalidPhoneNumberFormatException();

+ 45
- 10
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Bestand weergeven

@@ -1,13 +1,15 @@
1 1
 package com.zipcodewilmington.phone;
2 2
 
3 3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4
+import com.zipcodewilmington.tools.RandomNumberFactory;
4 5
 
5
-import java.util.logging.Logger;
6 6
 
7
+import java.util.logging.Logger;
7 8
 /**
8 9
  * Created by leon on 5/1/17.
9 10
  */
10
-public final class PhoneNumberFactory {
11
+public final class PhoneNumberFactory
12
+{
11 13
     private static final Logger logger = Logger.getGlobal();
12 14
 
13 15
     private PhoneNumberFactory() {
@@ -19,15 +21,28 @@ public final class PhoneNumberFactory {
19 21
      * @param phoneNumberCount - number of PhoneNumber objects to instantiate
20 22
      * @return array of randomly generated PhoneNumber objects
21 23
      */ //TODO - Implement logic
22
-    public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
24
+    public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount)
25
+    {
26
+        //Initializing an array with the size of phoneNumberCount
27
+        PhoneNumber [] phoneNumberArray = new PhoneNumber[phoneNumberCount];
28
+
29
+        for(int i =0; i<phoneNumberCount; i++)
30
+        {
31
+            phoneNumberArray[i]=createRandomPhoneNumber();
32
+        }
33
+        return phoneNumberArray;
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
-    public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
39
+    public static PhoneNumber createRandomPhoneNumber()
40
+    {
41
+        int areaCode = RandomNumberFactory.createInteger(100,999);
42
+        int centralOfficeCode = RandomNumberFactory.createInteger(100,999);
43
+        int phoneLineCode = RandomNumberFactory.createInteger(1000,9999);
44
+
45
+        return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
31 46
     }
32 47
 
33 48
 
@@ -37,8 +52,26 @@ public final class PhoneNumberFactory {
37 52
      * @param phoneLineCode     - 4 digit code
38 53
      * @return a new phone number object
39 54
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40
-    public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
41
-        return createPhoneNumber(null);
55
+    public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode)
56
+    {
57
+        StringBuilder sb = new StringBuilder();
58
+        sb.append("("+areaCode+")" + "-" + centralOfficeCode + "-" + phoneLineCode);
59
+        String phoneNumberString = sb.toString();
60
+
61
+        try
62
+        {
63
+            return createPhoneNumber(phoneNumberString);
64
+        }
65
+
66
+        catch (InvalidPhoneNumberFormatException e)
67
+        {
68
+            logger.info(phoneNumberString + " is not a valid phone number entry");
69
+            return null;
70
+        }
71
+
72
+
73
+
74
+
42 75
     }
43 76
 
44 77
     /**
@@ -46,7 +79,9 @@ public final class PhoneNumberFactory {
46 79
      * @return a new phone number object
47 80
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48 81
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
82
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException
83
+    {
84
+        //logger.info("Attempting to create a new PhoneNumber object with a value of " + phoneNumberString);
85
+        return new PhoneNumber(phoneNumberString);
51 86
     }
52 87
 }

+ 4
- 2
src/main/java/com/zipcodewilmington/tools/RandomNumberFactory.java Bestand weergeven

@@ -10,12 +10,14 @@ public abstract class RandomNumberFactory {
10 10
     private static final Random random = new Random();
11 11
 
12 12
     /** @return a random float between the specified min and max numeric range */
13
-    public static Float createFloat(float min, float max) {
13
+    public static Float createFloat(float min, float max)
14
+    {
14 15
         return random.nextFloat() * (max - min) + min;
15 16
     }
16 17
 
17 18
     /** @return a random integer between the specified min and max numeric range */
18
-    public static Integer createInteger(Integer min, Integer max) {
19
+    public static Integer createInteger(Integer min, Integer max)
20
+    {
19 21
         return createFloat(min, max).intValue();
20 22
     }
21 23
 }