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
     //instance variable
12
     //instance variable
13
     private TreeMap<String, String> treeMap;
13
     private TreeMap<String, String> treeMap;
14
 
14
 
15
+
15
     // void "no arguments" constructor -creates empty map
16
     // void "no arguments" constructor -creates empty map
16
     public PhoneBook() {
17
     public PhoneBook() {
17
         this.treeMap= new TreeMap<String, String>();
18
         this.treeMap= new TreeMap<String, String>();
24
 
25
 
25
     //add a name & number entry
26
     //add a name & number entry
26
     public void add(String name, String number) {
27
     public void add(String name, String number) {
28
+
27
         if(!treeMap.containsKey(name)) {
29
         if(!treeMap.containsKey(name)) {
28
             treeMap.put(name, number);
30
             treeMap.put(name, number);
29
         }
31
         }
32
+
30
     }
33
     }
31
 
34
 
32
     //remove a name & number entry
35
     //remove a name & number entry
40
 
43
 
41
     //find phone number lookup by name
44
     //find phone number lookup by name
42
     public String lookup(String name) {
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
     //print out all of the entries in PhoneBook
50
     //print out all of the entries in PhoneBook

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

13
     public void testDefaultConstructor() {
13
     public void testDefaultConstructor() {
14
 
14
 
15
         PhoneBook book = new PhoneBook();
15
         PhoneBook book = new PhoneBook();
16
-        book.add("Robert", "302-555-1234");
17
         Assert.assertNotNull(book);
16
         Assert.assertNotNull(book);
18
-
19
-
20
-
21
     }
17
     }
18
+
22
     @Test
19
     @Test
23
     public void testConstructorWithArgument() {
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
     @Test
26
     @Test
28
     public void testAdd() {
27
     public void testAdd() {
28
+
29
+        //Given
29
         PhoneBook book = new PhoneBook();
30
         PhoneBook book = new PhoneBook();
31
+        String expectedAddition = "302-555-1234";
32
+
33
+        //When
30
         book.add("Bob", "302-555-1234");
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