nafis nibir 6 years ago
parent
commit
6c52b2194d
2 changed files with 110 additions and 5 deletions
  1. 31
    4
      PhoneBook.java
  2. 79
    1
      PhoneBookTest.java

+ 31
- 4
PhoneBook.java View File

@@ -16,6 +16,33 @@ public class PhoneBook {
16 16
         }
17 17
     }
18 18
 
19
+    public void addNumber(String name, String newNumber){
20
+        String checkName = lookup(name);
21
+        if(checkName == null){
22
+            addEntrant(name, newNumber);
23
+        } else {
24
+            List<String> newList = new ArrayList<>(phoneBook.get(name));
25
+            newList.add(newNumber);
26
+            phoneBook.put(name, newList);
27
+        }
28
+    }
29
+    
30
+    public void removeNumber(String name, String numberToDelete){
31
+        String checkName = lookup(name);
32
+        if(checkName == null){
33
+            System.out.println("User does not exist");
34
+        } else {
35
+            List<String> newList = new ArrayList<>(phoneBook.get(name));
36
+            ListIterator<String> pbIterator = newList.listIterator();
37
+            while(pbIterator.hasNext()){
38
+                if(pbIterator.next().equals(numberToDelete)){
39
+                    pbIterator.remove();
40
+                }
41
+            }
42
+            phoneBook.put(name, newList);
43
+        }
44
+    }
45
+
19 46
     public void removeEntrant(String name){
20 47
         phoneBook.remove(name);   
21 48
     }
@@ -33,16 +60,16 @@ public class PhoneBook {
33 60
                 numbersList.append(", ");
34 61
             }
35 62
         }
36
-
37 63
         return numbersList.toString();
38 64
     }
39 65
 
40 66
     public String reverseLookup(String phoneNumber){
41 67
         for(Map.Entry<String, List<String>> element: phoneBook.entrySet()){
42 68
             String key = element.getKey();
43
-            List<String> value = element.getValue();
44
-            if(value.equals(phoneNumber)){
45
-                return key;
69
+            for(String number: phoneBook.get(key)){
70
+                if(number.equals(phoneNumber)){
71
+                    return key;
72
+                }
46 73
             }
47 74
         }
48 75
         return "This number does not belong to anyone";

+ 79
- 1
PhoneBookTest.java View File

@@ -65,10 +65,88 @@ public class PhoneBookTest
65 65
     }
66 66
     
67 67
     @Test
68
-    public void testReverseLookup(){
68
+    public void testReverseLookup1(){
69
+        PhoneBook pb = new PhoneBook();
70
+        
71
+        String expected = "Joe";
72
+        
73
+        pb.addEntrant("Josh" , "123", "456", "789");
74
+        pb.addEntrant("John" , "321", "654", "987");
75
+        pb.addEntrant("Joe" , "111", "222", "333");
69 76
         
77
+        String actual = pb.reverseLookup("222");
70 78
         
79
+        assertEquals(expected, actual);
71 80
     }
72 81
     
82
+    @Test
83
+    public void testReverseLookup2(){
84
+        PhoneBook pb = new PhoneBook();
85
+        
86
+        String expected = "John";
87
+        
88
+        pb.addEntrant("Josh" , "123", "456", "789");
89
+        pb.addEntrant("John" , "321", "654", "987");
90
+        pb.addEntrant("Joe" , "111", "222", "333");
91
+        
92
+        String actual = pb.reverseLookup("654");
93
+        
94
+        assertEquals(expected, actual);
95
+    }
96
+    
97
+    @Test
98
+    public void testAddNumber1(){
99
+        PhoneBook pb = new PhoneBook();
100
+        
101
+        String expected = "123, 456";
102
+        
103
+        pb.addEntrant("Josh" , "123");
104
+        pb.addNumber("Josh","456");
105
+        
106
+        String actual = pb.lookup("Josh");
107
+        assertEquals(expected, actual);
108
+    }
109
+    
110
+    @Test
111
+    public void testAddNumber2(){
112
+        PhoneBook pb = new PhoneBook();
113
+        
114
+        String expected = "123, 456, 789";
115
+        
116
+        pb.addEntrant("Jim" , "123");
117
+        pb.addNumber("Jim","456");
118
+        pb.addNumber("Jim","789");
119
+        
120
+        String actual = pb.lookup("Jim");
121
+        assertEquals(expected, actual);
122
+    }
123
+    
124
+    @Test
125
+    public void testRemoveNumber1(){
126
+        PhoneBook pb = new PhoneBook();
127
+        
128
+        String expected = "123, 456";
129
+        
130
+        pb.addEntrant("Jim" , "123", "456", "789");
131
+        pb.removeNumber("Jim", "789");
132
+        
133
+        String actual = pb.lookup("Jim");
134
+        assertEquals(expected, actual);
135
+    }
136
+    
137
+    @Test
138
+    public void testRemoveNumber2(){
139
+        PhoneBook pb = new PhoneBook();
140
+        
141
+        String expected = "123, 789";
142
+        
143
+        pb.addEntrant("Jim" , "123", "456", "789");
144
+        pb.removeNumber("Jim", "456");
145
+        
146
+        String actual = pb.lookup("Jim");
147
+        assertEquals(expected, actual);
148
+    }
149
+    
150
+    
73 151
     
74 152
 }