Browse Source

final changes

Jared Norris 6 years ago
parent
commit
eea2f32c26
2 changed files with 37 additions and 12 deletions
  1. 20
    9
      PhoneBook.java
  2. 17
    3
      PhoneBookTest.java

+ 20
- 9
PhoneBook.java View File

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

+ 17
- 3
PhoneBookTest.java View File

@@ -33,7 +33,7 @@ public class PhoneBookTest
33 33
        assertEquals(expectedNumber, actual);
34 34
     }
35 35
     
36
-    @Test(expected = NullPointerException.class)
36
+    @Test
37 37
     public void testRemoveEntry_And_Lookup()
38 38
     {
39 39
        PhoneBook test = new PhoneBook();
@@ -41,7 +41,21 @@ public class PhoneBookTest
41 41
        String number = "(302)-867-5309";
42 42
        test.add(name, number);
43 43
        test.removeEntry(name);
44
-       test.lookup(name);   
44
+       String actual = test.lookup(name);
45
+       assertEquals("N/A", actual);
46
+    }
47
+    
48
+    @Test
49
+    public void testRemoveIndividualNumber() {
50
+       PhoneBook test = new PhoneBook();
51
+       String name = "Jenny";
52
+       String number = "(302)-867-5309";
53
+       String number2 = "(601)-482-9832";
54
+       test.add(name, number);
55
+       test.add(name, number2);
56
+       test.remove(number);
57
+       String actual = test.reverseLookup(number);
58
+       assertEquals("N/A", actual);
45 59
     }
46 60
     
47 61
     @Test
@@ -63,6 +77,6 @@ public class PhoneBookTest
63 77
        test.add("hey", "302");
64 78
        test.add("yo", "521");
65 79
        test.add("hey", "498");
66
-       System.out.println(test.display());
80
+       test.display();
67 81
     }
68 82
 }