Procházet zdrojové kódy

I actually understand the assignment now, more test cases passing

Mitch Taylor před 8 roky
rodič
revize
7586165ece

+ 15
- 2
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Zobrazit soubor

@@ -68,7 +68,20 @@ public final class PhoneNumberFactory {
68 68
      * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
69 69
      */ // TODO - Add throws statement to method signature
70 70
     public static PhoneNumber createPhoneNumber (String phoneNumberString) throws InvalidPhoneNumberFormatException {
71
-        logger.log(Level.FINE, ("Attempting to create a new PhoneNumber object with a value of " + phoneNumberString));
72
-        return new PhoneNumber(phoneNumberString);
71
+        StringBuilder sb = new StringBuilder();
72
+        try {
73
+            sb.append("(");
74
+            sb.append(phoneNumberString.substring(0, 3));
75
+            sb.append(")-");
76
+            sb.append(phoneNumberString.substring(3, 6));
77
+            sb.append("-");
78
+            sb.append(phoneNumberString.substring(6));
79
+            return new PhoneNumber(sb.toString());
80
+        } catch (InvalidPhoneNumberFormatException e) {
81
+            logger.log(Level.FINE, ("Attempting to create a new PhoneNumber object with a value of " + phoneNumberString));
82
+            return null;
83
+        }
73 84
     }
74 85
 }
86
+
87
+// 012-345-6789

+ 4
- 4
src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java Zobrazit soubor

@@ -23,15 +23,15 @@ public class PhoneNumberFactoryTest {
23 23
     @Test
24 24
     public void testCreatePhoneNumberSafely() throws InvalidPhoneNumberFormatException {
25 25
         // : Given
26
-        int areaCode = 0;
27
-        int centralOfficeCode = 0;
28
-        int phoneLineCode = 0;
26
+        int areaCode = 302;
27
+        int centralOfficeCode = 228;
28
+        int phoneLineCode = 5151;
29 29
 
30 30
         // : When
31 31
         PhoneNumber phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
32 32
 
33 33
         // : Then
34
-        Assert.assertEquals(null, phoneNumber);
34
+        Assert.assertEquals("(302)-228-5151", phoneNumber.toString());
35 35
     }
36 36
 
37 37
     @Test