Raymond Farria 6 years ago
parent
commit
00ebbfc3d9
2 changed files with 138 additions and 24 deletions
  1. 49
    1
      PhoneBook.java
  2. 89
    23
      PhoneBookTest.java

+ 49
- 1
PhoneBook.java View File

@@ -1,7 +1,55 @@
1
- 
2 1
 
2
+import java.util.TreeMap;
3
+import java.util.*;
3 4
 /**
4 5
  * Created by leon on 1/23/18.
5 6
  */
6 7
 public class PhoneBook {
8
+    TreeMap<String, ArrayList<String>> pb;
9
+
10
+    public PhoneBook(){
11
+        pb = new TreeMap<>();   
12
+
13
+    }
14
+
15
+    public void addElement(String name, String phoneNumber){
16
+        ArrayList<String> aList  =  pb.containsKey(name) ? pb.get(name): new ArrayList<String>();
17
+        aList.add(phoneNumber + "\n");
18
+        pb.put(name, aList);
19
+    }
20
+
21
+    public void remove(String name){
22
+        pb.remove(name); 
23
+    }
24
+
25
+    public String lookUp(String name){
26
+        StringBuilder look = new StringBuilder();
27
+
28
+        for(String element: this.pb.get(name)){
29
+            
30
+                look.append(element);
31
+                
32
+            
33
+        }
34
+        return look.toString();
35
+    }
36
+
37
+    public String reverseLookup(String phoneNumber){
38
+        String gettingThatValue = null;
39
+        for(Map.Entry<String,ArrayList<String>> element: pb.entrySet()){
40
+            if(element.getValue().contains(phoneNumber)){
41
+                gettingThatValue = element.getKey();
42
+                break;
43
+            }
44
+        }
45
+
46
+        return gettingThatValue;
47
+    }
48
+
49
+    public void display(){
50
+        for(Map.Entry<String,ArrayList<String>> element:pb.entrySet()){
51
+            System.out.print(element.getKey() + ":" + element.getValue().toString());
52
+        }
53
+    }
54
+
7 55
 }

+ 89
- 23
PhoneBookTest.java View File

@@ -1,5 +1,4 @@
1 1
 
2
-
3 2
 import static org.junit.Assert.*;
4 3
 import org.junit.After;
5 4
 import org.junit.Before;
@@ -12,31 +11,98 @@ import org.junit.Test;
12 11
  * @version (a version number or a date)
13 12
  */
14 13
 public class PhoneBookTest
15
-{
16
-    /**
17
-     * Default constructor for test class PhoneBookTest
18
-     */
19
-    public PhoneBookTest()
20
-    {
14
+{   private PhoneBook phonebook;
15
+    private String name;
16
+    private String phoneNumber;
17
+    
18
+    @Before
19
+    public void setUp(){
20
+        phonebook = new PhoneBook();
21
+    
22
+    } 
23
+    
24
+    
25
+
26
+    @Test
27
+    public void addTestFirstNumberForName(){
28
+        String name = "Raymond";
29
+        String expected = "302-444-0094\n";
30
+        
31
+        phonebook.addElement(name,"302-444-0094");
32
+        String actual = phonebook.lookUp(name);
33
+        
34
+        assertEquals(expected,actual);
35
+        
36
+    }
37
+    
38
+    @Test
39
+    public void testAddKeyAlreadyExists() {
40
+        String name = "Raymond";
41
+        String phoneNumber1 = "302-450-8066";
42
+        String phoneNumber2 = "302-465-1770";
43
+        
44
+        String expected = phoneNumber1 + "\n" + phoneNumber2 + "\n";
45
+        phonebook.addElement(name,phoneNumber1);
46
+        phonebook.addElement(name,phoneNumber2);
47
+        String actual = phonebook.lookUp(name);
48
+        
49
+        assertEquals(expected,actual); 
21 50
     }
22 51
 
23
-    /**
24
-     * Sets up the test fixture.
25
-     *
26
-     * Called before every test case method.
27
-     */
28
-    @Before
29
-    public void setUp()
30
-    {
52
+    @Test
53
+    public void removeTest(){
54
+        String name1 = "Raymond";
55
+        String name2 = "Eric";
56
+        String phoneNumber1 = "302-450-8099";
57
+        String phoneNumber2 = "302-333-0003";
58
+        phonebook.addElement(name1, phoneNumber1);
59
+        phonebook.addElement(name2, phoneNumber2);
60
+        
61
+        String expected = "Eric";
62
+        phonebook.remove(name1);
63
+        String actual = name2;
64
+        
65
+        assertEquals(expected,actual);
31 66
     }
32 67
 
33
-    /**
34
-     * Tears down the test fixture.
35
-     *
36
-     * Called after every test case method.
37
-     */
38
-    @After
39
-    public void tearDown()
40
-    {
68
+    @Test
69
+    public void lookUpTest(){
70
+        String name = "Raymond";
71
+        String phoneNumber = "302-333-3333";
72
+        phonebook.addElement(name,phoneNumber);
73
+        
74
+        String expected = "302-333-3333";
75
+        phonebook.lookUp(name);
76
+        String actual = phoneNumber;
77
+        
78
+        assertEquals(expected,actual);
41 79
     }
80
+
81
+    @Test
82
+    public void reverseLookup(){
83
+        String name = "Raymond";
84
+        String phoneNumber = "302-333-3333";
85
+        phonebook.addElement(name,phoneNumber);
86
+        
87
+        String expected = "Raymond";
88
+        phonebook.reverseLookup(phoneNumber);
89
+        String actual = name;
90
+        
91
+        assertEquals(expected,actual);
92
+    }
93
+
94
+    @Test
95
+    public void displayTest(){
96
+        String name = "Raymond";
97
+        String phoneNumber = "302-450-2222";
98
+        phonebook.addElement(name,phoneNumber);
99
+        
100
+        String expected = "Raymond:302-450-2222";
101
+        phonebook.display();
102
+        String actual = name + ":" + phoneNumber;
103
+
104
+        assertEquals(expected,actual);
105
+    }
106
+
107
+    
42 108
 }