Browse Source

still need to add remove individual number

Jared Norris 6 years ago
parent
commit
2dadb70367
2 changed files with 42 additions and 30 deletions
  1. 28
    23
      PhoneBook.java
  2. 14
    7
      PhoneBookTest.java

+ 28
- 23
PhoneBook.java View File

1
  
1
  
2
-import java.util.TreeMap;
2
+import java.util.*;
3
 /**
3
 /**
4
  * Created by leon on 1/23/18.
4
  * Created by leon on 1/23/18.
5
  */
5
  */
6
 public class PhoneBook {
6
 public class PhoneBook {
7
-    private TreeMap<String, String> phoneBook;
8
-    private TreeMap<String, String> reversePhoneBook;
9
-    
7
+    private TreeMap<String, ArrayList<String>> phoneBook;
10
     public PhoneBook() {
8
     public PhoneBook() {
11
-        phoneBook = new TreeMap<String, String>();
12
-        reversePhoneBook = new TreeMap<String, String>();
9
+        phoneBook = new TreeMap<String, ArrayList<String>>(); 
13
     }
10
     }
14
     
11
     
15
     public void add(String name, String phoneNumber) {
12
     public void add(String name, String phoneNumber) {
16
-        phoneBook.put(name, phoneNumber);
17
-        reversePhoneBook.put(phoneNumber, name);
13
+        ArrayList<String> input = phoneBook.containsKey(name) ? 
14
+                                  phoneBook.get(name)
15
+                                  :                         
16
+                                  new ArrayList<String>();
17
+        input.add(phoneNumber);
18
+        phoneBook.put(name, input);
18
     }
19
     }
19
     
20
     
20
-    public void remove(String name) {
21
-        String phoneNumber = phoneBook.get(name);
21
+    public void removeEntry(String name) {
22
         phoneBook.remove(name);
22
         phoneBook.remove(name);
23
-        reversePhoneBook.remove(phoneNumber);
24
     }
23
     }
25
     
24
     
26
     public String lookup(String name) {
25
     public String lookup(String name) {
27
-        return phoneBook.get(name) != null ? 
28
-                                   phoneBook.get(name)
29
-                                   : 
30
-                                   "N/A";
26
+        return phoneBook.get(name)
27
+                        .toString()
28
+                        .replaceAll("[\\[\\]]", "");
31
     }
29
     }
32
     
30
     
33
     public String reverseLookup(String phoneNumber) {
31
     public String reverseLookup(String phoneNumber) {
34
-        return reversePhoneBook.get(phoneNumber) != null ? 
35
-                                                 reversePhoneBook.get(phoneNumber)
36
-                                                 :
37
-                                                 "N/A";
32
+        String output = "";
33
+        Set<String> keys = phoneBook.keySet();
34
+        for (String key : keys) {
35
+            if (phoneBook.get(key).contains(phoneNumber)) {
36
+                output = key;
37
+            }
38
+        }
39
+        return output.equals("") ? "No associated name." : output;
38
     }
40
     }
39
     
41
     
40
-    public TreeMap getPhoneBook() {
41
-        return phoneBook; 
42
+    public String display() {
43
+       return phoneBook.toString().replaceAll("[{}=,]", "")
44
+                                  .replaceAll("\\[", " - ")
45
+                                  .replaceAll("\\]", "\n");
46
+       
42
     }
47
     }
43
     
48
     
44
-    public TreeMap getReversePhoneBook() {
45
-        return reversePhoneBook;
49
+    public TreeMap getPhoneBook() {
50
+        return phoneBook; 
46
     }
51
     }
47
 }
52
 }

+ 14
- 7
PhoneBookTest.java View File

33
        assertEquals(expectedNumber, actual);
33
        assertEquals(expectedNumber, actual);
34
     }
34
     }
35
     
35
     
36
-    @Test
37
-    public void testRemovePhoneBookEntry_And_Lookup()
36
+    @Test(expected = NullPointerException.class)
37
+    public void testRemoveEntry_And_Lookup()
38
     {
38
     {
39
        PhoneBook test = new PhoneBook();
39
        PhoneBook test = new PhoneBook();
40
        String name = "Jenny";
40
        String name = "Jenny";
41
-       String wrongName = "Tommy Tutone";
42
        String number = "(302)-867-5309";
41
        String number = "(302)-867-5309";
43
        test.add(name, number);
42
        test.add(name, number);
44
-       
45
-       String actual = test.lookup(wrongName); 
46
-       assertEquals("N/A", actual);
43
+       test.removeEntry(name);
44
+       test.lookup(name);   
47
     }
45
     }
48
     
46
     
49
     @Test
47
     @Test
57
        String actual = test.reverseLookup(phoneNumber);
55
        String actual = test.reverseLookup(phoneNumber);
58
        assertEquals(expectedName, actual);
56
        assertEquals(expectedName, actual);
59
      
57
      
60
-    }   
58
+    } 
59
+    
60
+    @Test
61
+    public void testDisplay() {
62
+       PhoneBook test = new PhoneBook();
63
+       test.add("hey", "302");
64
+       test.add("yo", "521");
65
+       test.add("hey", "498");
66
+       System.out.println(test.display());
67
+    }
61
 }
68
 }