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