Browse Source

Part 3 completed

Peter McCormick 6 years ago
parent
commit
7ab6d64c32

+ 26
- 18
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -1,7 +1,6 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
-import java.util.Set;
4
-import java.util.TreeMap;
3
+import java.util.*;
5 4
 
6 5
 /**
7 6
  * Created by leon on 1/23/18.
@@ -10,23 +9,26 @@ public class PhoneBook {
10 9
 
11 10
     private String name;
12 11
     private String phoneNumber;
13
-    private String phoneNumber2;
14 12
 
15
-    private TreeMap<String, String> phoneBookDirectory = new TreeMap<String, String>();
16 13
 
17
-
18
-    public String lookupNameToGetAccompanyingPhoneNumber(String name) {
19
-        return phoneBookDirectory.get(name);
14
+    private TreeMap<String, ArrayList<String>> phoneBookDirectory = new TreeMap<String, ArrayList<String>>();
20 15
 
21 16
 
17
+    public ArrayList<String> lookupNameToGetAccompanyingPhoneNumber(String name) {
18
+       return phoneBookDirectory.get(name);
22 19
     }
23 20
 
24
-    public String addEntry(String name, String phoneNumber) {
25
-        phoneBookDirectory.put(name, phoneNumber);
26
-        return phoneBookDirectory.get(name);
27
-
21
+    public void addEntry(String name, String phoneNumber) {
22
+        if (phoneBookDirectory.containsKey(name)) {
23
+            phoneBookDirectory.get(name).add(String.valueOf(phoneNumber));
24
+        } else {
25
+            ArrayList<String> nameAndPhoneNumber = new ArrayList<String>();
26
+            nameAndPhoneNumber.add(0, (String.valueOf(phoneNumber)));
27
+            phoneBookDirectory.put(name, nameAndPhoneNumber);
28
+        }
28 29
     }
29 30
 
31
+
30 32
     public String removeEntry(String name) {
31 33
         phoneBookDirectory.remove(name);
32 34
         return null;
@@ -44,13 +46,19 @@ public class PhoneBook {
44 46
 
45 47
 
46 48
     public String reverseLookup(String phoneNumber) {
47
-        Set<String> names = phoneBookDirectory.keySet();
48
-        for (String key: names) {
49
-            if (phoneBookDirectory.get(key).contains(phoneNumber)) {
50
-                return key;
51
-            }
52
-        }
53
-        return null;
54 49
 
50
+       Set<String> keys = phoneBookDirectory.keySet();
51
+       for (String key: keys) {
52
+               if (phoneBookDirectory.get(key).contains(phoneNumber)) {
53
+                   return key;
54
+           }
55
+       } return "no phone number";
56
+
57
+        /* for (Map.Entry<String, ArrayList<String>> entry : phoneBookDirectory.entrySet()) {
58
+            if (entry.getValue().equals(phoneNumber)) {
59
+                return entry.getKey();
60
+            }
61
+        } return null;
62
+        */
55 63
     }
56 64
 }

+ 9
- 11
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

@@ -2,6 +2,8 @@ package com.zipcodewilmington.phonebook;
2 2
 
3 3
 import com.sun.xml.internal.ws.policy.AssertionSet;
4 4
 import org.junit.*;
5
+
6
+import java.util.ArrayList;
5 7
 import java.util.TreeMap;
6 8
 
7 9
 /**
@@ -17,25 +19,22 @@ public class PhoneBookTest {
17 19
     private TreeMap<String, String> phoneBook = new TreeMap<String, String>();
18 20
 
19 21
 
20
-
21
-
22 22
     @Test
23 23
     public void testlookUpNameToGetAccompanyingPhoneNumber() {
24 24
         testPhoneBook.addEntry("Rodrigo", "233-123-3445");
25 25
         testPhoneBook.addEntry("Elena", "234-123-3344");
26 26
         testPhoneBook.addEntry("Robert", "332-233-7821");
27 27
         String expected = "332-233-7821";
28
-        System.out.print(testPhoneBook.lookupNameToGetAccompanyingPhoneNumber("Robert"));
29
-        String actual = testPhoneBook.lookupNameToGetAccompanyingPhoneNumber("Robert");
28
+        String actual = testPhoneBook.lookupNameToGetAccompanyingPhoneNumber("Robert").get(0);
30 29
         Assert.assertEquals("should return Robert's phone number", expected, actual);
31 30
 
32 31
     }
33 32
     @Test
34
-    public void  testAddEntry() {
35
-        String expected ="233-123-3445";
36
-        String actual = testPhoneBook.addEntry("Rodrigo", "233-123-3445");
37
-        System.out.println(actual + " " + expected);
38
-        Assert.assertEquals(expected, actual);
33
+    public void testAddEntry() {
34
+        testPhoneBook.addEntry("Rodrigo", "233-123-3445");
35
+        String expected = "233-123-3445";
36
+        String actual = testPhoneBook.lookupNameToGetAccompanyingPhoneNumber("Rodrigo").get(0);
37
+        Assert.assertEquals(actual, expected);
39 38
     }
40 39
     @Test
41 40
     public void testRemoveEntry(){
@@ -69,9 +68,8 @@ public class PhoneBookTest {
69 68
         testPhoneBook.addEntry("Rodrigo", "233-123-3445");
70 69
         testPhoneBook.addEntry("Elena", "234-123-3344");
71 70
         testPhoneBook.addEntry("Robert", "332-233-7821");
72
-        String phoneNumber= "234-123-3344";
71
+        String actual= testPhoneBook.reverseLookup("234-123-3344");
73 72
         String expected = "Elena";
74
-        String actual = testPhoneBook.reverseLookup( "234-123-3344");
75 73
         Assert.assertEquals("should return who the phone number is assigned to ", expected, actual);
76 74
         }
77 75
 }