ソースを参照

Step two and added tests

Carolynn Vansant 6 年 前
コミット
065427997d
共有2 個のファイルを変更した75 個の追加14 個の削除を含む
  1. 12
    8
      src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java
  2. 63
    6
      src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java

+ 12
- 8
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java ファイルの表示

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

+ 63
- 6
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java ファイルの表示

@@ -44,19 +44,36 @@ public class PhoneBookTest {
44 44
 
45 45
         //Given
46 46
         PhoneBook book = new PhoneBook();
47
-        book.add("Robert", "302-555-1234");
48
-        String expectedValue = "Robert " + book.lookup("Robert") + " has been removed";
49
-        book.add("Robert", expectedValue);
47
+        book.add("Bob", "302-555-1234");
48
+        String expectedValue = "";
49
+
50
+        //When
51
+        book.remove("Bob");
52
+        String actualValue = book.display();
53
+
54
+        //Then
55
+        Assert.assertEquals(expectedValue, actualValue);
56
+    }
57
+
58
+    @Test
59
+    public void testRemove2() {
60
+
61
+        //Given
62
+        PhoneBook book = new PhoneBook();
63
+        book.add("Bob", "302-555-1234");
64
+        String expectedValue = "";
50 65
 
51 66
         //When
52
-        String actualNumber = book.remove("Robert");
67
+        book.remove("Robert");
68
+        String actualValue = book.display();
53 69
 
54 70
         //Then
55
-        Assert.assertEquals(expectedValue, actualNumber );
71
+        Assert.assertNotEquals(expectedValue, actualValue);
56 72
 
57 73
 
58 74
     }
59 75
 
76
+
60 77
     @Test
61 78
     public void testLookup(){
62 79
 
@@ -64,13 +81,53 @@ public class PhoneBookTest {
64 81
         PhoneBook book = new PhoneBook();
65 82
         String expectedNumber = "302-555-1234";
66 83
         book.add("Bob", expectedNumber);
67
-
68 84
         //When
69 85
         String actualNumber = book.lookup("Bob");
86
+        //Then
87
+        Assert.assertEquals(expectedNumber, actualNumber);
88
+    }
70 89
 
90
+    @Test
91
+    public void testLookup2(){
92
+
93
+        //Given
94
+        PhoneBook book = new PhoneBook();
95
+        book.add("Bob", "302-555-1234");
96
+        String expectedNumber = null;
97
+        //When
98
+        String actualNumber = book.lookup("Frank");
99
+        //Then
100
+        Assert.assertEquals(expectedNumber, actualNumber);
101
+    }
102
+
103
+    @Test
104
+    public void testLookup3(){
105
+
106
+        //Given
107
+        PhoneBook book = new PhoneBook();
108
+        book.add("Bob", "302-555-1234");
109
+        String expectedNumber = "302-555-9988";
110
+        book.add("Frank", expectedNumber);
111
+        //When
112
+        String actualNumber = book.lookup("Frank");
71 113
         //Then
72 114
         Assert.assertEquals(expectedNumber, actualNumber);
73 115
     }
116
+
117
+    @Test
118
+    public void testreverseLookup(){
119
+
120
+        //Given
121
+        PhoneBook book = new PhoneBook();
122
+        String expectedName = "Bob";
123
+        book.add(expectedName, "302-555-1234");
124
+        //When
125
+        String actualName = book.reverseLookup("302-555-1234");
126
+        //Then
127
+        Assert.assertEquals(expectedName, actualName);
128
+    }
129
+
130
+
74 131
     @Test
75 132
     public void testDisplay() {
76 133
         //Given: need help from instructor