|
@@ -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() {
|