Browse Source

added reverselookup

Keith Brinker 6 years ago
parent
commit
ee2b361184

+ 12
- 3
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -27,7 +27,6 @@ public PhoneBook() {
27 27
 
28 28
  public String lookup(String name){
29 29
         return contacts.get(name);
30
-
31 30
  }
32 31
 
33 32
  public String listNames(){
@@ -46,13 +45,23 @@ public PhoneBook() {
46 45
         StringBuilder namesnumbers = new StringBuilder();
47 46
         Set<String> keys = contacts.keySet();
48 47
         for(String key: keys)
49
-
50 48
         {
51
-
52 49
             namesnumbers.append(key+" "+contacts.get(key)+"\n");
53 50
         }
54 51
         return namesnumbers.toString();
55 52
     }
56 53
 
54
+    public String reverselookup(String number){
55
+        Set<String> names = contacts.keySet();
56
+        for(String name: names)
57
+        {
58
+            if (contacts.get(name).equals(number)){
59
+                return name;
60
+            }
61
+        }
62
+        return null;
63
+    }
64
+
65
+
57 66
 
58 67
 }

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

@@ -76,6 +76,14 @@ public class PhoneBookTest {
76 76
         String actual = testbook.listNames();
77 77
         Assert.assertEquals(expected, actual);
78 78
     }
79
-
80
-
79
+    @Test
80
+    public void testPhoneBookReverseLookUp() {
81
+        PhoneBook testbook = new PhoneBook();
82
+        testbook.add("Keith", "555-666-7777");
83
+        testbook.add("John", "555-612-7777");
84
+        testbook.add("Mike", "555-623-7777");
85
+        testbook.add("Steve", "555-645-7777");
86
+        String testnumber = testbook.reverselookup("555-666-7777");
87
+        Assert.assertEquals("Keith", testnumber);
88
+    }
81 89
 }