|
@@ -1,90 +1,62 @@
|
1
|
1
|
package com.zipcodewilmington.phonebook;
|
2
|
2
|
|
|
3
|
+import com.sun.tools.doclets.formats.html.SourceToHTMLConverter;
|
|
4
|
+
|
3
|
5
|
import java.util.ArrayList;
|
4
|
6
|
import java.util.Map;
|
5
|
7
|
import java.util.Set;
|
6
|
8
|
import java.util.TreeMap;
|
7
|
9
|
|
|
10
|
+//phoneBook class is like the cover of the book.
|
8
|
11
|
public class PhoneBook {
|
9
|
12
|
|
10
|
|
- //the below defines what kind of attribute you have, which in this case is your "contacts"
|
11
|
|
- TreeMap<String, String> personMap;
|
12
|
|
-
|
13
|
|
- String name = "";
|
14
|
|
-
|
15
|
|
- //this is your constructor. whenever you see public + the classname, that is your constructor
|
16
|
|
- //
|
17
|
|
- // (this phoneBook constructor is like the "cover" of your phonebook (the binding and the spine). it creates an empty book.)
|
18
|
|
-
|
19
|
|
- //this can make as many phonebook objects as you want
|
20
|
|
- public PhoneBook() {
|
21
|
|
-
|
22
|
|
- //when we make our phonebooks, this is the form they will take (below)
|
23
|
|
- //
|
24
|
|
- // this treeMap initializes the "blank pages of the book". the treemap is the structure of how your book will hold info
|
|
13
|
+ public static void main(String[] args) {
|
25
|
14
|
|
26
|
|
- //if you were to not set this, your person map would be null(default setting)
|
27
|
|
- this.personMap = new TreeMap<String, String>();
|
|
15
|
+ PhoneBook myPhoneBookActualObjectInstance= new PhoneBook();
|
|
16
|
+ myPhoneBookActualObjectInstance.add("vince", "1123456789");
|
|
17
|
+ myPhoneBookActualObjectInstance.add("amy", "29387429");
|
|
18
|
+ System.out.println(myPhoneBookActualObjectInstance.showNames());
|
28
|
19
|
}
|
29
|
20
|
|
30
|
|
- // this is our method below. it describes what we can do with this phonebook
|
31
|
21
|
|
32
|
|
- //so later, when you call "phonebook.add", that's like someone literally writing a new name and number in your phonebook object
|
|
22
|
+ /*the treeMap is like the t structure with key/values. key is person's name in this example, value is the arraylist
|
|
23
|
+ which can hold multiple numbers, fields etc.
|
|
24
|
+ */
|
|
25
|
+ TreeMap<String, Person> personTreeMap = new TreeMap<String, Person>();
|
33
|
26
|
|
|
27
|
+ public PhoneBook() {
|
34
|
28
|
|
35
|
|
- public void add(String name, String number) {
|
36
|
|
- personMap.put(name, number);
|
37
|
29
|
}
|
38
|
30
|
|
39
|
|
- /*
|
40
|
|
- //this method would really be better for if you need more information on your person objects (like address, etc).
|
41
|
|
-
|
42
|
|
- public void add(Person person){
|
43
|
|
- this.personMap.put(person.getName(), person);
|
|
31
|
+ public void add(String name, String number) {
|
|
32
|
+ Person phoneBookEntry = new Person(name, number);
|
|
33
|
+ personTreeMap.put(name, phoneBookEntry);
|
44
|
34
|
}
|
45
|
35
|
|
46
|
|
- */
|
47
|
36
|
|
48
|
|
- public void remove(String name, String number) {
|
|
37
|
+ public void remove(String name) {
|
49
|
38
|
|
50
|
|
- personMap.remove(name);
|
|
39
|
+ personTreeMap.remove(name);
|
51
|
40
|
}
|
52
|
41
|
|
53
|
42
|
public String lookup(String name) {
|
54
|
43
|
|
55
|
|
- return personMap.get(name);
|
|
44
|
+ return personTreeMap.get(name).getNumbers();
|
56
|
45
|
|
57
|
46
|
}
|
58
|
47
|
|
59
|
|
- /* public String reverseLookup (String phoneNumber){
|
60
|
|
-
|
61
|
|
- //this will "grab" all the listings in your book. it does NOT print them.
|
62
|
|
- Set<Map.Entry<String, String> = personMap.entrySet();
|
63
|
|
-
|
64
|
|
- for (Map.Entry<String, String> phoneBookEntry : entries) {
|
|
48
|
+ public String showNames() {
|
65
|
49
|
|
66
|
|
- Person person = phoneBookEntry.getValue();
|
67
|
|
-
|
68
|
|
- if (person.getPhoneNumber().equals(phoneNumber)){
|
69
|
|
- return phoneBookEntry.getKey();
|
|
50
|
+ String phoneList = "";
|
|
51
|
+ Set<String> unsortedKeys = personTreeMap.keySet();
|
|
52
|
+ for (String personsName : unsortedKeys) {
|
|
53
|
+ phoneList += (personsName + "\n");
|
70
|
54
|
}
|
71
|
|
- }
|
72
|
55
|
|
73
|
|
- return "We're sorry. The requested phone number could not be found. Have fun when you code!!!!!!!!";
|
|
56
|
+ return phoneList;
|
74
|
57
|
|
75
|
|
- }
|
76
|
|
-
|
77
|
|
- */
|
78
|
|
-
|
79
|
|
- public void displayAllEntries() {
|
80
|
|
-
|
81
|
|
- for (Map.Entry<String, String> entry : personMap.entrySet()) {
|
82
|
|
- String name = entry.getKey();
|
83
|
|
- String number = entry.getValue();
|
84
|
58
|
}
|
85
|
59
|
|
86
|
60
|
}
|
87
|
61
|
|
88
|
62
|
|
89
|
|
-}
|
90
|
|
-
|