Browse Source

Completed

Karousha Fennimore 6 years ago
parent
commit
6e7269c33d

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

@@ -4,10 +4,12 @@ public class Person {
4 4
 
5 5
     private String name;
6 6
     private String number;
7
+    private String number2;
7 8
 
8 9
     public Person(String name, String number) {
9 10
         this.name = name;
10 11
         this.number = number;
12
+        this.number2 = number2;
11 13
     }
12 14
 
13 15
     public String getName() {

+ 31
- 10
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -1,35 +1,56 @@
1 1
 package com.zipcodewilmington.phonebook;
2
+import java.util.*;
2 3
 
3
-import java.util.Set;
4
-import java.util.TreeMap;
5 4
 /**
6
- * Created by leon on 1/23/18.
5
+ * Created by Kay.
7 6
  */
8 7
 public class PhoneBook {
9 8
 
10
-    TreeMap phMap;
9
+    TreeMap<String, List<String>> phMap;
11 10
 
12 11
     public PhoneBook() {
13
-        this.phMap = new TreeMap();
12
+
13
+        this.phMap = new TreeMap<String, List<String>>();
14 14
     }
15 15
 
16 16
     public void add(String name, String number) {
17
-        phMap.put(name, number);
17
+        if (lookup(name) == null) {
18
+            List<String> phoneNumbers = new ArrayList<String>();
19
+            phoneNumbers.add(number);
20
+            phMap.put(name, phoneNumbers);
21
+        } else {
22
+            List<String> phoneNumbers = lookup(name);
23
+            phoneNumbers.add(number);
24
+        }
18 25
     }
19 26
 
20 27
     public void remove(String name) {
21 28
 
22 29
         phMap.remove(name);
23 30
     }
24
-    public String lookup(String name) {
25
-        return (String)phMap.get(name);
31
+
32
+    public List<String> lookup(String name) {
33
+
34
+        return phMap.get(name);
26 35
 
27 36
     }
28
-    public Set listNames () {
37
+
38
+    public Set listNames() {
39
+
29 40
         return phMap.keySet();
30 41
     }
42
+
31 43
     public Set listRecords() {
44
+
32 45
         return phMap.entrySet();
33 46
     }
34
-}
35 47
 
48
+    public String reverseLookup(String phoneNumber) {
49
+        for (Map.Entry<String, List<String>> valuePair : phMap.entrySet()) {
50
+            if (valuePair.getValue().contains(phoneNumber)){
51
+                return valuePair.getKey();
52
+            }
53
+        }
54
+        return "Entry not found";
55
+    }
56
+}

+ 18
- 17
src/test/java/com/zipcodewilmington/phonebook/PersonTest.java View File

@@ -1,7 +1,7 @@
1
-package com.zipcodewilmington.phonebook;
2
-import org.junit.Assert.*;
3
-import org.junit.Test;
4
-
1
+//package com.zipcodewilmington.phonebook;
2
+//import org.junit.Assert.*;
3
+//import org.junit.Test;
4
+//
5 5
 //import static org.junit.Assert.assertTrue;
6 6
 //
7 7
 ///**
@@ -9,19 +9,19 @@ import org.junit.Test;
9 9
 // */
10 10
 //public class PersonTest {
11 11
 //
12
-//        @Test
13
-//        public void getNameTest() {
14
-//            Person person = new Person();
15
-//            person.setName("Kay");
16
-//            assertTrue(person.getName().equalsIgnoreCase("Kay"));
17
-//        }
12
+//    @Test
13
+//    public void getNameTest() {
14
+//        Person person = new Person();
15
+//        person.setName("Kay");
16
+//        assertTrue(person.getName().equalsIgnoreCase("Kay"));
17
+//    }
18 18
 //
19
-//        @Test
20
-//        public void setNameTest() {
21
-//            Person person = new Person();
22
-//            person.getName("Kay");
23
-//            assertTrue(person.setName().equalsIgnoreCase("Kay"));
24
-//        }
19
+//    @Test
20
+//    public void setNameTest() {
21
+//        Person person = new Person();
22
+//        person.getName("Kay");
23
+//        assertTrue(person.setName().equalsIgnoreCase("Kay"));
24
+//    }
25 25
 //        @Test
26 26
 //        public void getNumberTest() {
27 27
 //            Person person = new Person();
@@ -34,4 +34,5 @@ import org.junit.Test;
34 34
 //            person.setNumber("123");
35 35
 //            assertTrue(person.getNumber().equalsIgnoreCase("123"));
36 36
 //        }
37
-//}
37
+
38
+

+ 48
- 21
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

@@ -14,7 +14,7 @@ public class PhoneBookTest {
14 14
         String name = "Kay";
15 15
         String number = "1234567890";
16 16
         phTest.add(name, number);
17
-        Assert.assertTrue(phTest.lookup(name).equals(number));
17
+        Assert.assertTrue(phTest.lookup(name).contains(number));
18 18
     }
19 19
 
20 20
     @Test
@@ -33,19 +33,20 @@ public class PhoneBookTest {
33 33
         String number = "3456784523";
34 34
         //adding a test value key pair to test for removal
35 35
         phTest.add(name, number);
36
-        Assert.assertTrue(phTest.lookup(name).equals(number));
36
+        Assert.assertTrue(phTest.lookup(name).contains(number));
37 37
         //after confirmation that it has been added, it needs to be removed
38 38
         phTest.remove(name);
39 39
         Assert.assertTrue(phTest.lookup(name) == null);
40 40
 
41 41
     }
42
+
42 43
     @Test
43 44
     public void lookupTest() {
44 45
         PhoneBook phTest = new PhoneBook();
45 46
         String name = "John";
46 47
         String number = "8765930";
47 48
         phTest.add(name, number);
48
-        Assert.assertTrue(phTest.lookup(name).equals(number));
49
+        Assert.assertTrue(phTest.lookup(name).contains(number));
49 50
     }
50 51
 
51 52
 
@@ -81,37 +82,63 @@ public class PhoneBookTest {
81 82
     }
82 83
 
83 84
     @Test
84
-    public void listRecordsTest(){
85
+    public void listRecordsTest() {
85 86
         PhoneBook phTest = new PhoneBook();
86 87
         //added two new entries to test
87
-        phTest.add("Nico","487542");
88
-        phTest.add("Laura","1-800-no-sugar");
88
+        phTest.add("Dumbo", "487542");
89
+        phTest.add("Blumbo", "1800234583");
89 90
 
90 91
         //turned into an array to iterate through.
91 92
         Object[] array = phTest.listRecords().toArray();
92 93
         //to.Array returns Object[] array
93 94
         for (int i = 0; i < array.length; i++) {
94 95
             //Object that was in array was Map.Entry: a record on map (key value pair)
95
-            Map.Entry record = (Map.Entry) array[i];
96
+            Map.Entry<String, List<String>> record = (Map.Entry) array[i];
96 97
             //0 index = Laura because treemap is sorted alphabetically
97
-           if (i == 0 ){
98
-               Assert.assertTrue(record.getKey().equals("Laura"));
99
-               Assert.assertTrue(record.getValue().equals("1-800-no-sugar"));
100
-           }else{
101
-               Assert.assertTrue(record.getKey().equals("Nico"));
102
-               Assert.assertTrue(record.getValue().equals("487542"));
103
-           }
98
+            if (i == 0) {
99
+                Assert.assertTrue(record.getKey().equals("Blumbo"));
100
+                Assert.assertTrue(record.getValue().contains("1800234583"));
101
+            } else {
102
+                Assert.assertTrue(record.getKey().equals("Dumbo"));
103
+                Assert.assertTrue(record.getValue().contains("487542"));
104
+            }
105
+        }
106
+    }
107
+
108
+    @Test
109
+    public void reverselookupTest() {
110
+        PhoneBook phTest = new PhoneBook();
111
+        String expectedName = "Blumbo";
112
+        String expectedNumber = "1234567";
113
+        phTest.add(expectedName, expectedNumber);
114
+        //adding entry to test return
115
+        String actual = phTest.reverseLookup(expectedNumber);
116
+        Assert.assertEquals(expectedName, actual);
117
+
118
+    }
104 119
 
120
+    @Test
121
+    public void addTwoNumbersTest() {
122
+        ArrayList<String> phoneNumbers = new ArrayList<String>();
123
+        phoneNumbers.add("1111");
124
+        phoneNumbers.add("2222");
125
+
126
+        PhoneBook phTest = new PhoneBook();
127
+        String expectedName = "Blumbo";
128
+        phTest.add(expectedName, phoneNumbers.get(0));
129
+        phTest.add(expectedName, phoneNumbers.get(1));
130
+
131
+
132
+
133
+        //adding entry to test return
134
+        List<String> actual = phTest.lookup(expectedName);
135
+
136
+        for(String phoneNumber : phoneNumbers) {
137
+            Assert.assertTrue(actual.contains(phoneNumber));
105 138
         }
106 139
 
140
+
107 141
     }
108 142
 
109
-        //    @Test
110
-//    public void reverselookupTest() {
111
-//        String name = "Bob";
112
-//        String number = "8765930";
113
-//        phTest.add(name, number);
114
-//        Assert.assertTrue(phTest.lookup(number).equals(name));
115
-//    }
116 143
 
117 144
     }