Quellcode durchsuchen

Part Three Commit

Carolynn Vansant vor 6 Jahren
Ursprung
Commit
39681ea97b

+ 4
- 2
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Datei anzeigen

@@ -42,8 +42,10 @@ public class PhoneBook {
42 42
     //no test cases yet
43 43
     public void removeNumberFromEntry(String name, String number) {
44 44
         for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) {
45
-            if (entry.getValue().contains(number)) {
46
-                entry.getValue().remove(number);
45
+            if (entry.getKey().equals(name)) {
46
+                if (entry.getValue().contains(number)) {
47
+                    entry.getValue().remove(number);
48
+                }
47 49
             }
48 50
         }
49 51
     }

+ 30
- 43
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Datei anzeigen

@@ -13,7 +13,6 @@ import java.util.TreeMap;
13 13
 public class PhoneBookTest {
14 14
     @Test
15 15
     public void testDefaultConstructor() {
16
-
17 16
         PhoneBook book = new PhoneBook();
18 17
         Assert.assertNotNull(book);
19 18
     }
@@ -50,92 +49,83 @@ public class PhoneBookTest {
50 49
         //Given
51 50
         PhoneBook book = new PhoneBook();
52 51
         ArrayList<String> bobNumber = new ArrayList<String>();
53
-        bobNumber.add("302-555-2222");
52
+        bobNumber.add("302-555-2234");
54 53
         book.inputEntry("Bob", bobNumber);
55
-        book.display();
56
-        //Expected
57 54
         ArrayList<String> expectedAddition = new ArrayList<String>();
58
-        expectedAddition.add("302-555-2222");
55
+        expectedAddition.add("302-555-2234");
59 56
         expectedAddition.add("302-555-1111");
60 57
         //When
61 58
         book.addNumberToEntry("Bob", "302-555-1111");
62 59
         ArrayList<String> actualAddition = book.lookup("Bob");
63 60
         //Then
64 61
         Assert.assertEquals(expectedAddition, actualAddition);
65
-
66 62
     }
67 63
 
68
-    //add test for removeNumberFromEntry
69
-
70 64
     @Test
71
-    public void testRemoveEntry() {
72
-
65
+    public void removeNumberFromEntry() {
73 66
         //Given
74 67
         PhoneBook book = new PhoneBook();
75 68
         ArrayList<String> bobNumber = new ArrayList<String>();
76
-        bobNumber.add("302-555-2222");
77
-        bobNumber.add("302-555-3456");
69
+        bobNumber.add("302-555-2234");
70
+        bobNumber.add("302-555-1111");
78 71
         book.inputEntry("Bob", bobNumber);
79
-        String expectedValue = "";
80 72
 
81
-        //When
82
-        book.removeEntry("Bob");
83
-        String actualValue = book.display();
73
+        ArrayList<String> expectedAfterDeletion = new ArrayList<String>();
74
+        expectedAfterDeletion.add("302-555-2234");
84 75
 
76
+        //When
77
+        book.removeNumberFromEntry("Bob", "302-555-1111");
78
+        ArrayList<String> actualAfterDeletion = book.lookup("Bob");
85 79
         //Then
86
-        Assert.assertEquals(expectedValue, actualValue);
80
+        Assert.assertEquals(expectedAfterDeletion, actualAfterDeletion);
87 81
     }
88 82
 
89 83
     @Test
90
-    public void testRemoveEntry2() {
84
+    public void testRemoveEntry() {
91 85
 
92 86
         //Given
93 87
         PhoneBook book = new PhoneBook();
94
-
95
-        ArrayList<String> expectedValue = new ArrayList<String>();
96
-        expectedValue.add("302-555-2222");
97
-        expectedValue.add("302-555-3456");
98
-        book.inputEntry("Bob", expectedValue);
88
+        ArrayList<String> bobNumber = new ArrayList<String>();
89
+        bobNumber.add("302-555-2222");
90
+        bobNumber.add("302-555-3456");
91
+        book.inputEntry("Bob", bobNumber);
92
+        String expectedValue = "";
99 93
 
100 94
         //When
101
-        book.removeEntry("Robert");
95
+        book.removeEntry("Bob");
102 96
         String actualValue = book.display();
103 97
 
104 98
         //Then
105
-        Assert.assertNotEquals(expectedValue, actualValue);
99
+        Assert.assertEquals(expectedValue, actualValue);
106 100
     }
107 101
 
108 102
     @Test
109 103
     public void testLookup(){
104
+        //To test if lookup returns all of the person's phone numbers
110 105
         //Given
111 106
         PhoneBook book = new PhoneBook();
112
-
113 107
         ArrayList<String> expectedNumber = new ArrayList<String>();
114 108
         expectedNumber.add("302-555-2223");
115 109
         expectedNumber.add("302-555-3456");
116 110
         book.inputEntry("Bob", expectedNumber);
117
-
118 111
         //When
119 112
         ArrayList<String> actualNumber = new ArrayList<String>(book.lookup("Bob"));
120
-
121 113
         //Then
122 114
         Assert.assertEquals(expectedNumber, actualNumber);
123 115
     }
124 116
 
125 117
     @Test
126
-    public void testLookup3(){
127
-
118
+    public void testLookup2(){
119
+        //To test if lookup picks out the right person when there are multiple entries
128 120
         //Given
129 121
         PhoneBook book = new PhoneBook();
130
-
131 122
         ArrayList<String> bobNumber = new ArrayList<String>();
132 123
         bobNumber.add("302-555-2223");
133 124
         book.inputEntry("Bob", bobNumber);
134
-
125
+        //Expected
135 126
         ArrayList<String> expectedNumber = new ArrayList<String>();
136 127
         expectedNumber.add("302-555-9988");
137 128
         book.inputEntry("Frank", expectedNumber);
138
-
139 129
         //When
140 130
         ArrayList<String> actualNumber = new ArrayList<String> (book.lookup("Frank"));
141 131
         //Then
@@ -144,18 +134,16 @@ public class PhoneBookTest {
144 134
 
145 135
     @Test
146 136
     public void testreverseLookup(){
147
-
137
+        // to test if right person is returned when searching by one of multiple phone numbers
148 138
         //Given
149 139
         PhoneBook book = new PhoneBook();
150
-        String expectedName = "Bob";
151 140
         ArrayList<String> bobNumber = new ArrayList<String>();
152 141
         bobNumber.add("302-555-2223");
153 142
         bobNumber.add("302-555-5555");
143
+        String expectedName = "Bob";
154 144
         book.inputEntry(expectedName, bobNumber);
155
-
156
-        //When;
145
+        //When
157 146
         String actualName = book.reverseLookup("302-555-2223");
158
-
159 147
         //Then
160 148
         Assert.assertEquals(expectedName, actualName);
161 149
     }
@@ -165,22 +153,21 @@ public class PhoneBookTest {
165 153
     public void testDisplay() {
166 154
         //Given:
167 155
         PhoneBook book = new PhoneBook();
156
+
168 157
         ArrayList<String> bobNumber = new ArrayList<String>();
169 158
         bobNumber.add("302-555-1234");
170 159
         bobNumber.add("302-556-1245");
160
+        book.inputEntry("Bob", bobNumber);
161
+
171 162
         ArrayList<String> frankNumber = new ArrayList<String>();
172 163
         frankNumber.add("777-555-1111");
173
-        book.inputEntry("Bob", bobNumber);
174 164
         book.inputEntry("Frank", frankNumber);
175
-
165
+        //Expected
176 166
         String expectedNameNumber = "Bob " + bobNumber.toString() + "\n" + "Frank " + frankNumber.toString() + "\n";
177
-
178 167
         //When
179 168
         String actualNameNumber = book.display();
180 169
         //Then
181 170
         Assert.assertEquals(expectedNameNumber, actualNameNumber);
182
-
183 171
     }
184 172
 
185
-
186 173
 }