Daniel Horowitz 6 年之前
父節點
當前提交
28ca435fd5

+ 16
- 10
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java 查看文件

@@ -65,25 +65,31 @@ public class PhoneBook {
65 65
 
66 66
             printBook.append(key).append(" = ").append(value).append("\n");
67 67
 
68
-//        String allInfo = "";
69
-//
70
-//        for (Map.Entry<String, String> entry: myTree.entrySet()){
71
-//            String getKeys = String.format("%1$-15s", entry.getKey());
72
-//            String getValues = String.format("%1$-15s", entry.getValue());
73
-//
74
-//            allInfo += getKeys + getValues + "\n";
75 68
         }
76 69
         String result = printBook.toString();
77 70
 
78 71
         return result;
79 72
     }
80 73
 
81
-    public String reverseLookup() {
74
+    public String reverseLookup(String number) {
75
+
76
+        String getName = "";
77
+        for (Map.Entry<String, String> entry : myTree.entrySet()) {
78
+            if (entry.getValue() == number) {
79
+                getName += entry.getKey();
80
+                return getName;
81
+            }
82
+
83
+        }
84
+
85
+
86
+        return "not in my phone book";
82 87
 
83
-        return null;
84 88
     }
85 89
 
86 90
 
91
+
92
+
87 93
     public static void main(String[] args) {
88 94
 
89 95
         PhoneBook phonebookEntry = new PhoneBook();
@@ -99,7 +105,7 @@ public class PhoneBook {
99 105
 
100 106
 
101 107
         phonebookEntry.listPhoneBook();
102
-        System.out.println(phonebookEntry.listPhoneBook());
108
+        System.out.println(phonebookEntry.reverseLookup("4899994190"));
103 109
 
104 110
     }
105 111
 

+ 8
- 0
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java 查看文件

@@ -84,5 +84,13 @@ public class PhoneBookTest {
84 84
 
85 85
 
86 86
     }
87
+    @Test
88
+    public void ReverseLookupTest() {
89
+        PhoneBook testBook = new PhoneBook();
90
+        testBook.addEntry("Kate", "4846569090");
91
+        String testNumber = testBook.reverseLookup("4846569090");
92
+        Assert.assertTrue(testNumber.equals("Kate"));
93
+
94
+    }
87 95
 
88 96
 }