|
@@ -1,6 +1,5 @@
|
1
|
1
|
package com.zipcodewilmington.phonebook;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
3
|
import java.util.Map;
|
5
|
4
|
import java.util.Set;
|
6
|
5
|
import java.util.TreeMap;
|
|
@@ -10,45 +9,58 @@ import java.util.TreeMap;
|
10
|
9
|
*/
|
11
|
10
|
public class PhoneBook {
|
12
|
11
|
|
13
|
|
- TreeMap<String, Person> phoneBook = new TreeMap<String, Person>();
|
|
12
|
+ TreeMap<String, String> phoneBook = new TreeMap<String, String>();
|
14
|
13
|
|
15
|
14
|
|
16
|
15
|
public void addEntryToPhoneBook(String name, String phoneNumber) {
|
17
|
|
- phoneBook.put(name, new Person(name, phoneNumber));
|
|
16
|
+ phoneBook.put(name, phoneNumber);
|
18
|
17
|
}
|
19
|
18
|
|
20
|
19
|
|
21
|
|
- public void removePhoneBookEntryFromPhoneBook(String name) {
|
22
|
|
- phoneBook.remove(name);
|
|
20
|
+ public void removeEntryFromPhoneBook(String name, String phoneNumber) {
|
|
21
|
+ phoneBook.remove(name, phoneNumber);
|
23
|
22
|
}
|
24
|
23
|
|
25
|
|
- public String[] listNames() {
|
26
|
|
- return null;
|
|
24
|
+ public String lookUp(String name) {
|
|
25
|
+ return phoneBook.get(name);
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ public String listNames() {
|
|
29
|
+ String completeListOfNames = "";
|
|
30
|
+ for (String key : phoneBook.keySet()) {
|
|
31
|
+ completeListOfNames += key + "\n";
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ return completeListOfNames;
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ public String listNumbers() {
|
|
38
|
+ String completeListOfNumbers = "";
|
|
39
|
+ for (Map.Entry<String, String> entry : phoneBook.entrySet()) {
|
|
40
|
+ completeListOfNumbers += entry.getValue() + "\n";
|
|
41
|
+ }
|
|
42
|
+ return completeListOfNumbers;
|
27
|
43
|
}
|
28
|
44
|
|
|
45
|
+
|
29
|
46
|
public String entryListAll() {
|
30
|
47
|
String fullList = "";
|
31
|
|
- Set<String> keys = phoneBook.keySet();
|
32
|
|
- for (Map.Entry<String, Person> entry: phoneBook.entrySet()) {
|
33
|
|
- fullList += entry.getKey() + " : " + entry.getValue().getPhoneNumber();
|
|
48
|
+ for (Map.Entry<String, String> entry : phoneBook.entrySet()) {
|
|
49
|
+ fullList += entry.getKey() + " : " + entry.getValue() + "\n"; //reverse lookup
|
34
|
50
|
|
35
|
51
|
}
|
36
|
52
|
return fullList;
|
37
|
53
|
}
|
38
|
|
- public static void main(String[] args) {
|
39
|
|
- PhoneBook nameNumber = new PhoneBook();
|
40
|
|
- nameNumber.addEntryToPhoneBook("eric", "3025551111 \n");
|
41
|
|
- nameNumber.addEntryToPhoneBook("eyan", "3025552222 \n");
|
42
|
|
- nameNumber.entryListAll();
|
43
|
|
- System.out.println(nameNumber.entryListAll());
|
44
|
54
|
|
|
55
|
+ public String reverseLookup(String numberToLookUp) {
|
|
56
|
+ for (Map.Entry<String,String > e : phoneBook.entrySet()) {
|
|
57
|
+ if (e.getValue().equals(numberToLookUp)){
|
|
58
|
+ return e.getKey();
|
|
59
|
+ }
|
|
60
|
+ }return null;
|
45
|
61
|
}
|
46
|
62
|
|
47
|
63
|
|
48
|
|
- public Person lookUp(String name) {
|
49
|
|
- return phoneBook.get(name);
|
50
|
|
-
|
51
|
|
- }
|
52
|
64
|
|
53
|
65
|
|
54
|
66
|
}
|