Whitney Martinez 6 years ago
parent
commit
bf40cbc396

+ 23
- 2
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.ArrayList;
3
 import java.util.ArrayList;
4
+import java.util.Arrays;
4
 import java.util.Map;
5
 import java.util.Map;
5
 import java.util.TreeMap;
6
 import java.util.TreeMap;
6
 
7
 
12
     public static void main(String[] args){
13
     public static void main(String[] args){
13
 
14
 
14
     }
15
     }
16
+
15
   private Map<String, ArrayList<String>> contacts;
17
   private Map<String, ArrayList<String>> contacts;
16
 
18
 
17
     public PhoneBook(){
19
     public PhoneBook(){
20
+
18
         contacts=new TreeMap<String, ArrayList<String>>();
21
         contacts=new TreeMap<String, ArrayList<String>>();
19
     }
22
     }
20
 
23
 
33
     }
36
     }
34
 
37
 
35
     public void add(String name, String phonenumber) {
38
     public void add(String name, String phonenumber) {
36
-        ArrayList<String> details = new ArrayList<>();
39
+    //    contacts.containsKey(name);
40
+        ArrayList<String>details = new ArrayList<>();
41
+
42
+        boolean exists =  contacts.containsKey(name);
43
+        if(exists){
44
+
45
+        details = getNumbersFor(name);
46
+        }
37
 
47
 
38
         details.add(phonenumber);
48
         details.add(phonenumber);
39
 
49
 
45
     public String getContact(String name,String phonenumber) {
55
     public String getContact(String name,String phonenumber) {
46
         ArrayList<String> phonenumbers = contacts.get(name);
56
         ArrayList<String> phonenumbers = contacts.get(name);
47
 
57
 
48
-            return (phonenumbers==null? null:name);
58
+            return (name==null? null:phonenumber);
49
     }
59
     }
50
 
60
 
51
 
61
 
74
 
84
 
75
 
85
 
76
     }
86
     }
87
+    public String [] lookupNumbersFor(String name){
88
+        String [] result;
89
+        ArrayList<String> numbers = contacts.get(name);
90
+//        result = (String[]d) numbers.toArray();
91
+                result = numbers.toArray(new String[numbers.size()]);
92
+
93
+        return result;
94
+    }
95
+    private ArrayList<String> getNumbersFor(String name){
96
+        return contacts.get(name);
97
+    }
77
 
98
 
78
 
99
 
79
 
100
 

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

1
 package com.zipcodewilmington.phonebook;
1
 package com.zipcodewilmington.phonebook;
2
 import org.junit.Assert;
2
 import org.junit.Assert;
3
+import org.junit.Before;
3
 import org.junit.Test;
4
 import org.junit.Test;
4
 /**
5
 /**
5
  * Created by leon on 1/23/18.
6
  * Created by leon on 1/23/18.
6
  */
7
  */
7
 public class PhoneBookTest {
8
 public class PhoneBookTest {
9
+    String name;
10
+    String phonenumber,phonenumber2;
11
+    PhoneBook book;
8
 
12
 
13
+@Before
14
+    public void setup (){
9
 
15
 
16
+    name = "Steven";
17
+    phonenumber="122-545-2344";
18
+    phonenumber2 = "453-343-2322";
19
+    book=new PhoneBook();
20
+    book.add(name,phonenumber);
21
+    book.add(name,phonenumber2);
22
+
23
+
24
+
25
+}
10
     @Test
26
     @Test
11
     public void hasEntryphone() {
27
     public void hasEntryphone() {
12
 
28
 
13
-        PhoneBook book = new PhoneBook();
14
-        String name = "AA";
15
-        String phonenumber = "234-232-2311";
29
+
30
+//        String name = "AA";
16
         //when
31
         //when
17
         book.add(name, phonenumber);
32
         book.add(name, phonenumber);
18
         boolean actualOutput = book.hasEntry(name);
33
         boolean actualOutput = book.hasEntry(name);
27
     public void add() {
42
     public void add() {
28
 
43
 
29
         PhoneBook book = new PhoneBook();
44
         PhoneBook book = new PhoneBook();
30
-        String name = "AA";
31
-        String phonenumber = "234-232-2311";
45
+//        String name = "AA";
46
+//        String phonenumber = "234-232-2311";
32
         book.add(name, phonenumber);
47
         book.add(name, phonenumber);
33
 
48
 
34
         //then
49
         //then
39
     public void getContact_test() {
54
     public void getContact_test() {
40
 
55
 
41
         PhoneBook book = new PhoneBook();
56
         PhoneBook book = new PhoneBook();
42
-        String name = "Whitney";
43
-        String phonenumber = "123-331-3231";
57
+//        String name = "Whitney";
58
+//        String phonenumber = "123-331-3231";
44
         book.add(name, phonenumber);
59
         book.add(name, phonenumber);
45
         String actual = book.getContact(name, phonenumber);
60
         String actual = book.getContact(name, phonenumber);
46
         String expected = name;
61
         String expected = name;
51
     @Test
66
     @Test
52
     public void reverseLookup_test() {
67
     public void reverseLookup_test() {
53
         PhoneBook book = new PhoneBook();
68
         PhoneBook book = new PhoneBook();
54
-        String name = "Whitney";
55
-        String phonenumber = "231-131-3257";
69
+//        String name = "Whitney";
70
+//        String phonenumber = "231-131-3257";
56
         book.add(name, phonenumber);
71
         book.add(name, phonenumber);
57
         String actual = book.reverselookup(phonenumber);
72
         String actual = book.reverselookup(phonenumber);
58
         String expect = name;
73
         String expect = name;
64
     @Test
79
     @Test
65
     public void remove_Test() {
80
     public void remove_Test() {
66
         PhoneBook book = new PhoneBook();
81
         PhoneBook book = new PhoneBook();
67
-        String name = "AA";
68
-        String phonenumber = "";
82
+//        String name = "AA";
83
+//        String phonenumber = "";
69
         book.add(name, phonenumber);
84
         book.add(name, phonenumber);
70
         book.remove(name);
85
         book.remove(name);
71
         boolean actual = book.hasEntry(name);
86
         boolean actual = book.hasEntry(name);
73
         Assert.assertFalse(actual);
88
         Assert.assertFalse(actual);
74
 
89
 
75
     }
90
     }
91
+    @Test
92
+    public void lookupNumbersFor_test() {
93
+
94
+            String [] expecting = new String[]{phonenumber,phonenumber2};
95
+            String [] actual = book.lookupNumbersFor(name);
96
+
97
+            Assert.assertEquals(expecting,actual);
98
+
99
+
100
+    }
76
 
101
 
77
     @Test
102
     @Test
78
     public void ListNamesandNumbers() {
103
     public void ListNamesandNumbers() {
79
         PhoneBook book = new PhoneBook();
104
         PhoneBook book = new PhoneBook();
80
-        String name = "";
81
-        String Phonenumber = "";
82
-        book.add(name, Phonenumber);
83
-        String actual = book.getContact(name, Phonenumber);
84
-        String expected = Phonenumber;
105
+        book.add(name, phonenumber);
106
+        String actual = book.getContact(name, phonenumber);
107
+        String expected = phonenumber;
85
         Assert.assertEquals(expected, actual);
108
         Assert.assertEquals(expected, actual);
86
 
109
 
87
     }
110
     }