Browse Source

mostly complete

Stwillia94 6 years ago
parent
commit
6e60ee3ffa

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

@@ -1,7 +1,102 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+
4
+import java.util.ArrayList;
5
+import java.util.Map;
6
+import java.util.TreeMap;
7
+
3 8
 /**
4 9
  * Created by leon on 1/23/18.
5 10
  */
6
-public class PhoneBook {
11
+public class PhoneBook{
12
+
13
+    TreeMap<String, ArrayList<String>> book =new TreeMap<>();
14
+
15
+    public boolean hasEntry(String keyName){
16
+        if(book.containsKey(keyName)){
17
+
18
+        return true;
19
+
20
+        } else
21
+
22
+        return false;
23
+
24
+        }
25
+
26
+    public void add(String name, ArrayList<String>num){
27
+        book.put(name, num);
28
+
29
+    }
30
+
31
+
32
+
33
+    public void addIndvNum(String name, String number){
34
+        if(hasEntry(name)){
35
+            ArrayList<String>pb = lookup(name);
36
+            pb.add(number);
37
+            book.put(name, pb);
38
+
39
+        }
40
+
41
+
42
+    }
43
+
44
+    public int size() {
45
+        return book.size();
46
+
47
+    }
48
+
49
+    public ArrayList<String> lookup(String name){
50
+        if(book.containsKey(name)){
51
+            return book.get(name);
52
+        }
53
+        else
54
+            return null;
55
+    }
56
+
57
+
58
+    public void remove(String name, String remNum) {
59
+        if(hasEntry(name)) {
60
+            ArrayList<String> pb = lookup(name);
61
+            for (String number : pb) {
62
+                if (remNum == number) {
63
+                    pb.remove(remNum);
64
+                    book.put(name, pb);
65
+                    break;
66
+
67
+                }
68
+            }
69
+        }
70
+
71
+
72
+    }
73
+
74
+    public void removeRecord(String name, ArrayList<String> nums) {
75
+        book.remove(name,nums);
76
+    }
77
+
78
+    public String reverseLookup(ArrayList<String>num){
79
+        String output = "";
80
+        for(Map.Entry<String, ArrayList<String>> entry : book.entrySet()) {
81
+            if(entry.getValue().equals(num)) {
82
+                output = entry.getKey();
83
+            }
84
+        }
85
+        return output;
86
+
87
+    }
88
+
89
+    public String listNameAndNumbers() {
90
+        String output = "";
91
+        for (Map.Entry<String, ArrayList<String>> entry : book.entrySet()) {
92
+            output += "\nName: " + entry.getKey() + "Number: " + entry.getValue();
93
+        }
94
+        return output;
95
+    }
96
+
97
+
7 98
 }
99
+
100
+
101
+
102
+

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

@@ -1,7 +1,183 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+import java.util.ArrayList;
7
+
3 8
 /**
4 9
  * Created by leon on 1/23/18.
5 10
  */
6 11
 public class PhoneBookTest {
12
+
13
+    @Test
14
+    public void testHasEntry() {
15
+        // Given
16
+        PhoneBook book = new PhoneBook();
17
+        String name = "Dan";
18
+        ArrayList<String> phoneNumber = new ArrayList<>();
19
+        phoneNumber.add("300-111-1222");
20
+
21
+        book.add(name, phoneNumber);
22
+
23
+        //When
24
+        boolean actualOutput = book.hasEntry(name);
25
+
26
+        //Then
27
+        Assert.assertTrue(actualOutput);
28
+
29
+    }
30
+
31
+    @Test
32
+    public void testAdd() {
33
+        //Given
34
+        PhoneBook book = new PhoneBook();
35
+        String name = "Ravi";
36
+        ArrayList<String> phoneNumber = new ArrayList<>();
37
+        phoneNumber.add("300-111-1222");
38
+        phoneNumber.add("300-111-1223");
39
+
40
+        //When
41
+        book.add(name, phoneNumber);
42
+
43
+        //Then
44
+        Assert.assertEquals(1, book.size());
45
+
46
+
47
+    }
48
+
49
+    @Test
50
+    public void testAddIndvNum() {
51
+        //Given
52
+        PhoneBook book = new PhoneBook();
53
+        String name = "Ravi";
54
+        ArrayList<String> phoneNumber = new ArrayList<>();
55
+        phoneNumber.add("300-111-1222");
56
+        phoneNumber.add("300-111-1223");
57
+        //When
58
+        book.addIndvNum(name, "300-111-1224");
59
+        String expOut = "300-111-1224";
60
+        ArrayList<String> list = book.lookup(name);
61
+        String actOut = list.get(2);
62
+
63
+        //Then
64
+        Assert.assertEquals(expOut, actOut);
65
+
66
+
67
+    }
68
+
69
+    @Test
70
+    public void testLookup() {
71
+        //Given
72
+        PhoneBook book = new PhoneBook();
73
+        String name = "John";
74
+        ArrayList<String> phoneNumber = new ArrayList<>();
75
+        phoneNumber.add("300-111-2222");
76
+        phoneNumber.add("300-111-2223");
77
+        book.add(name, phoneNumber);
78
+        String expNum1 = "300-111-2222";
79
+        String expNum2 = "300-111-2223";
80
+
81
+        //When
82
+        ArrayList<String> actNumList = book.lookup(name);
83
+        String actNum1 = actNumList.get(0);
84
+        String actNum2 = actNumList.get(1);
85
+
86
+        //Then
87
+        Assert.assertEquals(expNum1, actNum1);
88
+        Assert.assertEquals(expNum2, actNum2);
89
+
90
+
91
+    }
92
+
93
+    @Test
94
+    public void testRemove() {
95
+        //Given
96
+        PhoneBook book = new PhoneBook();
97
+        String name = "Jane";
98
+        ArrayList<String> phoneNumber = new ArrayList<>();
99
+        phoneNumber.add("300-111-1222");
100
+        phoneNumber.add("300-111-1223");
101
+        book.add(name, phoneNumber);
102
+        String remNum = "300-111-1223";
103
+
104
+        //When
105
+        book.remove(name, remNum);
106
+        ArrayList<String> actNumList = book.lookup(name);
107
+
108
+        //Then
109
+        Assert.assertEquals(1, actNumList.size());
110
+
111
+    }
112
+
113
+    @Test
114
+    public void testRemoveRecord() {
115
+        //Given
116
+        PhoneBook book = new PhoneBook();
117
+        String name1 = "Jimmy";
118
+        String name2 = "Jimmys";
119
+        ArrayList<String> phoneNumber1 = new ArrayList<>();
120
+        phoneNumber1.add("300-111-1222");
121
+        phoneNumber1.add("300-111-1223");
122
+        book.add(name1, phoneNumber1);
123
+        ArrayList<String> phoneNumber2 = new ArrayList<>();
124
+        phoneNumber2.add("300-111-2223");
125
+        phoneNumber2.add("300-111-2224");
126
+        book.add(name2, phoneNumber2);
127
+
128
+        //When
129
+        book.removeRecord(name2, phoneNumber2);
130
+
131
+        //Then
132
+        Assert.assertEquals(1, book.size());
133
+
134
+    }
135
+
136
+    @Test
137
+    public void testReverseLookup() {
138
+        //Given
139
+        PhoneBook book = new PhoneBook();
140
+        String name = "Franny";
141
+        ArrayList<String> phoneNumber = new ArrayList<>();
142
+        phoneNumber.add("300-111-1222");
143
+        phoneNumber.add("300-111-1223");
144
+        book.add(name, phoneNumber);
145
+
146
+        //When
147
+        String actName = book.reverseLookup(phoneNumber);
148
+
149
+        //Then
150
+        Assert.assertEquals("Franny", actName);
151
+
152
+
153
+    }
154
+
155
+    @Test
156
+    public void testListNmAndNums() {
157
+        //Given
158
+        PhoneBook book = new PhoneBook();
159
+        String name1 = "GG";
160
+        String name2 = "HH";
161
+        ArrayList<String> phoneNumber1 = new ArrayList<>();
162
+        phoneNumber1.add("300-111-1222");
163
+        phoneNumber1.add("300-111-1223");
164
+        book.add(name1, phoneNumber1);
165
+        ArrayList<String> phoneNumber2 = new ArrayList<>();
166
+        phoneNumber2.add("300-111-1333");
167
+        phoneNumber2.add("300-111-1334");
168
+        book.add(name2, phoneNumber2);
169
+
170
+        String expOut = "\nName: GG Number : [300-111-1222, 300-111-1223]" + "\nName: HH Number : [300-111-1333, 300-111-1334]";
171
+
172
+        //When
173
+        String actOut = book.listNameAndNumbers();
174
+
175
+        //Then
176
+        Assert.assertEquals(expOut, actOut);
177
+
178
+
179
+    }
7 180
 }
181
+
182
+
183
+