|
@@ -6,33 +6,43 @@ import java.util.*;
|
6
|
6
|
public class PhoneBook {
|
7
|
7
|
String name;
|
8
|
8
|
String phoneNumber;
|
|
9
|
+
|
|
10
|
+ // create treemap(hashmap), has key String, value arrayList of Strings
|
9
|
11
|
TreeMap<String, ArrayList<String>> phoneBook = new TreeMap<>();
|
10
|
|
-
|
|
12
|
+
|
|
13
|
+ // construct an instance of phoneBook
|
11
|
14
|
public PhoneBook(TreeMap<String, ArrayList<String>> phoneBook) {
|
12
|
15
|
this.phoneBook = phoneBook;
|
13
|
16
|
}
|
14
|
|
-
|
|
17
|
+
|
|
18
|
+ // default constructor
|
15
|
19
|
public PhoneBook(){}
|
16
|
20
|
|
17
|
21
|
public void add(String name, String phoneNumber){
|
|
22
|
+ // if phonebook does NOT contain key(name), put name on list
|
18
|
23
|
if (!phoneBook.containsKey(name)) {
|
19
|
24
|
ArrayList<String> list = new ArrayList<>();
|
20
|
25
|
list.add(phoneNumber);
|
21
|
|
- phoneBook.put(name, list);
|
|
26
|
+ phoneBook.put(name, list);
|
|
27
|
+ // or else, create tempoary arraylist and add phonenumber
|
22
|
28
|
} else {
|
23
|
|
-
|
24
|
29
|
ArrayList<String> temp = phoneBook.get(name);
|
25
|
30
|
temp.add(phoneNumber);
|
26
|
|
- phoneBook.replace(name, phoneBook.get(name), temp);//key,old,new
|
27
|
|
-
|
|
31
|
+
|
|
32
|
+ //(key,old key,new key)
|
|
33
|
+ phoneBook.replace(name, phoneBook.get(name), temp);
|
28
|
34
|
}
|
29
|
35
|
}
|
30
|
36
|
|
31
|
37
|
public void remove(String name, String phoneNumber){
|
|
38
|
+
|
32
|
39
|
ArrayList<String> list = phoneBook.get(name);
|
33
|
|
-
|
|
40
|
+ // if only one number, remove name
|
34
|
41
|
if(list.size() == 1) {
|
35
|
|
- phoneBook.remove(name);
|
|
42
|
+ phoneBook.remove(name);
|
|
43
|
+
|
|
44
|
+ // or else create temp arraylist and remove phonenumber/replace
|
|
45
|
+ // in list with parameters (name, list, temp)
|
36
|
46
|
} else {
|
37
|
47
|
ArrayList<String> temp = phoneBook.get(name);
|
38
|
48
|
temp.remove(phoneNumber);
|
|
@@ -43,13 +53,15 @@ public class PhoneBook {
|
43
|
53
|
public void removeRecord(String name) {
|
44
|
54
|
phoneBook.remove(name);
|
45
|
55
|
}
|
46
|
|
-
|
|
56
|
+
|
|
57
|
+ // lookup returns list of phonenumbers for given name
|
47
|
58
|
public ArrayList<String> lookup(String name){
|
48
|
59
|
return phoneBook.get(name);
|
49
|
60
|
}
|
50
|
|
-
|
|
61
|
+
|
|
62
|
+
|
51
|
63
|
public String reverseLookup(String phoneNumber) {
|
52
|
|
- // for each entry in phonebook
|
|
64
|
+ // for each entry in phonebook, lookup array of name/number
|
53
|
65
|
for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()) {
|
54
|
66
|
// gets name
|
55
|
67
|
String name = entry.getKey();
|
|
@@ -69,11 +81,13 @@ public class PhoneBook {
|
69
|
81
|
// build a string to display
|
70
|
82
|
StringBuilder sb = new StringBuilder();
|
71
|
83
|
|
|
84
|
+ // For (string, phone number) entry, iterate thru phoneBook.entrySet
|
72
|
85
|
for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()){
|
73
|
86
|
|
74
|
87
|
String name = entry.getKey();
|
75
|
88
|
ArrayList<String> numbers = entry.getValue();
|
76
|
|
-
|
|
89
|
+
|
|
90
|
+ // when key got, add name to space " "
|
77
|
91
|
sb.append(name).append(" ");
|
78
|
92
|
|
79
|
93
|
// for each number in element, append to name
|