Bläddra i källkod

Finished all, replaced HashMap with TreeMap

Luis Romero 6 år sedan
förälder
incheckning
06060978fd

+ 13
- 13
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Visa fil

@@ -1,7 +1,7 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3 3
 import java.util.Map;
4
-import java.util.HashMap;
4
+import java.util.TreeMap;
5 5
 import java.util.ArrayList;
6 6
 import java.util.Set;
7 7
 
@@ -10,42 +10,42 @@ import java.util.Set;
10 10
  */
11 11
 public class PhoneBook {
12 12
 
13
-    protected HashMap<String, Person> hashMap;
13
+    protected TreeMap<String, Person> treeMap;
14 14
     protected ArrayList<String> entryList;
15 15
     protected Set<String> keySet;
16 16
 
17 17
     public PhoneBook() {
18
-        this.hashMap = new HashMap<String, Person>();
18
+        this.treeMap = new TreeMap<String, Person>();
19 19
         this.entryList = new ArrayList<String>();
20
-        this.keySet = hashMap.keySet();
20
+        this.keySet = treeMap.keySet();
21 21
         //this.hashMap.put("Name_empty", new Person("Name_empty", "Phone_number_empty"));
22 22
     }
23 23
 
24 24
     //PART I
25 25
     public Person lookup(String name) {
26
-        return hashMap.get(name);
26
+        return treeMap.get(name);
27 27
     }
28 28
 
29 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 33
     // Changed method name for Part III
34 34
     public void removeRecord(String name) {
35
-        hashMap.remove(name);
35
+        treeMap.remove(name);
36 36
     }
37 37
 
38 38
     public Set<String> getKeySet() { //names and phone numbers
39
-        return hashMap.keySet();
39
+        return treeMap.keySet();
40 40
         //return all persons
41 41
     }
42 42
 
43 43
     public StringBuilder displayAllEntries() { //names and phone numbers
44 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 51
         return sb;
@@ -54,8 +54,8 @@ public class PhoneBook {
54 54
 
55 55
     //PART II
56 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 59
                 return name;
60 60
                 //break;
61 61
             }

+ 14
- 14
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Visa fil

@@ -32,7 +32,7 @@ public class PhoneBookTest {
32 32
         String actualPersonName = expectedPerson.getName();
33 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 37
         Assert.assertTrue(actualPersonNameExists);
38 38
     }
@@ -42,16 +42,16 @@ public class PhoneBookTest {
42 42
     public void addEntryTest() {
43 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 47
         Assert.assertTrue(actualPersonName);
48 48
         Assert.assertTrue(actualPhoneNumber);
49 49
 
50 50
         String expectedPhoneNumber1 = "(000) 000-0000";
51 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 56
         Assert.assertEquals(expectedPhoneNumber1, actualPhoneNumber1);
57 57
         Assert.assertEquals(expectedPhoneNumber2, actualPhoneNumber2);
@@ -63,8 +63,8 @@ public class PhoneBookTest {
63 63
         this.phonebook.addEntry("Bob", "(333) 444-5555");
64 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 69
         Assert.assertFalse(actualPersonName);
70 70
         Assert.assertFalse(actualPersonObject);
@@ -76,7 +76,7 @@ public class PhoneBookTest {
76 76
         this.phonebook.addEntry("Gabriel", "(100) 000-0000", "(110) 000-0000");
77 77
 
78 78
         System.out.println("getAllEntriesTest:");
79
-        String expectedKeySet = "[Gabriel, Frank]";
79
+        String expectedKeySet = "[Frank, Gabriel]";
80 80
         String actualKeySet = this.phonebook.getKeySet().toString();
81 81
 
82 82
         Assert.assertEquals(expectedKeySet, actualKeySet);
@@ -87,12 +87,12 @@ public class PhoneBookTest {
87 87
         this.phonebook.addEntry("Herbert", "(200) 000-0000", "(210) 000-0000");
88 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 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 96
         String actualEntries = this.phonebook.displayAllEntries().toString();
97 97
 
98 98
 //        System.out.println("displayAllEntriesTest:");
@@ -118,7 +118,7 @@ public class PhoneBookTest {
118 118
         String expectedPhoneNumber = "(000) 111-2222";
119 119
 
120 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 123
         Assert.assertEquals(expectedName, this.phonebook.reverseLookup(givenPhoneNumber));
124 124
         // Then