|
@@ -24,45 +24,34 @@ public class PhoneBook {
|
24
|
24
|
|
25
|
25
|
//add a name & number entry
|
26
|
26
|
public void add(String name, String number) {
|
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";
|
|
27
|
+ if(!treeMap.containsKey(name)) {
|
|
28
|
+ treeMap.put(name, number);
|
33
|
29
|
}
|
34
|
30
|
}
|
35
|
31
|
|
36
|
32
|
//remove a name & number entry
|
37
|
|
- public String remove(String name) {
|
38
|
|
- treeMap.remove(name);
|
39
|
|
- String response = "";
|
40
|
|
- if(treeMap.remove(name) != null) {
|
41
|
|
- response = "Name and number removed";
|
|
33
|
+ public void remove(String name) {
|
|
34
|
+ if(treeMap.containsKey(name)) {
|
|
35
|
+ treeMap.remove(name);
|
42
|
36
|
}
|
43
|
|
- if(treeMap.remove(name)== null) {
|
44
|
|
- response = "Invalid entry";
|
45
|
|
- }
|
46
|
|
- return response;
|
47
|
37
|
}
|
48
|
38
|
|
49
|
39
|
//find phone number lookup by 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
|
|
- }
|
|
40
|
+ public void lookup(String name) {
|
|
41
|
+ if(treeMap.containsKey(name)) {
|
|
42
|
+ treeMap.get(name);
|
|
43
|
+ }
|
|
44
|
+
|
59
|
45
|
}
|
60
|
46
|
|
61
|
47
|
//print out all of the entries in PhoneBook
|
62
|
48
|
//need to work on code for formatting
|
63
|
|
- public String display() {
|
64
|
|
- System.out.println(treeMap.entrySet().toString());
|
65
|
|
- String printOut = treeMap.entrySet().toString();
|
|
49
|
+ public String display(){
|
|
50
|
+ String printOut = "";
|
|
51
|
+ for(Map.Entry<String, String> entry: treeMap.entrySet()) {
|
|
52
|
+ printOut += entry.getKey() + " " + entry.getValue() + "\n";
|
|
53
|
+ }
|
|
54
|
+ System.out.println(printOut);
|
66
|
55
|
return printOut;
|
67
|
56
|
}
|
68
|
57
|
|