|
|
@@ -2,6 +2,7 @@ package com.zipcodewilmington.phone;
|
|
2
|
2
|
|
|
3
|
3
|
import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
|
|
4
|
4
|
|
|
|
5
|
+import java.util.logging.Level;
|
|
5
|
6
|
import java.util.logging.Logger;
|
|
6
|
7
|
|
|
7
|
8
|
/**
|
|
|
@@ -20,14 +21,25 @@ public final class PhoneNumberFactory {
|
|
20
|
21
|
* @return array of randomly generated PhoneNumber objects
|
|
21
|
22
|
*/ //TODO - Implement logic
|
|
22
|
23
|
public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
|
|
23
|
|
- return null;
|
|
|
24
|
+ PhoneNumber[] phoneNumberArray=new PhoneNumber[phoneNumberCount-1];
|
|
|
25
|
+ for(int i=0;i<phoneNumberCount;i++)
|
|
|
26
|
+ {
|
|
|
27
|
+ phoneNumberArray[i]=createRandomPhoneNumber();
|
|
|
28
|
+
|
|
|
29
|
+ }
|
|
|
30
|
+
|
|
|
31
|
+ return phoneNumberArray;
|
|
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=(int)(Math.random()*900+100);
|
|
|
39
|
+ int centralOfficeCode=(int)(Math.random()*900+100);
|
|
|
40
|
+ int phoneLineCode=(int)(Math.random()*9000+1000);
|
|
|
41
|
+
|
|
|
42
|
+ return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
|
|
31
|
43
|
}
|
|
32
|
44
|
|
|
33
|
45
|
|
|
|
@@ -38,7 +50,22 @@ 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
|
+ StringBuilder br=new StringBuilder();
|
|
|
54
|
+ br.append('(').append(areaCode).append(')').append("-").append(centralOfficeCode).append("-").append(phoneLineCode);
|
|
|
55
|
+ String number=br.toString();
|
|
|
56
|
+ PhoneNumber ph;
|
|
|
57
|
+ try {
|
|
|
58
|
+ ph = new PhoneNumber(number);
|
|
|
59
|
+ }
|
|
|
60
|
+ catch(Exception e)
|
|
|
61
|
+ {
|
|
|
62
|
+ logger.log(Level.SEVERE,"Attempting to create a new PhoneNumber object with a value of "+
|
|
|
63
|
+ br.toString());
|
|
|
64
|
+ return null;
|
|
|
65
|
+
|
|
|
66
|
+ }
|
|
|
67
|
+
|
|
|
68
|
+ return ph;
|
|
42
|
69
|
}
|
|
43
|
70
|
|
|
44
|
71
|
/**
|
|
|
@@ -46,7 +73,11 @@ public final class PhoneNumberFactory {
|
|
46
|
73
|
* @return a new phone number object
|
|
47
|
74
|
* @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
|
|
48
|
75
|
*/ // TODO - Add throws statement to method signature
|
|
49
|
|
- public static PhoneNumber createPhoneNumber(String phoneNumberString) {
|
|
50
|
|
- return null;
|
|
|
76
|
+ public static PhoneNumber createPhoneNumber(String phoneNumberString)throws InvalidPhoneNumberFormatException{
|
|
|
77
|
+
|
|
|
78
|
+ logger.log(Level.SEVERE,"Attempting to create a new PhoneNumber object with a value of %s",
|
|
|
79
|
+ phoneNumberString);
|
|
|
80
|
+
|
|
|
81
|
+ return new PhoneNumber(phoneNumberString);
|
|
51
|
82
|
}
|
|
52
|
83
|
}
|