|
@@ -1,7 +1,48 @@
|
1
|
1
|
|
2
|
|
-
|
|
2
|
+import java.util.*;
|
3
|
3
|
/**
|
4
|
4
|
* Created by leon on 1/23/18.
|
5
|
5
|
*/
|
6
|
6
|
public class PhoneBook {
|
|
7
|
+ private Map<String, ArrayList<String>> phoneBook;
|
|
8
|
+
|
|
9
|
+ public PhoneBook() {
|
|
10
|
+ phoneBook = new TreeMap<String, ArrayList<String>>();
|
|
11
|
+ }
|
|
12
|
+
|
|
13
|
+ public ArrayList<String> add(String name, String number) {
|
|
14
|
+ if (phoneBook.get(name) == null) {
|
|
15
|
+ phoneBook.put(name, new ArrayList<String>());
|
|
16
|
+ }
|
|
17
|
+ phoneBook.get(name).add(number);
|
|
18
|
+
|
|
19
|
+ return phoneBook.get(name);
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ public ArrayList<String> lookup(String name) {
|
|
23
|
+ return phoneBook.get(name);
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ public String reverseLookup(String phoneNumber) {
|
|
27
|
+ String name = "not found";
|
|
28
|
+ for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()) {
|
|
29
|
+ for (String numbers : entry.getValue()) {
|
|
30
|
+ if (numbers.equals(phoneNumber)) name = entry.getKey();
|
|
31
|
+ }
|
|
32
|
+ }
|
|
33
|
+ return name;
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ public String display() {
|
|
37
|
+ StringBuilder phoneNumbers = new StringBuilder();
|
|
38
|
+ for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()) {
|
|
39
|
+ phoneNumbers.append(entry.getKey() + " ");
|
|
40
|
+ for (String number : entry.getValue()) {
|
|
41
|
+ phoneNumbers.append(number + " ");
|
|
42
|
+ }
|
|
43
|
+ phoneNumbers.append("\n");
|
|
44
|
+ }
|
|
45
|
+ return phoneNumbers.toString();
|
|
46
|
+ }
|
|
47
|
+
|
7
|
48
|
}
|