|
@@ -1,7 +1,106 @@
|
1
|
1
|
package com.zipcodewilmington.phonebook;
|
2
|
2
|
|
|
3
|
+import java.lang.reflect.Array;
|
|
4
|
+import java.util.ArrayList;
|
|
5
|
+import java.util.Collections;
|
|
6
|
+import java.util.HashMap;
|
|
7
|
+import java.util.List;
|
|
8
|
+
|
3
|
9
|
/**
|
4
|
10
|
* Created by leon on 1/23/18.
|
5
|
11
|
*/
|
6
|
12
|
public class PhoneBook {
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+ public HashMap<String, String> contacts;
|
|
16
|
+
|
|
17
|
+ public PhoneBook (){
|
|
18
|
+
|
|
19
|
+ contacts = new HashMap<>();
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ //public void setContacts(HashMap<String, String> contacts) {
|
|
23
|
+ public void setContacts(String name, String number){
|
|
24
|
+ //this.contacts = contacts;
|
|
25
|
+ this.contacts.put(name, number);
|
|
26
|
+ System.out.println(this.contacts);
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ public void addEntry(String name, String phoneNumber){
|
|
30
|
+
|
|
31
|
+ contacts.put(name,phoneNumber);
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+ public boolean hasName(String name){
|
|
36
|
+ if(contacts.containsKey(name)){
|
|
37
|
+ return true;
|
|
38
|
+ } else {
|
|
39
|
+ return false;
|
|
40
|
+ }
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ public boolean hasNumber(String contactNumber){
|
|
44
|
+ if(contacts.containsValue(contactNumber)){
|
|
45
|
+ return true;
|
|
46
|
+ } else{
|
|
47
|
+ return false;
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ //public void addNumber()
|
|
53
|
+
|
|
54
|
+ public HashMap<String, String> getContacts() {
|
|
55
|
+
|
|
56
|
+ return contacts;
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ public boolean removeEntry(String name, String phoneNumber){
|
|
60
|
+
|
|
61
|
+ return contacts.remove(name, phoneNumber);
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ public String removeEntry(String name){
|
|
65
|
+ return contacts.remove(name);
|
|
66
|
+
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ public String reverseLookUp(String phoneNumber) {
|
|
70
|
+
|
|
71
|
+ for (String name : contacts.keySet()) {
|
|
72
|
+// contacts.get(name);
|
|
73
|
+ if (contacts.get(name).equals(phoneNumber)) {
|
|
74
|
+ return name;
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+ return null;
|
|
78
|
+// for(int i = 0; i < contacts.size(); i++){
|
|
79
|
+// if(contacts.containsValue(phoneNumber)){
|
|
80
|
+// return contacts.get()
|
|
81
|
+// }
|
|
82
|
+// }
|
|
83
|
+// return contacts.get(getContacts(phoneNumber));
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ public String listNamesAndNumbers(){
|
|
87
|
+ List<String> contactList = new ArrayList(contacts.keySet());
|
|
88
|
+ //System.out.println(contacts + "HERE'S THE CONTACTS");
|
|
89
|
+ Collections.sort(contactList);
|
|
90
|
+
|
|
91
|
+ String sortedNamesWithNumbers = " ";
|
|
92
|
+ for(String contactName : contactList){
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+ sortedNamesWithNumbers += contactName + " ";
|
|
96
|
+ sortedNamesWithNumbers += contacts.get(contactName);
|
|
97
|
+
|
|
98
|
+ sortedNamesWithNumbers += " \n";
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+ }
|
|
102
|
+ System.out.println(sortedNamesWithNumbers);
|
|
103
|
+
|
|
104
|
+ return sortedNamesWithNumbers;
|
|
105
|
+ }
|
7
|
106
|
}
|