|
@@ -1,25 +1,28 @@
|
1
|
1
|
package com.zipcodewilmington.phonebook;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.List;
|
3
|
5
|
import java.util.Map;
|
4
|
6
|
import java.util.TreeMap;
|
5
|
|
-import java.util.ArrayList;
|
6
|
7
|
|
7
|
8
|
public class PhoneBook {
|
8
|
9
|
|
9
|
|
- TreeMap<String, String> phoneBookList = new TreeMap<String, String>();
|
|
10
|
+ TreeMap<String, List<String>> phoneBookList = new TreeMap<String, List<String>>();
|
10
|
11
|
|
11
|
|
- public void addEntry(String name, String number) {
|
12
|
|
- phoneBookList.put(name, number);
|
13
|
|
- //Look to see if person exists
|
14
|
|
- //Get array list value
|
|
12
|
+ public void addEntry(String name, String ...phoneNumber) {
|
|
13
|
+ List<String> numberList = new ArrayList<String>();
|
|
14
|
+ for(String number : phoneNumber) {
|
|
15
|
+ numberList.add(number);
|
|
16
|
+ }
|
|
17
|
+ phoneBookList.put(name, numberList);
|
15
|
18
|
}
|
16
|
19
|
|
17
|
|
- public void removeEntry(String name, String number) {
|
18
|
|
- phoneBookList.remove(name, number);
|
|
20
|
+ public void removeEntry(String name) {
|
|
21
|
+ phoneBookList.remove(name);
|
19
|
22
|
}
|
20
|
23
|
|
21
|
24
|
public String lookUp(String name) {
|
22
|
|
- return phoneBookList.get(name);
|
|
25
|
+ return phoneBookList.get(name).toString();
|
23
|
26
|
}
|
24
|
27
|
|
25
|
28
|
public String listNames() {
|
|
@@ -29,28 +32,29 @@ public class PhoneBook {
|
29
|
32
|
public String listAll() {
|
30
|
33
|
|
31
|
34
|
StringBuilder sb = new StringBuilder();
|
32
|
|
- for(Map.Entry<String, String> entry : phoneBookList.entrySet()) {
|
|
35
|
+ for(Map.Entry<String, List<String>> entry : phoneBookList.entrySet()) {
|
33
|
36
|
sb.append(entry.getKey() + entry.getValue());
|
34
|
37
|
sb.append("\n");
|
35
|
38
|
}
|
36
|
39
|
return sb.toString();
|
37
|
|
-
|
38
|
40
|
}
|
39
|
41
|
|
40
|
42
|
public String reverseLookUp(String number) {
|
41
|
|
- for(Map.Entry<String, String>entry : phoneBookList.entrySet()) {
|
42
|
|
- if(entry.getValue() == number) {
|
|
43
|
+ for(Map.Entry<String, List<String>> entry : phoneBookList.entrySet()) {
|
|
44
|
+ if(entry.getValue().contains(number)) {
|
43
|
45
|
return entry.getKey();
|
44
|
46
|
}
|
45
|
47
|
}
|
46
|
48
|
return "That number is not in the phone book";
|
47
|
49
|
}
|
48
|
50
|
|
49
|
|
- public static void main(String[] args) {
|
50
|
|
- PhoneBook pb1 = new PhoneBook();
|
51
|
|
- pb1.addEntry("Brian", "12345678");
|
52
|
|
- pb1.addEntry("Lawrence", "23458984");
|
53
|
|
- pb1.addEntry("Anthony", "12381927");
|
54
|
|
-
|
|
51
|
+ public void removeOneNumber(String name, String number) {
|
|
52
|
+ int i = 0;
|
|
53
|
+ for (String phoneNumber : phoneBookList.get(name)) {
|
|
54
|
+ if(phoneNumber == number) {
|
|
55
|
+ phoneBookList.get(name).remove(i);
|
|
56
|
+ }
|
|
57
|
+ i++;
|
|
58
|
+ }
|
55
|
59
|
}
|
56
|
60
|
}
|