|
@@ -6,28 +6,23 @@ import java.util.*;
|
6
|
6
|
*/
|
7
|
7
|
public class PhoneBook {
|
8
|
8
|
|
9
|
|
- //instance variable
|
10
|
9
|
private TreeMap<String, ArrayList<String>> treeMap;
|
11
|
10
|
|
12
|
|
-
|
13
|
|
- // void "no arguments" constructor -creates empty map
|
14
|
11
|
public PhoneBook() {
|
15
|
12
|
this.treeMap= new TreeMap<String, ArrayList<String>>();
|
16
|
13
|
}
|
17
|
14
|
|
18
|
|
- // single argument constructor -type Map -creates new map w/ same key/value mappings as argument
|
19
|
15
|
public PhoneBook(TreeMap treeMap) {
|
20
|
16
|
this.treeMap = treeMap;
|
21
|
17
|
}
|
22
|
18
|
|
23
|
|
- //add a name & number entry, add number
|
24
|
19
|
public void addEntry(String name, ArrayList<String> number) {
|
25
|
20
|
if (!treeMap.containsKey(name)) {
|
26
|
21
|
treeMap.put(name, number);
|
27
|
22
|
}
|
28
|
23
|
}
|
29
|
24
|
|
30
|
|
- //add numbers to existing entries
|
|
25
|
+
|
31
|
26
|
public void addNumberToEntry(String name, String number) {
|
32
|
27
|
for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) {
|
33
|
28
|
if (name.equals(entry.getKey())) {
|
|
@@ -37,7 +32,7 @@ public class PhoneBook {
|
37
|
32
|
}
|
38
|
33
|
}
|
39
|
34
|
|
40
|
|
- //remove numbers from existing entries
|
|
35
|
+
|
41
|
36
|
public void removeNumberFromEntry(String name, String number) {
|
42
|
37
|
for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) {
|
43
|
38
|
if (entry.getKey().equals(name)) {
|
|
@@ -48,31 +43,29 @@ public class PhoneBook {
|
48
|
43
|
}
|
49
|
44
|
}
|
50
|
45
|
|
51
|
|
- //remove entire entry
|
|
46
|
+
|
52
|
47
|
public void removeEntry(String name) {
|
53
|
48
|
treeMap.remove(name);
|
54
|
49
|
}
|
55
|
50
|
|
56
|
|
- //lookup: find phone number from name
|
|
51
|
+
|
57
|
52
|
public ArrayList<String> lookup(String name) {
|
58
|
53
|
return treeMap.get(name);
|
59
|
54
|
}
|
60
|
55
|
|
61
|
|
- //reverse lookup: find name from number
|
|
56
|
+
|
62
|
57
|
public String reverseLookup (String number) {
|
63
|
58
|
String name = "";
|
64
|
59
|
for(Map.Entry<String, ArrayList<String>> entry: treeMap.entrySet()) {
|
65
|
60
|
ArrayList<String> value = entry.getValue();
|
66
|
61
|
for(String revnumber: value) {
|
67
|
|
- if(number.contains(revnumber)) {
|
68
|
|
- name = entry.getKey();
|
69
|
|
- }
|
|
62
|
+ if(number.contains(revnumber)) name = entry.getKey();
|
70
|
63
|
}
|
71
|
64
|
}
|
72
|
65
|
return name;
|
73
|
66
|
}
|
74
|
67
|
|
75
|
|
- //print out all of the entries in PhoneBook
|
|
68
|
+
|
76
|
69
|
public String display(){
|
77
|
70
|
StringBuilder printOut = new StringBuilder();
|
78
|
71
|
for(Map.Entry<String, ArrayList<String>> entry: treeMap.entrySet()) {
|