chitraB преди 6 години
родител
ревизия
bb923b3194
променени са 1 файла, в които са добавени 31 реда и са изтрити 6 реда
  1. 31
    6
      src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java

+ 31
- 6
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Целия файл

@@ -1,7 +1,11 @@
1 1
 package com.zipcodewilmington.phone;
2 2
 
3 3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4
+import com.zipcodewilmington.tools.RandomNumberFactory;
5
+import sun.rmi.runtime.Log;
4 6
 
7
+import java.util.Random;
8
+import java.util.logging.Level;
5 9
 import java.util.logging.Logger;
6 10
 
7 11
 /**
@@ -20,14 +24,21 @@ public final class PhoneNumberFactory {
20 24
      * @return array of randomly generated PhoneNumber objects
21 25
      */ //TODO - Implement logic
22 26
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
27
+        PhoneNumber[] phoneNumbers = new PhoneNumber[phoneNumberCount];
28
+        for(int i=0; i<phoneNumbers.length; i++){
29
+            phoneNumbers[i] = createRandomPhoneNumber();
30
+        }
31
+        return phoneNumbers;
24 32
     }
25 33
 
26 34
     /**
27 35
      * @return an instance of PhoneNumber with randomly generated phone number value
28 36
      */ //TODO - Implement logic
29 37
     public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
38
+        int areaCode = RandomNumberFactory.createInteger(100,999);
39
+        int centralOfficeCode = RandomNumberFactory.createInteger(100,999);
40
+        int phoneLineCode = RandomNumberFactory.createInteger(1000,9999);
41
+        return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
31 42
     }
32 43
 
33 44
 
@@ -38,7 +49,18 @@ public final class PhoneNumberFactory {
38 49
      * @return a new phone number object
39 50
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40 51
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
41
-        return createPhoneNumber(null);
52
+
53
+        String num = "(" + areaCode + ")" + "-" + centralOfficeCode + "-" + phoneLineCode;
54
+        PhoneNumber phoneNumber;
55
+
56
+        try {
57
+             phoneNumber = new PhoneNumber(num);
58
+    }
59
+       catch (InvalidPhoneNumberFormatException e){
60
+          logger.log(Level.WARNING, num +  " is not a valid phone number");
61
+        return null;
62
+       }
63
+        return phoneNumber;
42 64
     }
43 65
 
44 66
     /**
@@ -46,7 +68,10 @@ public final class PhoneNumberFactory {
46 68
      * @return a new phone number object
47 69
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48 70
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
71
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
72
+
73
+        logger.log(Level.INFO, "Attempting to create a new PhoneNumber object with a value of " + phoneNumberString);
74
+        return new PhoneNumber(phoneNumberString);
51 75
     }
52
-}
76
+
77
+}