|
@@ -1,25 +1,68 @@
|
1
|
1
|
package com.zipcodewilmington.phonebook;
|
2
|
2
|
|
3
|
3
|
import java.util.Map;
|
|
4
|
+import java.util.Set;
|
4
|
5
|
import java.util.TreeMap;
|
5
|
6
|
|
6
|
7
|
public class PhoneBook {
|
|
8
|
+
|
|
9
|
+ //the below defines what kind of attribute you have, which in this case is your "contacts"
|
7
|
10
|
Map<String, Person> personMap;
|
8
|
11
|
|
|
12
|
+ //this is your constructor. whenever you see public + the classname, that is your constructor
|
|
13
|
+ //
|
|
14
|
+ // (this phoneBook constructor is like the "cover" of your phonebook (the binding and the spine). it creates an empty book.)
|
|
15
|
+
|
|
16
|
+ //this can make as many phonebook objects as you want
|
9
|
17
|
public PhoneBook() {
|
|
18
|
+
|
|
19
|
+ //when we make our phonebooks, this is the form they will take (below)
|
|
20
|
+ //
|
|
21
|
+ // this treeMap initializes the "blank pages of the book". the treemap is the structure of how your book will hold info
|
|
22
|
+
|
|
23
|
+ //if you were to not set this, your person map would be null(default setting)
|
10
|
24
|
this.personMap = new TreeMap<String, Person>();
|
11
|
25
|
}
|
12
|
26
|
|
|
27
|
+ // this is our method below. it describes what we can do with this phonebook
|
|
28
|
+
|
|
29
|
+ //so later, when you call "phonebook.add", that's like someone literally writing a new name and number in your phonebook object
|
13
|
30
|
public void add(String name, String number){
|
14
|
31
|
this.personMap.put(name, new Person(name, number));
|
15
|
32
|
}
|
16
|
33
|
|
|
34
|
+ /*
|
|
35
|
+ //this method would really be better for if you need more information on your person objects (like address, etc).
|
|
36
|
+
|
17
|
37
|
public void add(Person person){
|
18
|
38
|
this.personMap.put(person.getName(), person);
|
19
|
39
|
}
|
20
|
40
|
|
21
|
|
- public String lookup(String name) {
|
|
41
|
+ */
|
|
42
|
+
|
|
43
|
+ public Person lookup(String name) {
|
|
44
|
+
|
|
45
|
+ return personMap.get(name);
|
22
|
46
|
|
23
|
47
|
}
|
|
48
|
+
|
|
49
|
+ public String reverseLookup (String phoneNumber){
|
|
50
|
+
|
|
51
|
+ //this will "grab" all the listings in your book. it does NOT print them.
|
|
52
|
+ Set<Map.Entry<String, Person>> entries = personMap.entrySet();
|
|
53
|
+
|
|
54
|
+ for (Map.Entry<String, Person> phoneBookEntry : entries) {
|
|
55
|
+
|
|
56
|
+ Person person = phoneBookEntry.getValue();
|
|
57
|
+
|
|
58
|
+ if (person.getPhoneNumber().equals(phoneNumber)){
|
|
59
|
+ return phoneBookEntry.getKey();
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ return "We're sorry. The requested phone number could not be found. Have fun when you code!!!!!!!!";
|
|
64
|
+
|
|
65
|
+ }
|
|
66
|
+
|
24
|
67
|
}
|
25
|
68
|
|