Browse Source

Merge 7ab6d64c326f468f5863177fd83183c2b32feac1 into 74a9673e36c7d25c28315cb54247a91792f8fbc0

Peteyocre 6 years ago
parent
commit
c1974af58c
No account linked to committer's email

+ 7
- 0
pom.xml View File

@@ -7,6 +7,13 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>phonebok</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>4.12</version>
15
+        </dependency>
16
+    </dependencies>
10 17
 
11 18
 
12 19
 </project>

+ 10
- 0
src/main/java/com/zipcodewilmington/phonebook/Person.java View File

@@ -0,0 +1,10 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+public class Person {
4
+
5
+    private Object name;
6
+    private Object phoneNumber;
7
+
8
+
9
+
10
+}

+ 57
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -1,7 +1,64 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.*;
4
+
3 5
 /**
4 6
  * Created by leon on 1/23/18.
5 7
  */
6 8
 public class PhoneBook {
9
+
10
+    private String name;
11
+    private String phoneNumber;
12
+
13
+
14
+    private TreeMap<String, ArrayList<String>> phoneBookDirectory = new TreeMap<String, ArrayList<String>>();
15
+
16
+
17
+    public ArrayList<String> lookupNameToGetAccompanyingPhoneNumber(String name) {
18
+       return phoneBookDirectory.get(name);
19
+    }
20
+
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
+        }
29
+    }
30
+
31
+
32
+    public String removeEntry(String name) {
33
+        phoneBookDirectory.remove(name);
34
+        return null;
35
+
36
+    }
37
+
38
+    public java.util.Set listNames() {
39
+        return phoneBookDirectory.keySet();
40
+    }
41
+
42
+
43
+    public java.util.Set listNamesAndNumbers() {
44
+        return phoneBookDirectory.entrySet();
45
+    }
46
+
47
+
48
+    public String reverseLookup(String phoneNumber) {
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
+        */
63
+    }
7 64
 }

+ 9
- 0
src/test/java/com/zipcodewilmington/phonebook/PhoneBookPopulator.java View File

@@ -0,0 +1,9 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+import java.util.TreeMap;
4
+
5
+public class PhoneBookPopulator {
6
+
7
+    //public TreeMap;
8
+
9
+}

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

@@ -1,7 +1,77 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import com.sun.xml.internal.ws.policy.AssertionSet;
4
+import org.junit.*;
5
+
6
+import java.util.ArrayList;
7
+import java.util.TreeMap;
8
+
3 9
 /**
4 10
  * Created by leon on 1/23/18.
5 11
  */
6 12
 public class PhoneBookTest {
13
+    PhoneBook testPhoneBook;
14
+    @Before
15
+    public void setup (){
16
+        testPhoneBook = new PhoneBook();
17
+    }
18
+
19
+    private TreeMap<String, String> phoneBook = new TreeMap<String, String>();
20
+
21
+
22
+    @Test
23
+    public void testlookUpNameToGetAccompanyingPhoneNumber() {
24
+        testPhoneBook.addEntry("Rodrigo", "233-123-3445");
25
+        testPhoneBook.addEntry("Elena", "234-123-3344");
26
+        testPhoneBook.addEntry("Robert", "332-233-7821");
27
+        String expected = "332-233-7821";
28
+        String actual = testPhoneBook.lookupNameToGetAccompanyingPhoneNumber("Robert").get(0);
29
+        Assert.assertEquals("should return Robert's phone number", expected, actual);
30
+
31
+    }
32
+    @Test
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);
38
+    }
39
+    @Test
40
+    public void testRemoveEntry(){
41
+        testPhoneBook.addEntry("Rodrigo", "233-123-3445");
42
+        testPhoneBook.addEntry("Elena", "234-123-3344");
43
+        testPhoneBook.addEntry("Robert", "332-233-7821");
44
+        String actual =testPhoneBook.removeEntry("Robert");
45
+        String name = "Robert";
46
+        String expected = null;
47
+        Assert.assertEquals(expected, actual);
48
+
49
+    }
50
+    @Test
51
+    public void testListNames() {
52
+        testPhoneBook.addEntry("Rodrigo", "233-123-3445");
53
+        testPhoneBook.addEntry("Elena", "234-123-3344");
54
+        testPhoneBook.addEntry("Robert", "332-233-7821");
55
+        System.out.println(testPhoneBook.listNames());
56
+        }
57
+
58
+    @Test
59
+    public void testListNamesandNumbers() {
60
+        testPhoneBook.addEntry("Rodrigo", "233-123-3445");
61
+        testPhoneBook.addEntry("Elena", "234-123-3344");
62
+        testPhoneBook.addEntry("Robert", "332-233-7821");
63
+        System.out.println(testPhoneBook.listNamesAndNumbers());
64
+    }
65
+
66
+    @Test
67
+    public void testReverseLookup() {
68
+        testPhoneBook.addEntry("Rodrigo", "233-123-3445");
69
+        testPhoneBook.addEntry("Elena", "234-123-3344");
70
+        testPhoneBook.addEntry("Robert", "332-233-7821");
71
+        String actual= testPhoneBook.reverseLookup("234-123-3344");
72
+        String expected = "Elena";
73
+        Assert.assertEquals("should return who the phone number is assigned to ", expected, actual);
74
+        }
7 75
 }
76
+
77
+