Browse Source

almost finished

Carolynn Vansant 6 years ago
parent
commit
9d58f8d8cf

+ 15
- 10
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

27
         }
27
         }
28
     }
28
     }
29
 
29
 
30
-    //add and remove numbers from existing names
31
-    //unfinished, no test cases yet
32
-    public void modifyNumbers(String name, ArrayList<String> number) {
30
+    //add numbers to existing entries
31
+    //no test cases yet
32
+    public void addNumberToEntry(String name, String number) {
33
         for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) {
33
         for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) {
34
-            ArrayList<String> value = entry.getValue();
35
-            for (String firstNumber : value) {
36
-
34
+            if (name.equals(entry.getKey())) {
35
+                ArrayList<String> value = entry.getValue();
36
+                value.add(number);
37
             }
37
             }
38
         }
38
         }
39
-
40
     }
39
     }
41
 
40
 
41
+    //remove numbers from existing entries
42
+    //no test cases yet
43
+    public void removeNumberFromEntry(String name, String number) {
44
+        for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) {
45
+            if (entry.getValue().contains(number)) {
46
+                entry.getValue().remove(number);
47
+            }
48
+        }
49
+    }
42
 
50
 
43
     //remove a name & number entry
51
     //remove a name & number entry
44
     public void removeEntry(String name) {
52
     public void removeEntry(String name) {
77
         return printOut.toString();
85
         return printOut.toString();
78
     }
86
     }
79
 
87
 
80
-
81
-
82
 }
88
 }
83
-

+ 24
- 2
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

29
     }
29
     }
30
 
30
 
31
     @Test
31
     @Test
32
-    public void testAdd() {
33
-
32
+    public void testInputEntry() {
34
         //Given
33
         //Given
35
         PhoneBook book = new PhoneBook();
34
         PhoneBook book = new PhoneBook();
36
 
35
 
37
         ArrayList<String> expectedAddition = new ArrayList<String>();
36
         ArrayList<String> expectedAddition = new ArrayList<String>();
38
         expectedAddition.add("302-555-2222");
37
         expectedAddition.add("302-555-2222");
39
         expectedAddition.add("302-555-3456");
38
         expectedAddition.add("302-555-3456");
39
+
40
         //When
40
         //When
41
         book.inputEntry("Bob", expectedAddition);
41
         book.inputEntry("Bob", expectedAddition);
42
         ArrayList<String> actualAddition = new ArrayList<String>(book.lookup("Bob"));
42
         ArrayList<String> actualAddition = new ArrayList<String>(book.lookup("Bob"));
43
 
43
 
44
         //Then
44
         //Then
45
         Assert.assertEquals(expectedAddition, actualAddition);
45
         Assert.assertEquals(expectedAddition, actualAddition);
46
+    }
47
+
48
+    @Test
49
+    public void addNumberToEntry() {
50
+        //Given
51
+        PhoneBook book = new PhoneBook();
52
+        ArrayList<String> bobNumber = new ArrayList<String>();
53
+        bobNumber.add("302-555-2222");
54
+        book.inputEntry("Bob", bobNumber);
55
+        book.display();
56
+        //Expected
57
+        ArrayList<String> expectedAddition = new ArrayList<String>();
58
+        expectedAddition.add("302-555-2222");
59
+        expectedAddition.add("302-555-1111");
60
+        //When
61
+        book.addNumberToEntry("Bob", "302-555-1111");
62
+        ArrayList<String> actualAddition = book.lookup("Bob");
63
+        //Then
64
+        Assert.assertEquals(expectedAddition, actualAddition);
46
 
65
 
47
     }
66
     }
48
 
67
 
68
+    //add test for removeNumberFromEntry
69
+
49
     @Test
70
     @Test
50
     public void testRemoveEntry() {
71
     public void testRemoveEntry() {
51
 
72
 
129
         String expectedName = "Bob";
150
         String expectedName = "Bob";
130
         ArrayList<String> bobNumber = new ArrayList<String>();
151
         ArrayList<String> bobNumber = new ArrayList<String>();
131
         bobNumber.add("302-555-2223");
152
         bobNumber.add("302-555-2223");
153
+        bobNumber.add("302-555-5555");
132
         book.inputEntry(expectedName, bobNumber);
154
         book.inputEntry(expectedName, bobNumber);
133
 
155
 
134
         //When;
156
         //When;