Browse Source

Finished all, replaced HashMap with TreeMap

Luis Romero 6 years ago
parent
commit
06060978fd

+ 13
- 13
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

1
 package com.zipcodewilmington.phonebook;
1
 package com.zipcodewilmington.phonebook;
2
 
2
 
3
 import java.util.Map;
3
 import java.util.Map;
4
-import java.util.HashMap;
4
+import java.util.TreeMap;
5
 import java.util.ArrayList;
5
 import java.util.ArrayList;
6
 import java.util.Set;
6
 import java.util.Set;
7
 
7
 
10
  */
10
  */
11
 public class PhoneBook {
11
 public class PhoneBook {
12
 
12
 
13
-    protected HashMap<String, Person> hashMap;
13
+    protected TreeMap<String, Person> treeMap;
14
     protected ArrayList<String> entryList;
14
     protected ArrayList<String> entryList;
15
     protected Set<String> keySet;
15
     protected Set<String> keySet;
16
 
16
 
17
     public PhoneBook() {
17
     public PhoneBook() {
18
-        this.hashMap = new HashMap<String, Person>();
18
+        this.treeMap = new TreeMap<String, Person>();
19
         this.entryList = new ArrayList<String>();
19
         this.entryList = new ArrayList<String>();
20
-        this.keySet = hashMap.keySet();
20
+        this.keySet = treeMap.keySet();
21
         //this.hashMap.put("Name_empty", new Person("Name_empty", "Phone_number_empty"));
21
         //this.hashMap.put("Name_empty", new Person("Name_empty", "Phone_number_empty"));
22
     }
22
     }
23
 
23
 
24
     //PART I
24
     //PART I
25
     public Person lookup(String name) {
25
     public Person lookup(String name) {
26
-        return hashMap.get(name);
26
+        return treeMap.get(name);
27
     }
27
     }
28
 
28
 
29
     public void addEntry(String name, String... phoneNumbers) {
29
     public void addEntry(String name, String... phoneNumbers) {
30
-        hashMap.put(name, new Person(name, phoneNumbers));
30
+        treeMap.put(name, new Person(name, phoneNumbers));
31
     }
31
     }
32
 
32
 
33
     // Changed method name for Part III
33
     // Changed method name for Part III
34
     public void removeRecord(String name) {
34
     public void removeRecord(String name) {
35
-        hashMap.remove(name);
35
+        treeMap.remove(name);
36
     }
36
     }
37
 
37
 
38
     public Set<String> getKeySet() { //names and phone numbers
38
     public Set<String> getKeySet() { //names and phone numbers
39
-        return hashMap.keySet();
39
+        return treeMap.keySet();
40
         //return all persons
40
         //return all persons
41
     }
41
     }
42
 
42
 
43
     public StringBuilder displayAllEntries() { //names and phone numbers
43
     public StringBuilder displayAllEntries() { //names and phone numbers
44
         StringBuilder sb = new StringBuilder();
44
         StringBuilder sb = new StringBuilder();
45
 
45
 
46
-        for (String key : hashMap.keySet()) {
47
-            sb.append(hashMap.get(key).getName() + "\n");
48
-            sb.append(hashMap.get(key).getPhoneNumberListString());
46
+        for (String key : treeMap.keySet()) {
47
+            sb.append(treeMap.get(key).getName() + "\n");
48
+            sb.append(treeMap.get(key).getPhoneNumberListString());
49
         }
49
         }
50
 
50
 
51
         return sb;
51
         return sb;
54
 
54
 
55
     //PART II
55
     //PART II
56
     public String reverseLookup(String phoneNumber) { //lookup names from phone numbers
56
     public String reverseLookup(String phoneNumber) { //lookup names from phone numbers
57
-        for (String name : hashMap.keySet()) {
58
-            if (hashMap.get(name).phoneNumberList.contains(phoneNumber) == true) {
57
+        for (String name : treeMap.keySet()) {
58
+            if (treeMap.get(name).phoneNumberList.contains(phoneNumber) == true) {
59
                 return name;
59
                 return name;
60
                 //break;
60
                 //break;
61
             }
61
             }

+ 14
- 14
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

32
         String actualPersonName = expectedPerson.getName();
32
         String actualPersonName = expectedPerson.getName();
33
         String actualPersonPhoneNumber = expectedPerson.phoneNumberList.get(0);
33
         String actualPersonPhoneNumber = expectedPerson.phoneNumberList.get(0);
34
 
34
 
35
-        boolean actualPersonNameExists = this.phonebook.hashMap.containsKey("Jane");
35
+        boolean actualPersonNameExists = this.phonebook.treeMap.containsKey("Jane");
36
 
36
 
37
         Assert.assertTrue(actualPersonNameExists);
37
         Assert.assertTrue(actualPersonNameExists);
38
     }
38
     }
42
     public void addEntryTest() {
42
     public void addEntryTest() {
43
         this.phonebook.addEntry("Adam", "(000) 000-0000", "(111) 111-1111");
43
         this.phonebook.addEntry("Adam", "(000) 000-0000", "(111) 111-1111");
44
 
44
 
45
-        boolean actualPersonName = this.phonebook.hashMap.containsKey("Adam");
46
-        boolean actualPhoneNumber = this.phonebook.hashMap.containsValue(this.phonebook.hashMap.get("Adam"));
45
+        boolean actualPersonName = this.phonebook.treeMap.containsKey("Adam");
46
+        boolean actualPhoneNumber = this.phonebook.treeMap.containsValue(this.phonebook.treeMap.get("Adam"));
47
         Assert.assertTrue(actualPersonName);
47
         Assert.assertTrue(actualPersonName);
48
         Assert.assertTrue(actualPhoneNumber);
48
         Assert.assertTrue(actualPhoneNumber);
49
 
49
 
50
         String expectedPhoneNumber1 = "(000) 000-0000";
50
         String expectedPhoneNumber1 = "(000) 000-0000";
51
         String expectedPhoneNumber2 = "(111) 111-1111";
51
         String expectedPhoneNumber2 = "(111) 111-1111";
52
 
52
 
53
-        String actualPhoneNumber1 = this.phonebook.hashMap.get("Adam").phoneNumberList.get(0);
54
-        String actualPhoneNumber2 = this.phonebook.hashMap.get("Adam").phoneNumberList.get(1);
53
+        String actualPhoneNumber1 = this.phonebook.treeMap.get("Adam").phoneNumberList.get(0);
54
+        String actualPhoneNumber2 = this.phonebook.treeMap.get("Adam").phoneNumberList.get(1);
55
 
55
 
56
         Assert.assertEquals(expectedPhoneNumber1, actualPhoneNumber1);
56
         Assert.assertEquals(expectedPhoneNumber1, actualPhoneNumber1);
57
         Assert.assertEquals(expectedPhoneNumber2, actualPhoneNumber2);
57
         Assert.assertEquals(expectedPhoneNumber2, actualPhoneNumber2);
63
         this.phonebook.addEntry("Bob", "(333) 444-5555");
63
         this.phonebook.addEntry("Bob", "(333) 444-5555");
64
         this.phonebook.removeRecord("Bob");
64
         this.phonebook.removeRecord("Bob");
65
 
65
 
66
-        boolean actualPersonName = this.phonebook.hashMap.containsKey("Bob");
67
-        boolean actualPersonObject = this.phonebook.hashMap.containsValue(this.phonebook.hashMap.get("Bob"));
66
+        boolean actualPersonName = this.phonebook.treeMap.containsKey("Bob");
67
+        boolean actualPersonObject = this.phonebook.treeMap.containsValue(this.phonebook.treeMap.get("Bob"));
68
 
68
 
69
         Assert.assertFalse(actualPersonName);
69
         Assert.assertFalse(actualPersonName);
70
         Assert.assertFalse(actualPersonObject);
70
         Assert.assertFalse(actualPersonObject);
76
         this.phonebook.addEntry("Gabriel", "(100) 000-0000", "(110) 000-0000");
76
         this.phonebook.addEntry("Gabriel", "(100) 000-0000", "(110) 000-0000");
77
 
77
 
78
         System.out.println("getAllEntriesTest:");
78
         System.out.println("getAllEntriesTest:");
79
-        String expectedKeySet = "[Gabriel, Frank]";
79
+        String expectedKeySet = "[Frank, Gabriel]";
80
         String actualKeySet = this.phonebook.getKeySet().toString();
80
         String actualKeySet = this.phonebook.getKeySet().toString();
81
 
81
 
82
         Assert.assertEquals(expectedKeySet, actualKeySet);
82
         Assert.assertEquals(expectedKeySet, actualKeySet);
87
         this.phonebook.addEntry("Herbert", "(200) 000-0000", "(210) 000-0000");
87
         this.phonebook.addEntry("Herbert", "(200) 000-0000", "(210) 000-0000");
88
         this.phonebook.addEntry("Isabel", "(300) 000-0000", "(310) 000-0000");
88
         this.phonebook.addEntry("Isabel", "(300) 000-0000", "(310) 000-0000");
89
 
89
 
90
-        String expectedEntries = "Isabel" + "\n" +
91
-                                 "\t" + "(300) 000-0000" + "\n" +
92
-                                 "\t" + "(310) 000-0000" + "\n" +
93
-                                 "Herbert" + "\n" +
90
+        String expectedEntries = "Herbert" + "\n" +
94
                                  "\t" + "(200) 000-0000" + "\n" +
91
                                  "\t" + "(200) 000-0000" + "\n" +
95
-                                 "\t" + "(210) 000-0000" + "\n";
92
+                                 "\t" + "(210) 000-0000" + "\n" +
93
+                                 "Isabel" + "\n" +
94
+                                 "\t" + "(300) 000-0000" + "\n" +
95
+                                 "\t" + "(310) 000-0000" + "\n";
96
         String actualEntries = this.phonebook.displayAllEntries().toString();
96
         String actualEntries = this.phonebook.displayAllEntries().toString();
97
 
97
 
98
 //        System.out.println("displayAllEntriesTest:");
98
 //        System.out.println("displayAllEntriesTest:");
118
         String expectedPhoneNumber = "(000) 111-2222";
118
         String expectedPhoneNumber = "(000) 111-2222";
119
 
119
 
120
         String actualNameFromPhoneBook = "Ava";
120
         String actualNameFromPhoneBook = "Ava";
121
-        boolean actualPhoneNumberFromPhoneBook = this.phonebook.hashMap.get(actualNameFromPhoneBook).phoneNumberList.contains(expectedPhoneNumber);
121
+        boolean actualPhoneNumberFromPhoneBook = this.phonebook.treeMap.get(actualNameFromPhoneBook).phoneNumberList.contains(expectedPhoneNumber);
122
 
122
 
123
         Assert.assertEquals(expectedName, this.phonebook.reverseLookup(givenPhoneNumber));
123
         Assert.assertEquals(expectedName, this.phonebook.reverseLookup(givenPhoneNumber));
124
         // Then
124
         // Then