Yesoda Sanka 7 yıl önce
ebeveyn
işleme
3f787a76f8

+ 1
- 1
README.md Dosyayı Görüntüle

@@ -52,7 +52,7 @@ String phoneLineCode = phoneNumber.getPhoneLineCode();
52 52
 	* where `(###)-###-####` will be replaced with the respective input parameter.
53 53
 
54 54
 * Add logging to the `createPhoneNumberSafely` method from `Part 2`, which logs the message
55
-	* `(###)-###-#### is not a valid phone number`
55
+	* `"Attempting to create a new PhoneNumber object with a value of (###)-###-####
56 56
 	* Where `(###)-###-####` will be replaced with the respective input parameter.
57 57
 
58 58
 	

+ 11
- 3
src/main/java/com/zipcodewilmington/phone/PhoneNumber.java Dosyayı Görüntüle

@@ -4,14 +4,22 @@ import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4 4
 
5 5
 /**
6 6
  * Created by leon on 5/10/17.
7
+ * Upon instantiating a new `PhoneNumber` object, it is possible to
8
+ * receive a `InvalidPhoneNumberFormatException` if the `String` passed
9
+ * into the `PhoneNumber` constructor does not fit the format `(###)-###-####`.<br>
10
+ * * `InvalidPhoneNumberFormatException` extends `IOException`, which is a `checked exception`.<br>
11
+ * * Modify the `createPhoneNumber` method so that it throws any resulting `InvalidPhoneNumberFormatException`.
12
+ * 	* This will ensure that any method calling `createPhoneNumber` will have to handle the exception.
7 13
  */
8 14
 public final class PhoneNumber {
9 15
     private final String phoneNumberString;
10 16
 
17
+
11 18
     // default constructor is uncallable
12
-    private PhoneNumber() throws InvalidPhoneNumberFormatException {
13
-        this(null);
14
-    }
19
+  private PhoneNumber() throws InvalidPhoneNumberFormatException {
20
+
21
+         this(null);
22
+     }
15 23
 
16 24
     // non-default constructor is package-protected
17 25
     protected PhoneNumber(String phoneNumber) throws InvalidPhoneNumberFormatException {

+ 32
- 7
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Dosyayı Görüntüle

@@ -2,6 +2,7 @@ package com.zipcodewilmington.phone;
2 2
 
3 3
 import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4 4
 
5
+import java.util.Random;
5 6
 import java.util.logging.Logger;
6 7
 
7 8
 /**
@@ -19,15 +20,27 @@ public final class PhoneNumberFactory {
19 20
      * @param phoneNumberCount - number of PhoneNumber objects to instantiate
20 21
      * @return array of randomly generated PhoneNumber objects
21 22
      */ //TODO - Implement logic
22
-    public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
23
-        return null;
23
+    public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) throws InvalidPhoneNumberFormatException{
24
+
25
+    PhoneNumber[]  phone=new PhoneNumber[phoneNumberCount];
26
+    for(int i=0;i<phoneNumberCount;i++ )
27
+    {
28
+        phone[i]=createRandomPhoneNumber();
24 29
     }
25 30
 
31
+        return phone;
32
+    }
26 33
     /**
27 34
      * @return an instance of PhoneNumber with randomly generated phone number value
28 35
      */ //TODO - Implement logic
29
-    public static PhoneNumber createRandomPhoneNumber() {
30
-        return createPhoneNumberSafely(-1, -1, -1);
36
+    public static PhoneNumber createRandomPhoneNumber()  {
37
+        Random random=new Random();
38
+        int areaCode=random.nextInt(900 ) +100;
39
+        int centralOfficeCode=random.nextInt(900)+100;
40
+        int phoneLineCode=random.nextInt(8990)+1000;
41
+        System.out.println("areacode" +areaCode+"\tcentralOfficeCode:"+ centralOfficeCode +"\t phoneLineCode"+phoneLineCode);
42
+
43
+        return createPhoneNumberSafely(areaCode , centralOfficeCode ,phoneLineCode );
31 44
     }
32 45
 
33 46
 
@@ -38,15 +51,27 @@ public final class PhoneNumberFactory {
38 51
      * @return a new phone number object
39 52
      */ //TODO - if input is valid, return respective PhoneNumber object, else return null
40 53
     public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
41
-        return createPhoneNumber(null);
54
+        String phoneNumber="(" + areaCode + ")-" +centralOfficeCode +"-"+phoneLineCode;
55
+        try {
56
+            PhoneNumber phoneNumber1 = new PhoneNumber(phoneNumber);
57
+            return phoneNumber1;
58
+
59
+        } catch( InvalidPhoneNumberFormatException e)
60
+         {
61
+         logger.warning("invalid phone number" ) ;
62
+         return  null;
42 63
     }
64
+}
43 65
 
44 66
     /**
45 67
      * @param phoneNumberString - some String corresponding to a phone number whose format is `(###)-###-####`
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
+        //phoneNumberString ="302-312-5555";
73
+        PhoneNumber phone=new PhoneNumber(phoneNumberString );
74
+        logger.info("Attempting to create a new PhoneNumber object with a value of " + phone);
75
+        return   phone;
51 76
     }
52 77
 }