Browse Source

first draft completed but still need extra test cases

Carolynn Vansant 6 years ago
parent
commit
692b86e553

+ 5
- 7
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -12,6 +12,7 @@ public class PhoneBook {
12 12
     //instance variable
13 13
     private TreeMap<String, String> treeMap;
14 14
 
15
+
15 16
     // void "no arguments" constructor -creates empty map
16 17
     public PhoneBook() {
17 18
         this.treeMap= new TreeMap<String, String>();
@@ -24,9 +25,11 @@ public class PhoneBook {
24 25
 
25 26
     //add a name & number entry
26 27
     public void add(String name, String number) {
28
+
27 29
         if(!treeMap.containsKey(name)) {
28 30
             treeMap.put(name, number);
29 31
         }
32
+
30 33
     }
31 34
 
32 35
     //remove a name & number entry
@@ -40,13 +43,8 @@ public class PhoneBook {
40 43
 
41 44
     //find phone number lookup by name
42 45
     public String lookup(String name) {
43
-        String look = "";
44
-        if (treeMap.containsKey(name)) {
45
-            look = treeMap.get(name);
46
-        } else {
47
-            look = name + " is not in database";
48
-        }
49
-        return look;
46
+        String number = treeMap.get(name);
47
+        return number;
50 48
     }
51 49
 
52 50
     //print out all of the entries in PhoneBook

+ 12
- 5
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

@@ -13,22 +13,29 @@ public class PhoneBookTest {
13 13
     public void testDefaultConstructor() {
14 14
 
15 15
         PhoneBook book = new PhoneBook();
16
-        book.add("Robert", "302-555-1234");
17 16
         Assert.assertNotNull(book);
18
-
19
-
20
-
21 17
     }
18
+
22 19
     @Test
23 20
     public void testConstructorWithArgument() {
24
-
21
+        PhoneBook book = new PhoneBook();
22
+        book.add("Bob", "302-555-1234");
23
+        Assert.assertNotNull(book);
25 24
     }
26 25
 
27 26
     @Test
28 27
     public void testAdd() {
28
+
29
+        //Given
29 30
         PhoneBook book = new PhoneBook();
31
+        String expectedAddition = "302-555-1234";
32
+
33
+        //When
30 34
         book.add("Bob", "302-555-1234");
35
+        String actualAddition = book.lookup("Bob");
31 36
 
37
+        //Then
38
+        Assert.assertEquals(expectedAddition, actualAddition);
32 39
 
33 40
     }
34 41