Browse Source

reverseLookup left

nafis nibir 6 years ago
parent
commit
7dcc563172
2 changed files with 118 additions and 8 deletions
  1. 56
    2
      PhoneBook.java
  2. 62
    6
      PhoneBookTest.java

+ 56
- 2
PhoneBook.java View File

@@ -1,4 +1,58 @@
1
-
1
+import java.util.Map;
2
+import java.util.*;
2 3
 public class PhoneBook {
3
-    
4
+    Map<String,List<String>> phoneBook = new TreeMap<String,List<String>>();
5
+
6
+    public PhoneBook(){
7
+
8
+    }
9
+
10
+    public void addEntrant(String name, String...phoneNumber){
11
+        String checkName = lookup(name);
12
+        if(checkName == null){
13
+            phoneBook.put(name, Arrays.asList(phoneNumber));
14
+        } else {
15
+            System.out.println("Error, user already exists");
16
+        }
17
+    }
18
+
19
+    public void removeEntrant(String name){
20
+        phoneBook.remove(name);   
21
+    }
22
+
23
+    public String lookup(String name){
24
+        if (phoneBook.get(name) == null){
25
+            return null;
26
+        }
27
+        StringBuilder numbersList = new StringBuilder();
28
+        int counter = 0;
29
+        for(String number: phoneBook.get(name)){
30
+            numbersList.append(number);
31
+            counter++;
32
+            if(counter != phoneBook.get(name).size()){
33
+                numbersList.append(", ");
34
+            }
35
+        }
36
+
37
+        return numbersList.toString();
38
+    }
39
+
40
+    public String reverseLookup(String phoneNumber){
41
+        for(Map.Entry<String, List<String>> element: phoneBook.entrySet()){
42
+            String key = element.getKey();
43
+            List<String> value = element.getValue();
44
+            if(value.equals(phoneNumber)){
45
+                return key;
46
+            }
47
+        }
48
+        return "This number does not belong to anyone";
49
+    }
50
+
51
+    public void display(){
52
+        for(Map.Entry<String, List<String>> element: phoneBook.entrySet()){
53
+            String key = element.getKey();
54
+            List<String> value = element.getValue();
55
+            System.out.println( key + ":" + value);
56
+        }
57
+    }
4 58
 }

+ 62
- 6
PhoneBookTest.java View File

@@ -7,12 +7,68 @@ import org.junit.Test;
7 7
 
8 8
 public class PhoneBookTest
9 9
 {
10
-    /**
11
-     * Sets up the test fixture.
12
-     *
13
-     * Called before every test case method.
14
-     */
10
+
15 11
     @Before
16
-    public void setUp(){    
12
+    public void setUp(){ 
13
+        PhoneBook pb = new PhoneBook(); 
14
+    }
15
+    
16
+    @Test
17
+    public void testAdd1(){
18
+        PhoneBook pb = new PhoneBook();
19
+        
20
+        String expected = "123, 456";
21
+        
22
+        pb.addEntrant("John" , "123", "456");
23
+        String actual = pb.lookup("John");
24
+        
25
+        assertEquals(expected, actual);
26
+    }
27
+    
28
+    @Test
29
+    public void testAdd2(){
30
+        PhoneBook pb = new PhoneBook();
31
+        
32
+        String expected = "123";
33
+        
34
+        pb.addEntrant("Josh" , "123");
35
+        String actual = pb.lookup("Josh");
36
+        
37
+        assertEquals(expected, actual);
38
+    }
39
+    
40
+    
41
+    @Test
42
+    public void testRemove(){
43
+        PhoneBook pb = new PhoneBook();
44
+        
45
+        String expected = null;
46
+        
47
+        pb.addEntrant("Josh" , "123");
48
+        pb.removeEntrant("Josh");
49
+        String actual = pb.lookup("Josh");
50
+        
51
+        assertEquals(expected, actual);
52
+        
53
+    }
54
+    
55
+    @Test
56
+    public void testLookup(){
57
+        PhoneBook pb = new PhoneBook();
58
+        
59
+        String expected = "123, 456, 789";
60
+        
61
+        pb.addEntrant("Josh" , "123", "456", "789");
62
+        String actual = pb.lookup("Josh");
63
+        
64
+        assertEquals(expected, actual);
65
+    }
66
+    
67
+    @Test
68
+    public void testReverseLookup(){
69
+        
70
+        
17 71
     }
72
+    
73
+    
18 74
 }