Bladeren bron

committing assignment

chitraB 6 jaren geleden
bovenliggende
commit
bb923b3194
1 gewijzigde bestanden met toevoegingen van 31 en 6 verwijderingen
  1. 31
    6
      src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java

+ 31
- 6
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Bestand weergeven

1
 package com.zipcodewilmington.phone;
1
 package com.zipcodewilmington.phone;
2
 
2
 
3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
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
 import java.util.logging.Logger;
9
 import java.util.logging.Logger;
6
 
10
 
7
 /**
11
 /**
20
      * @return array of randomly generated PhoneNumber objects
24
      * @return array of randomly generated PhoneNumber objects
21
      */ //TODO - Implement logic
25
      */ //TODO - Implement logic
22
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
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
      * @return an instance of PhoneNumber with randomly generated phone number value
35
      * @return an instance of PhoneNumber with randomly generated phone number value
28
      */ //TODO - Implement logic
36
      */ //TODO - Implement logic
29
     public static PhoneNumber createRandomPhoneNumber() {
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
      * @return a new phone number object
49
      * @return a new phone number object
39
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
50
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
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
      * @return a new phone number object
68
      * @return a new phone number object
47
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
69
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48
      */ // TODO - Add throws statement to method signature
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
+}