|
@@ -1,9 +1,6 @@
|
1
|
1
|
package com.zipcodewilmington.phonebook;
|
2
|
2
|
|
3
|
|
-import java.util.Set;
|
4
|
|
-import java.util.TreeMap;
|
5
|
|
-import java.util.TreeSet;
|
6
|
|
-import java.util.Map;
|
|
3
|
+import java.util.*;
|
7
|
4
|
|
8
|
5
|
/**
|
9
|
6
|
* Created by leon on 1/23/18.
|
|
@@ -13,7 +10,7 @@ public class PhoneBook {
|
13
|
10
|
private String name;
|
14
|
11
|
private String number;
|
15
|
12
|
|
16
|
|
- TreeMap<String, String> myTree = new TreeMap<String, String>();
|
|
13
|
+ TreeMap<String, ArrayList<String>> myTree = new TreeMap<String, ArrayList<String>>();
|
17
|
14
|
|
18
|
15
|
|
19
|
16
|
public PhoneBook(){
|
|
@@ -21,30 +18,40 @@ public class PhoneBook {
|
21
|
18
|
}
|
22
|
19
|
|
23
|
20
|
|
24
|
|
- public void addEntry(String name, String number) {
|
25
|
|
- myTree.put(name, number);
|
26
|
|
-
|
|
21
|
+ public void addEntry(String name, String... number) {
|
|
22
|
+ ArrayList<String> listOfPhoneNumbers = new ArrayList<String>(Arrays.asList(number));
|
|
23
|
+ myTree.put(name, listOfPhoneNumbers);
|
27
|
24
|
|
28
|
25
|
}
|
29
|
26
|
|
|
27
|
+ public void additionalPhoneNumberToKey(String name, String number) {
|
|
28
|
+ myTree.get(name).add(number);
|
|
29
|
+ }
|
30
|
30
|
|
31
|
|
- public void remove(String name) {
|
32
|
|
- myTree.remove(name);
|
33
|
31
|
|
|
32
|
+ public void removeNumberFromName(String name, String number) {
|
|
33
|
+ myTree.get(name).remove(number);
|
34
|
34
|
}
|
35
|
35
|
|
36
|
|
- public String stringLookup(String name) {
|
37
|
36
|
|
38
|
|
- return myTree.get(name);
|
|
37
|
+ public void removeEntry(String name) {
|
|
38
|
+ myTree.remove(name);
|
39
|
39
|
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ public String lookupNumber(String name) {
|
|
43
|
+ ArrayList<String> matchingNumbers = myTree.get(name);
|
|
44
|
+ String listedNumbers = "";
|
|
45
|
+ for (String phoneNumbers : matchingNumbers) {
|
|
46
|
+ listedNumbers +=phoneNumbers + "\n";
|
|
47
|
+ }
|
|
48
|
+ return listedNumbers.trim();
|
40
|
49
|
|
41
|
50
|
|
42
|
51
|
}
|
43
|
52
|
|
44
|
53
|
public String listNames() {
|
45
|
54
|
|
46
|
|
-
|
47
|
|
-
|
48
|
55
|
Set<String> contacts = myTree.keySet();
|
49
|
56
|
|
50
|
57
|
String allNames = "";
|
|
@@ -52,18 +59,19 @@ public class PhoneBook {
|
52
|
59
|
allNames += contact + "\n";
|
53
|
60
|
|
54
|
61
|
}
|
55
|
|
- return allNames;
|
|
62
|
+ return allNames.trim();
|
56
|
63
|
}
|
57
|
64
|
|
58
|
65
|
public String listPhoneBook() {
|
59
|
66
|
|
|
67
|
+ Set<String> contacts = myTree.keySet();
|
60
|
68
|
StringBuilder printBook = new StringBuilder();
|
61
|
69
|
|
62
|
|
- for (Map.Entry<String, String> entry : myTree.entrySet()) {
|
63
|
|
- String key = entry.getKey();
|
64
|
|
- String value = entry.getValue();
|
65
|
|
-
|
66
|
|
- printBook.append(key).append(" = ").append(value).append("\n");
|
|
70
|
+ for (String name : contacts) {
|
|
71
|
+ printBook.append(name).append(": ");
|
|
72
|
+ for (int x = 0; x<myTree.get(name).size(); x++) {
|
|
73
|
+ printBook.append(x).append(" ");
|
|
74
|
+ } printBook.append("\n");
|
67
|
75
|
|
68
|
76
|
}
|
69
|
77
|
String result = printBook.toString();
|
|
@@ -73,13 +81,14 @@ public class PhoneBook {
|
73
|
81
|
|
74
|
82
|
public String reverseLookup(String number) {
|
75
|
83
|
|
76
|
|
- String getName = "";
|
77
|
|
- for (Map.Entry<String, String> entry : myTree.entrySet()) {
|
78
|
|
- if (entry.getValue() == number) {
|
79
|
|
- getName += entry.getKey();
|
80
|
|
- return getName;
|
81
|
|
- }
|
|
84
|
+ Set<String> contacts = myTree.keySet();
|
82
|
85
|
|
|
86
|
+ for (String name : contacts) {
|
|
87
|
+ for (int y = 0; y < myTree.get(name).size(); y++) {
|
|
88
|
+ if (myTree.get(name).get(y).equals(number)) {
|
|
89
|
+ return name;
|
|
90
|
+ }
|
|
91
|
+ }
|
83
|
92
|
}
|
84
|
93
|
|
85
|
94
|
|
|
@@ -105,7 +114,7 @@ public class PhoneBook {
|
105
|
114
|
|
106
|
115
|
|
107
|
116
|
phonebookEntry.listPhoneBook();
|
108
|
|
- System.out.println(phonebookEntry.reverseLookup("4899994190"));
|
|
117
|
+ System.out.println(phonebookEntry.listPhoneBook());
|
109
|
118
|
|
110
|
119
|
}
|
111
|
120
|
|