|
@@ -7,13 +7,13 @@ public class PhoneBook {
|
7
|
7
|
|
8
|
8
|
Map<String, ArrayList<String> > entries = new HashMap<String, ArrayList<String>>();
|
9
|
9
|
|
10
|
|
- public void add (String name, String phoneNumber){
|
|
10
|
+ public void addEntry(String name, String phoneNumber){
|
11
|
11
|
ArrayList<String> pn = new ArrayList<String>();
|
12
|
12
|
pn.add(phoneNumber);
|
13
|
13
|
this.entries.put(name, pn);
|
14
|
14
|
}
|
15
|
15
|
|
16
|
|
- public void addNumber(String name, String phoneNumber){
|
|
16
|
+ public void addNumberTo(String name, String phoneNumber){
|
17
|
17
|
for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
|
18
|
18
|
if(name.equals(entry.getKey())){
|
19
|
19
|
entry.getValue().add(phoneNumber);
|
|
@@ -22,46 +22,63 @@ public class PhoneBook {
|
22
|
22
|
}
|
23
|
23
|
}
|
24
|
24
|
|
25
|
|
- public void remove(String name, String phoneNumber){
|
|
25
|
+ public void removePhoneNumber(String name, String phoneNumber){
|
|
26
|
+ for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
|
|
27
|
+ if(name.equals(entry.getKey())){
|
|
28
|
+ int indexOf = entry.getValue().indexOf(phoneNumber);
|
|
29
|
+ if(indexOf >= 0){
|
|
30
|
+ entry.getValue().remove(indexOf);
|
|
31
|
+ break;
|
|
32
|
+ }
|
|
33
|
+ }
|
|
34
|
+ }
|
26
|
35
|
}
|
27
|
36
|
|
28
|
37
|
public void removeRecord(String name){
|
29
|
38
|
this.entries.remove(name);
|
30
|
39
|
}
|
31
|
40
|
|
32
|
|
-
|
33
|
|
-
|
34
|
|
- public void lookUp(String name){
|
35
|
|
- System.out.print(this.entries.get(name));
|
|
41
|
+ public String lookUp(String name){
|
|
42
|
+ StringBuilder sb = new StringBuilder();
|
|
43
|
+ for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
|
|
44
|
+ if(entry.getKey().equals(name)){
|
|
45
|
+ for(String pn : entry.getValue()){
|
|
46
|
+ sb.append(pn);
|
|
47
|
+ }
|
|
48
|
+ }
|
|
49
|
+ }
|
|
50
|
+ return sb.toString();
|
|
51
|
+
|
36
|
52
|
}
|
37
|
53
|
|
38
|
54
|
public String reverseLoopup(String phoneNumber){
|
39
|
|
- String name = "No Records...";
|
|
55
|
+ String name = null;
|
40
|
56
|
for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
|
41
|
|
-
|
42
|
57
|
for(String pn : entry.getValue()){
|
43
|
58
|
if(pn.equals(phoneNumber)){
|
44
|
59
|
name = entry.getKey();
|
45
|
60
|
}
|
46
|
61
|
}
|
47
|
|
-
|
48
|
62
|
}
|
49
|
63
|
return name;
|
50
|
|
-
|
51
|
64
|
}
|
52
|
65
|
|
53
|
66
|
|
54
|
|
- public void display() {
|
|
67
|
+ public String display() {
|
55
|
68
|
StringBuilder sb = new StringBuilder();
|
56
|
69
|
|
57
|
70
|
for(Map.Entry<String, ArrayList<String>> entry :this.entries.entrySet()){
|
58
|
71
|
sb.append(entry.getKey());
|
59
|
|
- sb.append(" - ");
|
60
|
|
- sb.append(entry.getValue());
|
61
|
|
- sb.append("\n");
|
|
72
|
+ sb.append(": ");
|
|
73
|
+
|
|
74
|
+ for(String pn : entry.getValue()){
|
|
75
|
+ sb.append(pn);
|
|
76
|
+ //sb.append(" ");
|
|
77
|
+ }
|
|
78
|
+
|
62
|
79
|
}
|
63
|
80
|
|
64
|
|
- System.out.print(sb.toString());
|
|
81
|
+ return sb.toString();
|
65
|
82
|
}
|
66
|
83
|
|
67
|
84
|
|