|
@@ -8,6 +8,7 @@ import java.util.Map;
|
8
|
8
|
* Created by leon on 1/23/18.
|
9
|
9
|
*/
|
10
|
10
|
public class PhoneBook {
|
|
11
|
+
|
11
|
12
|
//instance variable
|
12
|
13
|
private TreeMap<String, String> treeMap;
|
13
|
14
|
|
|
@@ -22,25 +23,50 @@ public class PhoneBook {
|
22
|
23
|
}
|
23
|
24
|
|
24
|
25
|
//add a name & number entry
|
25
|
|
- public void addNameNumberEntry(String name, String number) {
|
|
26
|
+ public void add(String name, String number) {
|
26
|
27
|
treeMap.put(name, number);
|
|
28
|
+ String confirm = "";
|
|
29
|
+ if(treeMap.put(name, number)!= null) {
|
|
30
|
+ confirm = name + " and " + number + " are now in the database";
|
|
31
|
+ } else {
|
|
32
|
+ confirm = "Error: " + name + " is already in the database";
|
|
33
|
+ }
|
27
|
34
|
}
|
28
|
35
|
|
29
|
36
|
//remove a name & number entry
|
30
|
|
- public void removeEntry(String name) {
|
|
37
|
+ public String remove(String name) {
|
31
|
38
|
treeMap.remove(name);
|
|
39
|
+ String response = "";
|
|
40
|
+ if(treeMap.remove(name) != null) {
|
|
41
|
+ response = "Name and number removed";
|
|
42
|
+ }
|
|
43
|
+ if(treeMap.remove(name)== null) {
|
|
44
|
+ response = "Invalid entry";
|
|
45
|
+ }
|
|
46
|
+ return response;
|
32
|
47
|
}
|
33
|
48
|
|
34
|
49
|
//find phone number lookup by name
|
35
|
|
- public String lookupByName(String name) {
|
36
|
|
- return treeMap.get(name);
|
|
50
|
+ public String lookup(String name) {
|
|
51
|
+ String lookup = treeMap.get(name);
|
|
52
|
+ String notFound = name + " is not in the directory";
|
|
53
|
+ if(lookup != null) {
|
|
54
|
+ return lookup;
|
|
55
|
+ }
|
|
56
|
+ else {
|
|
57
|
+ return notFound;
|
|
58
|
+ }
|
37
|
59
|
}
|
38
|
60
|
|
39
|
61
|
//print out all of the entries in PhoneBook
|
40
|
|
- public Set<Map.Entry<String,String>> printOutPhoneBook() {
|
41
|
|
- return treeMap.entrySet();
|
|
62
|
+ //need to work on code for formatting
|
|
63
|
+ public String display() {
|
|
64
|
+ System.out.println(treeMap.entrySet().toString());
|
|
65
|
+ String printOut = treeMap.entrySet().toString();
|
|
66
|
+ return printOut;
|
42
|
67
|
}
|
43
|
68
|
|
44
|
69
|
|
45
|
70
|
|
46
|
71
|
}
|
|
72
|
+
|