Browse Source

almost finished draft

Carolynn Vansant 6 years ago
parent
commit
d0451b34e7

+ 19
- 8
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -30,29 +30,40 @@ public class PhoneBook {
30 30
     }
31 31
 
32 32
     //remove a name & number entry
33
-    public void remove(String name) {
33
+    public String remove(String name) {
34
+        String removeName = name + " " + treeMap.get(name) + " has been removed";
34 35
         if(treeMap.containsKey(name)) {
35 36
             treeMap.remove(name);
36 37
         }
38
+        return removeName;
37 39
     }
38 40
 
39 41
     //find phone number lookup by name
40
-    public void lookup(String name) {
41
-        if(treeMap.containsKey(name)) {
42
-            treeMap.get(name);
42
+    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";
43 48
         }
44
-
49
+        return look;
45 50
     }
46 51
 
47 52
     //print out all of the entries in PhoneBook
48 53
     //need to work on code for formatting
54
+
49 55
     public String display(){
50
-        String printOut = "";
56
+        StringBuilder printOut = new StringBuilder();
51 57
         for(Map.Entry<String, String> entry: treeMap.entrySet()) {
52
-            printOut += entry.getKey() + " " + entry.getValue() + "\n";
58
+            printOut
59
+                    .append(entry.getKey())
60
+                    .append(" ")
61
+                    .append(entry.getValue())
62
+                    .append("\n");
53 63
         }
64
+        String print = printOut.toString();
54 65
         System.out.println(printOut);
55
-        return printOut;
66
+        return print;
56 67
     }
57 68
 
58 69
 

+ 30
- 10
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

@@ -9,48 +9,68 @@ import java.util.TreeMap;
9 9
  * Created by leon on 1/23/18.
10 10
  */
11 11
 public class PhoneBookTest {
12
-
12
+    @Test
13 13
     public void testDefaultConstructor() {
14 14
 
15 15
         PhoneBook book = new PhoneBook();
16
+        book.add("Robert", "302-555-1234");
17
+        Assert.assertNotNull(book);
16 18
 
17 19
 
18 20
 
19 21
     }
20
-
22
+    @Test
21 23
     public void testConstructorWithArgument() {
22 24
 
23 25
     }
24 26
 
27
+    @Test
25 28
     public void testAdd() {
26 29
         PhoneBook book = new PhoneBook();
27 30
         book.add("Bob", "302-555-1234");
28
-        Assert.assertNotNull(book);
31
+
32
+
29 33
     }
30 34
 
35
+    @Test
31 36
     public void testRemove() {
32 37
 
38
+        //Given
39
+        PhoneBook book = new PhoneBook();
40
+        book.add("Robert", "302-555-1234");
41
+        String expectedValue = "Robert " + book.lookup("Robert") + " has been removed";
42
+        book.add("Robert", expectedValue);
43
+
44
+        //When
45
+        String actualNumber = book.remove("Robert");
46
+
47
+        //Then
48
+        Assert.assertEquals(expectedValue, actualNumber );
49
+
50
+
33 51
     }
34 52
 
53
+    @Test
35 54
     public void testLookup(){
36 55
 
37 56
         //Given
38
-        String expectedNumber = "302-555-1234";
39 57
         PhoneBook book = new PhoneBook();
40
-        book.add("Bob", "302-555-1234");
58
+        String expectedNumber = "302-555-1234";
59
+        book.add("Bob", expectedNumber);
41 60
 
42 61
         //When
43
-        book.lookup("Bob");
62
+        String actualNumber = book.lookup("Bob");
44 63
 
45 64
         //Then
46
-        Assert.assertEquals(expectedNumber, "302-555-1234");
65
+        Assert.assertEquals(expectedNumber, actualNumber);
47 66
     }
48
-
67
+    @Test
49 68
     public void testDisplay() {
50
-        //Given
51
-        String expectedNameNumber = "Bob 302-555-1234 \n";
69
+        //Given: need help from instructor
70
+        String expectedNameNumber = "Bob 302-555-1234\nFrank 777-555-1111\n";
52 71
         PhoneBook book = new PhoneBook();
53 72
         book.add("Bob", "302-555-1234");
73
+        book.add("Frank", "777-555-1111");
54 74
         //When
55 75
         String actualNameNumber = book.display();
56 76
         //Then