Whitney Martinez пре 6 година
родитељ
комит
bf40cbc396

+ 23
- 2
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Прегледај датотеку

@@ -1,6 +1,7 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3 3
 import java.util.ArrayList;
4
+import java.util.Arrays;
4 5
 import java.util.Map;
5 6
 import java.util.TreeMap;
6 7
 
@@ -12,9 +13,11 @@ public class PhoneBook {
12 13
     public static void main(String[] args){
13 14
 
14 15
     }
16
+
15 17
   private Map<String, ArrayList<String>> contacts;
16 18
 
17 19
     public PhoneBook(){
20
+
18 21
         contacts=new TreeMap<String, ArrayList<String>>();
19 22
     }
20 23
 
@@ -33,7 +36,14 @@ public class PhoneBook {
33 36
     }
34 37
 
35 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 48
         details.add(phonenumber);
39 49
 
@@ -45,7 +55,7 @@ public class PhoneBook {
45 55
     public String getContact(String name,String phonenumber) {
46 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,6 +84,17 @@ public class PhoneBook {
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 Прегледај датотеку

@@ -1,18 +1,33 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 import org.junit.Assert;
3
+import org.junit.Before;
3 4
 import org.junit.Test;
4 5
 /**
5 6
  * Created by leon on 1/23/18.
6 7
  */
7 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 26
     @Test
11 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 31
         //when
17 32
         book.add(name, phonenumber);
18 33
         boolean actualOutput = book.hasEntry(name);
@@ -27,8 +42,8 @@ public class PhoneBookTest {
27 42
     public void add() {
28 43
 
29 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 47
         book.add(name, phonenumber);
33 48
 
34 49
         //then
@@ -39,8 +54,8 @@ public class PhoneBookTest {
39 54
     public void getContact_test() {
40 55
 
41 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 59
         book.add(name, phonenumber);
45 60
         String actual = book.getContact(name, phonenumber);
46 61
         String expected = name;
@@ -51,8 +66,8 @@ public class PhoneBookTest {
51 66
     @Test
52 67
     public void reverseLookup_test() {
53 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 71
         book.add(name, phonenumber);
57 72
         String actual = book.reverselookup(phonenumber);
58 73
         String expect = name;
@@ -64,8 +79,8 @@ public class PhoneBookTest {
64 79
     @Test
65 80
     public void remove_Test() {
66 81
         PhoneBook book = new PhoneBook();
67
-        String name = "AA";
68
-        String phonenumber = "";
82
+//        String name = "AA";
83
+//        String phonenumber = "";
69 84
         book.add(name, phonenumber);
70 85
         book.remove(name);
71 86
         boolean actual = book.hasEntry(name);
@@ -73,15 +88,23 @@ public class PhoneBookTest {
73 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 102
     @Test
78 103
     public void ListNamesandNumbers() {
79 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 108
         Assert.assertEquals(expected, actual);
86 109
 
87 110
     }