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
     }
30
     }
31
 
31
 
32
     //remove a name & number entry
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
         if(treeMap.containsKey(name)) {
35
         if(treeMap.containsKey(name)) {
35
             treeMap.remove(name);
36
             treeMap.remove(name);
36
         }
37
         }
38
+        return removeName;
37
     }
39
     }
38
 
40
 
39
     //find phone number lookup by name
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
     //print out all of the entries in PhoneBook
52
     //print out all of the entries in PhoneBook
48
     //need to work on code for formatting
53
     //need to work on code for formatting
54
+
49
     public String display(){
55
     public String display(){
50
-        String printOut = "";
56
+        StringBuilder printOut = new StringBuilder();
51
         for(Map.Entry<String, String> entry: treeMap.entrySet()) {
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
         System.out.println(printOut);
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
  * Created by leon on 1/23/18.
9
  * Created by leon on 1/23/18.
10
  */
10
  */
11
 public class PhoneBookTest {
11
 public class PhoneBookTest {
12
-
12
+    @Test
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
 
18
 
17
 
19
 
18
 
20
 
19
     }
21
     }
20
-
22
+    @Test
21
     public void testConstructorWithArgument() {
23
     public void testConstructorWithArgument() {
22
 
24
 
23
     }
25
     }
24
 
26
 
27
+    @Test
25
     public void testAdd() {
28
     public void testAdd() {
26
         PhoneBook book = new PhoneBook();
29
         PhoneBook book = new PhoneBook();
27
         book.add("Bob", "302-555-1234");
30
         book.add("Bob", "302-555-1234");
28
-        Assert.assertNotNull(book);
31
+
32
+
29
     }
33
     }
30
 
34
 
35
+    @Test
31
     public void testRemove() {
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
     public void testLookup(){
54
     public void testLookup(){
36
 
55
 
37
         //Given
56
         //Given
38
-        String expectedNumber = "302-555-1234";
39
         PhoneBook book = new PhoneBook();
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
         //When
61
         //When
43
-        book.lookup("Bob");
62
+        String actualNumber = book.lookup("Bob");
44
 
63
 
45
         //Then
64
         //Then
46
-        Assert.assertEquals(expectedNumber, "302-555-1234");
65
+        Assert.assertEquals(expectedNumber, actualNumber);
47
     }
66
     }
48
-
67
+    @Test
49
     public void testDisplay() {
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
         PhoneBook book = new PhoneBook();
71
         PhoneBook book = new PhoneBook();
53
         book.add("Bob", "302-555-1234");
72
         book.add("Bob", "302-555-1234");
73
+        book.add("Frank", "777-555-1111");
54
         //When
74
         //When
55
         String actualNameNumber = book.display();
75
         String actualNameNumber = book.display();
56
         //Then
76
         //Then