|
@@ -1,7 +1,113 @@
|
1
|
1
|
package com.zipcodewilmington.phonebook;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.Map;
|
|
5
|
+import java.util.TreeMap;
|
|
6
|
+
|
3
|
7
|
/**
|
4
|
8
|
* Created by leon on 1/23/18.
|
5
|
9
|
*/
|
6
|
10
|
public class PhoneBook {
|
7
|
|
-}
|
|
11
|
+ public static void main(String[] args) {
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+ PhoneBook phonebook = new PhoneBook();
|
|
18
|
+
|
|
19
|
+ phonebook.add("johnny", "302-555-6789");
|
|
20
|
+ phonebook.add("lewis black", "244-324-5590");
|
|
21
|
+ phonebook.add("johnny", "215-344-5645");
|
|
22
|
+ phonebook.add("sally fields", "214-335-4545");
|
|
23
|
+ phonebook.add("sally fields", "213-336-4588");
|
|
24
|
+ phonebook.add("jimbo", "210-2478-1111");
|
|
25
|
+ phonebook.add("eric idle", "215-2245-3323");
|
|
26
|
+ System.out.println(phonebook.listNamesAndNumbers());
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ private Map<String, ArrayList<String>> contacts;
|
|
32
|
+
|
|
33
|
+ public PhoneBook() {
|
|
34
|
+ contacts = new TreeMap<String, ArrayList<String>>();
|
|
35
|
+
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ public boolean hasEntry(String name) {
|
|
39
|
+
|
|
40
|
+ return contacts.containsKey(name);
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ public void add(String name, String phoneNumber) {
|
|
44
|
+ ArrayList<String> details = new ArrayList<>();
|
|
45
|
+ if (hasEntry(name)) {
|
|
46
|
+ details = contacts.get(name);
|
|
47
|
+ }
|
|
48
|
+ details.add(phoneNumber);
|
|
49
|
+ contacts.put(name, details);
|
|
50
|
+
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ public String lookup(String name) {
|
|
54
|
+ String s = "";
|
|
55
|
+ ArrayList<String> pnumber = new ArrayList<>();
|
|
56
|
+ pnumber = contacts.get(name);
|
|
57
|
+ // for (int i = 0; i < pnumber.size(); i++) {
|
|
58
|
+ // s += pnumber.get(i);
|
|
59
|
+ s += pnumber.get(0);
|
|
60
|
+ return s;
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+ public String reverseLookup(String phoneNumber) {
|
|
65
|
+ // if (contacts.containsValue(phoneNumber)) {
|
|
66
|
+ String status = "person not found!";
|
|
67
|
+ for (String name : contacts.keySet()) {
|
|
68
|
+ ArrayList<String> reverse = contacts.get(name);
|
|
69
|
+ for (String searching : reverse) {
|
|
70
|
+ if (searching.equals(phoneNumber)) {
|
|
71
|
+ status = name;
|
|
72
|
+ }
|
|
73
|
+ }
|
|
74
|
+ }
|
|
75
|
+ return status;
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ public void remove(String name) {
|
|
79
|
+ contacts.remove(name);
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ public void removeRecord(String name)
|
|
83
|
+ {
|
|
84
|
+ contacts.remove(name);
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ public void removePhoneNumber(String number) {
|
|
88
|
+ String srcName = reverseLookup(number);
|
|
89
|
+ ArrayList<String> newPhonelist = new ArrayList<>();
|
|
90
|
+ ArrayList<String> pnumber = new ArrayList<>();
|
|
91
|
+ pnumber = contacts.get(srcName);
|
|
92
|
+ for (int i = 0; i < pnumber.size(); i++) {
|
|
93
|
+ if (!(pnumber.get(i).equals(number))) {
|
|
94
|
+ newPhonelist.add(i, pnumber.get(i));
|
|
95
|
+ }
|
|
96
|
+ contacts.put(srcName, newPhonelist);
|
|
97
|
+ }
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ public String listNamesAndNumbers() {
|
|
101
|
+ {
|
|
102
|
+ String s = "";
|
|
103
|
+ //System.out.println(contacts.keySet());
|
|
104
|
+ for (String name : contacts.keySet()) {
|
|
105
|
+ ArrayList<String> phoneList = contacts.get(name);
|
|
106
|
+ for (int i = 0; i < phoneList.size(); i++) {
|
|
107
|
+ s += " " + name + " " + " " + phoneList.get(i) + "\n";
|
|
108
|
+ }
|
|
109
|
+ }
|
|
110
|
+ return(s);
|
|
111
|
+ }
|
|
112
|
+ }
|
|
113
|
+}
|