Jared Norris před 6 roky
rodič
revize
eea2f32c26
2 změnil soubory, kde provedl 37 přidání a 12 odebrání
  1. 20
    9
      PhoneBook.java
  2. 17
    3
      PhoneBookTest.java

+ 20
- 9
PhoneBook.java Zobrazit soubor

1
- 
1
+//JN 2018  
2
 import java.util.*;
2
 import java.util.*;
3
-/**
4
- * Created by leon on 1/23/18.
5
- */
6
 public class PhoneBook {
3
 public class PhoneBook {
7
     private TreeMap<String, ArrayList<String>> phoneBook;
4
     private TreeMap<String, ArrayList<String>> phoneBook;
8
     public PhoneBook() {
5
     public PhoneBook() {
12
     public void add(String name, String phoneNumber) {
9
     public void add(String name, String phoneNumber) {
13
         ArrayList<String> input = phoneBook.containsKey(name) ? 
10
         ArrayList<String> input = phoneBook.containsKey(name) ? 
14
                                   phoneBook.get(name)
11
                                   phoneBook.get(name)
15
-                                  :                         
16
-                                  new ArrayList<String>();
12
+                                : new ArrayList<String>();
17
         input.add(phoneNumber);
13
         input.add(phoneNumber);
18
         phoneBook.put(name, input);
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
     public void removeEntry(String name) {
29
     public void removeEntry(String name) {
22
         phoneBook.remove(name);
30
         phoneBook.remove(name);
23
     }
31
     }
24
     
32
     
25
     public String lookup(String name) {
33
     public String lookup(String name) {
26
-        return phoneBook.get(name)
34
+        
35
+        return phoneBook.containsKey(name) ?
36
+               phoneBook.get(name)
27
                         .toString()
37
                         .toString()
28
-                        .replaceAll("[\\[\\]]", "");
38
+                        .replaceAll("[\\[\\]]", "")
39
+            : "N/A";
29
     }
40
     }
30
     
41
     
31
     public String reverseLookup(String phoneNumber) {
42
     public String reverseLookup(String phoneNumber) {
36
                 output = key;
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
     public String display() {
53
     public String display() {

+ 17
- 3
PhoneBookTest.java Zobrazit soubor

33
        assertEquals(expectedNumber, actual);
33
        assertEquals(expectedNumber, actual);
34
     }
34
     }
35
     
35
     
36
-    @Test(expected = NullPointerException.class)
36
+    @Test
37
     public void testRemoveEntry_And_Lookup()
37
     public void testRemoveEntry_And_Lookup()
38
     {
38
     {
39
        PhoneBook test = new PhoneBook();
39
        PhoneBook test = new PhoneBook();
41
        String number = "(302)-867-5309";
41
        String number = "(302)-867-5309";
42
        test.add(name, number);
42
        test.add(name, number);
43
        test.removeEntry(name);
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
     @Test
61
     @Test
63
        test.add("hey", "302");
77
        test.add("hey", "302");
64
        test.add("yo", "521");
78
        test.add("yo", "521");
65
        test.add("hey", "498");
79
        test.add("hey", "498");
66
-       System.out.println(test.display());
80
+       test.display();
67
     }
81
     }
68
 }
82
 }