Browse Source

need tests

John Kim 6 years ago
parent
commit
06176b5bcf
2 changed files with 47 additions and 9 deletions
  1. 42
    1
      PhoneBook.java
  2. 5
    8
      PhoneBookTest.java

+ 42
- 1
PhoneBook.java View File

@@ -1,7 +1,48 @@
1 1
  
2
-
2
+import java.util.*;
3 3
 /**
4 4
  * Created by leon on 1/23/18.
5 5
  */
6 6
 public class PhoneBook {
7
+    private Map<String, ArrayList<String>> phoneBook;
8
+    
9
+    public PhoneBook() {
10
+        phoneBook = new TreeMap<String, ArrayList<String>>();
11
+    }
12
+    
13
+    public ArrayList<String> add(String name, String number) {
14
+        if (phoneBook.get(name) == null) {
15
+            phoneBook.put(name, new ArrayList<String>());
16
+        }
17
+        phoneBook.get(name).add(number);
18
+        
19
+        return phoneBook.get(name);
20
+    }
21
+    
22
+    public ArrayList<String> lookup(String name) {
23
+        return phoneBook.get(name);
24
+    }
25
+    
26
+    public String reverseLookup(String phoneNumber) {
27
+        String name = "not found";
28
+        for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()) {
29
+            for (String numbers : entry.getValue()) {
30
+                if (numbers.equals(phoneNumber)) name = entry.getKey();
31
+            }
32
+        }
33
+        return name;
34
+    }
35
+    
36
+    public String display() {
37
+        StringBuilder phoneNumbers = new StringBuilder();
38
+        for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()) {
39
+            phoneNumbers.append(entry.getKey() + " ");
40
+            for (String number : entry.getValue()) {
41
+                phoneNumbers.append(number + " ");
42
+            }
43
+            phoneNumbers.append("\n");
44
+        }
45
+        return phoneNumbers.toString();
46
+    }
47
+    
7 48
 }

+ 5
- 8
PhoneBookTest.java View File

@@ -30,13 +30,10 @@ public class PhoneBookTest
30 30
     {
31 31
     }
32 32
 
33
-    /**
34
-     * Tears down the test fixture.
35
-     *
36
-     * Called after every test case method.
37
-     */
38
-    @After
39
-    public void tearDown()
40
-    {
33
+    @Test
34
+    public void addTest() {
35
+        PhoneBook phone = new PhoneBook();
36
+        phone.add("John", "215-658-1490");
37
+        assertEquals("215-658-1490", phone.lookup("John"));
41 38
     }
42 39
 }