Jason Gibbs 8 лет назад
Родитель
Сommit
409126289d

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

1
+package com.zipcodewilmington.phone;
2
+
3
+public class Main {
4
+    public static void main(String[] args) {
5
+        System.out.println(PhoneNumberFactory.createRandomPhoneNumber());
6
+    }
7
+}

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

3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4
 
4
 
5
 import java.util.logging.Logger;
5
 import java.util.logging.Logger;
6
+import java.util.Random;
6
 
7
 
7
 /**
8
 /**
8
  * Created by leon on 5/1/17.
9
  * Created by leon on 5/1/17.
20
      * @return array of randomly generated PhoneNumber objects
21
      * @return array of randomly generated PhoneNumber objects
21
      */ //TODO - Implement logic
22
      */ //TODO - Implement logic
22
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
     public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
24
+        PhoneNumber[] randomPhoneNumberArray = new PhoneNumber[phoneNumberCount];
25
+        for (int i = 0; i < phoneNumberCount; i++) {
26
+            randomPhoneNumberArray[i] = createRandomPhoneNumber();
27
+        }
28
+        return randomPhoneNumberArray;
24
     }
29
     }
25
-
26
     /**
30
     /**
27
      * @return an instance of PhoneNumber with randomly generated phone number value
31
      * @return an instance of PhoneNumber with randomly generated phone number value
28
      */ //TODO - Implement logic
32
      */ //TODO - Implement logic
29
     public static PhoneNumber createRandomPhoneNumber() {
33
     public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
34
+        Random rand = new Random();
35
+        int areaCode = rand.nextInt(899) + 100;
36
+        int centralOfficeCode = rand.nextInt(899) + 100;
37
+        int phoneLineCode = rand.nextInt(8999) + 1000;
38
+        return createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
31
     }
39
     }
32
 
40
 
33
-
34
     /**
41
     /**
35
      * @param areaCode          - 3 digit code
42
      * @param areaCode          - 3 digit code
36
      * @param centralOfficeCode - 3 digit code
43
      * @param centralOfficeCode - 3 digit code
38
      * @return a new phone number object
45
      * @return a new phone number object
39
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
46
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
47
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
41
-        return createPhoneNumber(null);
48
+        String phoneNumberRepresentation = "(" + areaCode + ")-" + centralOfficeCode + "-" + phoneLineCode;
49
+        logger.info("Create phonenumber");
50
+        try {
51
+            return createPhoneNumber(phoneNumberRepresentation);
52
+        } catch (InvalidPhoneNumberFormatException e) {
53
+            logger.warning("" +  phoneNumberRepresentation + " is not a valid phone number" );
54
+            return null;
55
+        }
42
     }
56
     }
43
 
57
 
44
     /**
58
     /**
46
      * @return a new phone number object
60
      * @return a new phone number object
47
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
61
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
48
      */ // TODO - Add throws statement to method signature
62
      */ // TODO - Add throws statement to method signature
49
-    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
50
-        return null;
63
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) throws InvalidPhoneNumberFormatException {
64
+        logger.info("Attempting to create a new phoneNumber object with a value of " + phoneNumberString);
65
+        return new PhoneNumber(phoneNumberString);
51
     }
66
     }
52
 }
67
 }