Browse Source

Refactored and cleaned up

Kibret Tecle 6 years ago
parent
commit
da4a508597

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

@@ -6,19 +6,19 @@ import java.util.*;
6 6
  * Created by leon on 1/23/18.
7 7
  */
8 8
 public class PhoneBook {
9
-    Map<String, List<String>> phoneBook;
9
+    Map<String, ArrayList<String>> phoneBook;
10 10
 
11 11
     public PhoneBook() {
12
-        this.phoneBook = new TreeMap<String, List<String>>();
12
+        this.phoneBook = new TreeMap<String, ArrayList<String>>();
13 13
     }
14 14
 
15
-    public boolean add(String name, String phone) {
16
-        if (phoneBook.containsKey(name)) {
17
-            phoneBook.get(name).add(phone);
15
+    protected boolean add(String name, String phone) {
16
+        if (hasEntry(name)) {
17
+            getArrayListFor(name).add(phone);
18 18
             return true;
19 19
 
20 20
         } else {
21
-            List<String> contactNumbers = new ArrayList<String>();
21
+            ArrayList<String> contactNumbers = new ArrayList<String>();
22 22
             contactNumbers.add(phone);
23 23
             phoneBook.put(name, contactNumbers);
24 24
         }
@@ -26,22 +26,20 @@ public class PhoneBook {
26 26
         return true;
27 27
     }
28 28
 
29
-    public boolean remove(String name) {
30
-        return phoneBook.remove(name) != null;
29
+    protected void remove(String name) {
30
+         phoneBook.remove(name);
31 31
     }
32 32
 
33
-    public String lookup(String name) {
34
-        String myNumbers = "";
35
-        for (String key : phoneBook.keySet()) {
36
-            for (int i = 0; i < phoneBook.get(key).size(); i++) {
37
-                myNumbers += phoneBook.get(key).get(i) + "  ";
38
-            }
39
-
33
+    protected String lookup(String name) {
34
+        String contactInfo = "";
35
+        ArrayList<String>phoneNumberLists = getArrayListFor(name);
36
+        for (String phoneNumbers:phoneNumberLists) {
37
+            contactInfo+=phoneNumbers + " ";
40 38
         }
41
-        return myNumbers;
39
+        return contactInfo;
42 40
     }
43 41
 
44
-    public String listNames() {
42
+    protected String listNames() {
45 43
         String names = "";
46 44
         for (String a : phoneBook.keySet()) {
47 45
             names += a + "\n";
@@ -50,27 +48,43 @@ public class PhoneBook {
50 48
         return names;
51 49
     }
52 50
 
53
-    public String listNamesAndNumbers() {
51
+    protected String listNamesAndNumbers() {
54 52
         StringBuilder namesAndNumbers = new StringBuilder();
55 53
         for (String keys : phoneBook.keySet()) {
56
-            List<String> contact = phoneBook.get(keys);
54
+            namesAndNumbers.append(keys+"==>");
55
+            ArrayList<String> contact = getArrayListFor(keys);
57 56
             for (String num : contact) {
58
-                namesAndNumbers.append(keys).append("    ").append(num);
57
+                namesAndNumbers.append(num+",");
59 58
             }
60 59
             namesAndNumbers.append("\n");
61 60
         }
62 61
         return namesAndNumbers.toString();
63 62
     }
64 63
 
65
-    public String reverseLookUp(String number) {
64
+    protected String reverseLookUp(String number) {
66 65
         for (String keys : phoneBook.keySet()) {
67
-            for (int i = 0; i < phoneBook.get(keys).size(); i++) {
68
-                if (phoneBook.get(keys).get(i).equals(number))
66
+            for (int i = 0; i < getArrayListFor(keys).size(); i++) {
67
+                if (getArrayListFor(keys).get(i).equals(number))
69 68
                     return keys;
70 69
             }
71 70
         }
72 71
         return null;
73 72
     }
73
+    protected boolean hasEntry(String name){
74
+        if(phoneBook.containsKey(name)){
75
+            return true;
76
+        }
77
+        return false;
78
+    }
79
+    protected ArrayList<String> getArrayListFor(String name){
80
+        return phoneBook.get(name);
81
+    }
82
+    protected void removeIndividualNumbers(String name,String number){
83
+        getArrayListFor(name).remove(number);
84
+    }
74 85
 
86
+    protected void removeRecord() {
75 87
 
88
+        phoneBook.clear();
89
+    }
76 90
 }

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

@@ -5,24 +5,16 @@ import org.junit.Assert;
5 5
 import org.junit.Before;
6 6
 import org.junit.Test;
7 7
 
8
-import java.util.List;
9
-import java.util.Map;
10
-import java.util.TreeMap;
11 8
 
12 9
 /**
13 10
  * Created by leon on 1/23/18.
14 11
  */
15 12
 
16 13
 
17
-
18
-
19 14
 public class PhoneBookTest {
20
-    private static PhoneBook phoneBookTest;
21 15
 
22
-     @Before
23
-     public void setUp() {
24
-        PhoneBook phoneBookTest = new PhoneBook();
25
-        phoneBookTest.add("John","1234567890");
16
+    @Before
17
+    public void setUp() {
26 18
 
27 19
     }
28 20
 
@@ -31,62 +23,99 @@ public class PhoneBookTest {
31 23
     }
32 24
 
33 25
     @Test
34
-   public void testLookup() {
26
+    public void testLookup() {
35 27
         PhoneBook phoneBookTest = new PhoneBook();
36
-        phoneBookTest.add("James","555555555");
28
+        phoneBookTest.add("James", "555555555");
29
+        phoneBookTest.add("Charles", "2026783422");
37 30
 
38
-        String actual = phoneBookTest.lookup("John");
39
-        Assert.assertEquals("555555555  ",actual);
31
+        String actual = phoneBookTest.lookup("Charles");
32
+        Assert.assertEquals("2026783422 ", actual);
40 33
     }
34
+
41 35
     @Test
42
-    public void testListNames(){
36
+    public void testListNames() {
43 37
         PhoneBook phoneBookTest = new PhoneBook();
44
-        phoneBookTest.add("John","1234567890");
45
-        phoneBookTest.add("James","555555555");
38
+        phoneBookTest.add("John", "1234567890");
39
+        phoneBookTest.add("James", "555555555");
46 40
         String expected = "James\n" +
47
-                            "John\n";
41
+                "John\n";
48 42
         String actual = phoneBookTest.listNames();
49 43
         Assert.assertEquals(expected, actual);
50 44
 
51 45
     }
46
+
52 47
     @Test
53
-    public void testListNumbersAndNumbers(){
48
+    public void testListNumbersAndNumbers() {
54 49
         PhoneBook phoneBookTest = new PhoneBook();
55
-        phoneBookTest.add("John","1234567890");
56
-        phoneBookTest.add("James","555555555");
57
-        phoneBookTest.add("Micheal","5678901234");
58
-        String expected = "James    "+ "555555555\n"+
59
-                "John    "+"1234567890\n"+
60
-                "Micheal    "+"5678901234\n";
61
-        String actual =phoneBookTest.listNamesAndNumbers();
50
+        phoneBookTest.add("John", "1234567890");
51
+        phoneBookTest.add("James", "5555555555");
52
+        phoneBookTest.add("James", "6666666666");
53
+        phoneBookTest.add("Mike", "5678901234");
54
+        String expected = "James==>" + "5555555555,6666666666,\n" +
55
+                "John==>" + "1234567890,\n" +
56
+                "Mike==>" + "5678901234,\n";
57
+        String actual = phoneBookTest.listNamesAndNumbers();
62 58
         Assert.assertEquals(expected, actual);
63 59
     }
64 60
 
65 61
     @Test
66
-    public void testAdd(){
62
+    public void testAdd() {
67 63
         PhoneBook phoneBookTest = new PhoneBook();
68 64
         boolean expected = true;
69
-        boolean actual = phoneBookTest.add("Peter","7777777777");
65
+        boolean actual = phoneBookTest.add("Peter", "7777777777");
70 66
         Assert.assertEquals(expected, actual);
71 67
 
72 68
     }
69
+
73 70
     @Test
74
-    public void testRemove(){
71
+    public void testRemove() {
75 72
         PhoneBook phoneBookTest = new PhoneBook();
76
-        phoneBookTest.add("John","1234567890");
77
-        boolean expected =true;
78
-        boolean actual = phoneBookTest.remove("John");
73
+        phoneBookTest.add("John", "1234567890");
74
+        phoneBookTest.add("Peter", "7777777777");
75
+        String ListPreRemoving = "John==>1234567890,\n" +
76
+                "Peter==>7777777777,\n";
77
+        phoneBookTest.remove("John");
78
+        String postRemoving = "Peter==>7777777777,\n";
79
+        String expected = postRemoving;
80
+        String actual = phoneBookTest.listNamesAndNumbers();
79 81
         Assert.assertEquals(expected, actual);
80 82
     }
83
+
81 84
     @Test
82 85
     public void testReverseLookup() {
83 86
         PhoneBook phoneBookTest = new PhoneBook();
84
-        phoneBookTest.add("James","555555555");
87
+        phoneBookTest.add("James", "555555555");
85 88
 
86 89
         String actual = phoneBookTest.reverseLookUp("555555555");
87
-        Assert.assertEquals("James",actual);
90
+        Assert.assertEquals("James", actual);
91
+    }
92
+
93
+    @Test
94
+    public void testRemoveIndividualNumbers() {
95
+        PhoneBook phonebookeTest = new PhoneBook();
96
+        phonebookeTest.add("James", "2222222222");
97
+        phonebookeTest.add("James", "3333333333");
98
+
99
+        String expected = "James==>3333333333,\n";
100
+        phonebookeTest.removeIndividualNumbers("James", "2222222222");
101
+
102
+        String actual = phonebookeTest.listNamesAndNumbers();
103
+        Assert.assertEquals(expected, actual);
88 104
     }
89 105
 
106
+    @Test
107
+    public void testRemoveRecord() {
108
+        PhoneBook phoneBookTest = new PhoneBook();
109
+        phoneBookTest.add("Efrem", "1234567890");
110
+        phoneBookTest.add("Brhane", "1010101010");
111
+
112
+        phoneBookTest.removeRecord();
113
+
114
+        String expected = "";
115
+        String actual = phoneBookTest.listNamesAndNumbers();
116
+
117
+        Assert.assertEquals(expected, actual);
118
+    }
90 119
 
91 120
 
92 121
 }