|
@@ -1,7 +1,96 @@
|
1
|
|
-
|
|
1
|
+import java.util.Iterator;
|
|
2
|
+import java.util.Map;
|
|
3
|
+import java.util.Set;
|
|
4
|
+import java.util.SortedMap;
|
|
5
|
+import java.util.TreeMap;
|
|
6
|
+import java.util.ArrayList;
|
2
|
7
|
|
3
|
|
-/**
|
4
|
|
- * Created by leon on 1/23/18.
|
5
|
|
- */
|
6
|
8
|
public class PhoneBook {
|
|
9
|
+ public TreeMap<String, ArrayList<String>> bookEntries;
|
|
10
|
+
|
|
11
|
+ public PhoneBook (){
|
|
12
|
+ this.bookEntries = new TreeMap<String, ArrayList<String>>();
|
|
13
|
+ }
|
|
14
|
+
|
|
15
|
+ public void add(String name, String phoneNumber) {
|
|
16
|
+ ArrayList<String> addMe = new ArrayList<String>();
|
|
17
|
+ if (bookEntries.containsKey(name)) {
|
|
18
|
+ addMe = bookEntries.get(name);
|
|
19
|
+ }
|
|
20
|
+ addMe.add(phoneNumber);
|
|
21
|
+ bookEntries.put(name, addMe);
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ public void remove(String name, String phoneNumber) {
|
|
25
|
+ ArrayList listOfNumbers = new ArrayList();
|
|
26
|
+ if (bookEntries.containsKey(name)) {
|
|
27
|
+ listOfNumbers = bookEntries.get(name);
|
|
28
|
+ listOfNumbers.remove(phoneNumber);
|
|
29
|
+ }
|
|
30
|
+ if (listOfNumbers.size() == 0) {
|
|
31
|
+ bookEntries.remove(name);
|
|
32
|
+ }
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ public void removeRecord(String name) {
|
|
36
|
+ if (bookEntries.containsKey(name)) {
|
|
37
|
+ bookEntries.remove(name);
|
|
38
|
+ }
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ public String lookup(String name) {
|
|
42
|
+ String phoneNumber = "This entry doesn't exist. Please enter valid entry.";
|
|
43
|
+ if (bookEntries.containsKey(name)) {
|
|
44
|
+ phoneNumber = bookEntries.get(name).toString().replace("[", "").replace("]", "");
|
|
45
|
+ }
|
|
46
|
+ return phoneNumber;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ public String reverseLookup(String phoneNumber) {
|
|
50
|
+ String name = "This entry doesn't exist. Please enter valid entry.";
|
|
51
|
+ Set<String> keys = bookEntries.keySet();
|
|
52
|
+ for(String key: keys){
|
|
53
|
+ if(bookEntries.get(key).contains(phoneNumber) == true) {
|
|
54
|
+ name = key;
|
|
55
|
+ }
|
|
56
|
+ }
|
|
57
|
+ return name;
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ public void display() {
|
|
61
|
+ Set<String> keys = bookEntries.keySet();
|
|
62
|
+ for(String key: keys){
|
|
63
|
+ System.out.println(key + ": " + bookEntries.get(key).toString().replace("[", "").replace("]", ""));
|
|
64
|
+ }
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ public void main(String[] args) {
|
|
68
|
+ PhoneBook myBook = new PhoneBook();
|
|
69
|
+ myBook.add("Tomato", "267-945-5231");
|
|
70
|
+ myBook.add("Asparagus", "305-504-1159");
|
|
71
|
+ myBook.add("Eggplant", "123-345-6789");
|
|
72
|
+ myBook.add("Beet", "233-464-1289");
|
|
73
|
+ myBook.add("Asparagus", "454-345-4566");
|
|
74
|
+ myBook.add("Leeks", "194-345-3458");
|
|
75
|
+ myBook.add("Leeks", "345-209-1095");
|
|
76
|
+ myBook.display();
|
|
77
|
+
|
|
78
|
+ System.out.println("***");
|
|
79
|
+ myBook.remove("Asparagus", "305-504-1159");
|
|
80
|
+ myBook.removeRecord("Beet");
|
|
81
|
+ System.out.println("Phonebook after removing Asparagus' number and Beets' record");
|
|
82
|
+ myBook.display();
|
|
83
|
+
|
|
84
|
+ System.out.println("***");
|
|
85
|
+ System.out.println("Looking for Leeks' phone number");
|
|
86
|
+ System.out.println(myBook.lookup("Leeks"));
|
|
87
|
+
|
|
88
|
+ System.out.println("***");
|
|
89
|
+ System.out.println("Looking for the owner of number 454-345-4566");
|
|
90
|
+ System.out.println(myBook.reverseLookup("454-345-4566"));
|
|
91
|
+
|
|
92
|
+ System.out.println("***");
|
|
93
|
+ System.out.println("Looking for Banana's phone number");
|
|
94
|
+ System.out.println(myBook.lookup("Banana"));
|
|
95
|
+ }
|
7
|
96
|
}
|