Browse Source

Merge 1acd89902f4aa9ec150aa921f201a4b2093eb877 into 74a9673e36c7d25c28315cb54247a91792f8fbc0

carolynnmarie 6 years ago
parent
commit
986fc3701f
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>

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

@@ -1,7 +1,87 @@
1 1
 package com.zipcodewilmington.phonebook;
2
+import java.util.*;
2 3
 
3 4
 /**
4 5
  * Created by leon on 1/23/18.
5 6
  */
6 7
 public class PhoneBook {
8
+
9
+    //instance variable
10
+    private TreeMap<String, ArrayList<String>> treeMap;
11
+
12
+
13
+    // void "no arguments" constructor -creates empty map
14
+    public PhoneBook() {
15
+        this.treeMap= new TreeMap<String, ArrayList<String>>();
16
+    }
17
+
18
+    // single argument constructor -type Map -creates new map w/ same key/value mappings as argument
19
+    public PhoneBook(TreeMap treeMap) {
20
+        this.treeMap = treeMap;
21
+    }
22
+
23
+    //add a name & number entry, add number
24
+    public void addEntry(String name, ArrayList<String> number) {
25
+        if (!treeMap.containsKey(name)) {
26
+            treeMap.put(name, number);
27
+        }
28
+    }
29
+
30
+    //add numbers to existing entries
31
+    public void addNumberToEntry(String name, String number) {
32
+        for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) {
33
+            if (name.equals(entry.getKey())) {
34
+                ArrayList<String> value = entry.getValue();
35
+                value.add(number);
36
+            }
37
+        }
38
+    }
39
+
40
+    //remove numbers from existing entries
41
+    public void removeNumberFromEntry(String name, String number) {
42
+        for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) {
43
+            if (entry.getKey().equals(name)) {
44
+                if (entry.getValue().contains(number)) {
45
+                    entry.getValue().remove(number);
46
+                }
47
+            }
48
+        }
49
+    }
50
+
51
+    //remove entire entry
52
+    public void removeEntry(String name) {
53
+            treeMap.remove(name);
54
+    }
55
+
56
+    //lookup: find phone number from name
57
+    public ArrayList<String> lookup(String name) {
58
+        return treeMap.get(name);
59
+    }
60
+
61
+    //reverse lookup: find name from number
62
+    public String reverseLookup (String number) {
63
+        String name = "";
64
+        for(Map.Entry<String, ArrayList<String>> entry: treeMap.entrySet()) {
65
+            ArrayList<String> value = entry.getValue();
66
+            for(String revnumber: value) {
67
+                if(number.contains(revnumber)) {
68
+                    name = entry.getKey();
69
+                }
70
+            }
71
+        }
72
+        return name;
73
+    }
74
+
75
+    //print out all of the entries in PhoneBook
76
+    public String display(){
77
+        StringBuilder printOut = new StringBuilder();
78
+        for(Map.Entry<String, ArrayList<String>> entry: treeMap.entrySet()) {
79
+            printOut.append(entry.getKey())
80
+                    .append(" ")
81
+                    .append(entry.getValue().toString())
82
+                    .append("\n");
83
+        }
84
+        return printOut.toString();
85
+    }
86
+
7 87
 }

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

@@ -1,7 +1,147 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+import sun.nio.cs.ext.SJIS;
6
+
7
+import java.util.ArrayList;
8
+import java.util.Arrays;
9
+import java.util.TreeMap;
10
+
3 11
 /**
4 12
  * Created by leon on 1/23/18.
5 13
  */
6 14
 public class PhoneBookTest {
15
+    @Test
16
+    public void testDefaultConstructor() {
17
+        PhoneBook book = new PhoneBook();
18
+        Assert.assertNotNull(book);
19
+    }
20
+
21
+    @Test
22
+    public void testConstructorWithArgument() {
23
+        PhoneBook book = new PhoneBook();
24
+        ArrayList<String> number = new ArrayList<String>(Arrays.asList("302-555-2222","302-555-3456"));
25
+        book.addEntry("Bob", number);
26
+        Assert.assertNotNull(book);
27
+    }
28
+
29
+    @Test
30
+    public void testAddEntry() {
31
+        //Given
32
+        PhoneBook book = new PhoneBook();
33
+        ArrayList<String> expectedAddition = new ArrayList<String>(Arrays.asList("302-555-2222","302-555-3456"));
34
+        //When
35
+        book.addEntry("Bob", expectedAddition);
36
+        ArrayList<String> actualAddition = new ArrayList<String>(book.lookup("Bob"));
37
+        //Then
38
+        Assert.assertEquals(expectedAddition, actualAddition);
39
+    }
40
+
41
+    @Test
42
+    public void addNumberToEntry() {
43
+        //Given
44
+        PhoneBook book = new PhoneBook();
45
+        ArrayList<String> bobNumber = new ArrayList<String>(Arrays.asList("302-555-2234"));
46
+        book.addEntry("Bob", bobNumber);
47
+        //Expected
48
+        ArrayList<String> expectedAddition = new ArrayList<String>(Arrays.asList("302-555-2234","302-555-1111"));
49
+        //When
50
+        book.addNumberToEntry("Bob", "302-555-1111");
51
+        ArrayList<String> actualAddition = book.lookup("Bob");
52
+        //Then
53
+        Assert.assertEquals(expectedAddition, actualAddition);
54
+    }
55
+
56
+    @Test
57
+    public void removeNumberFromEntry() {
58
+        //Given
59
+        PhoneBook book = new PhoneBook();
60
+        ArrayList<String> bobNumber = new ArrayList<String>(Arrays.asList("302-555-2234","302-555-1111"));
61
+        book.addEntry("Bob", bobNumber);
62
+        //Expected
63
+        ArrayList<String> expectedAfterDeletion = new ArrayList<String>(Arrays.asList("302-555-2234"));
64
+        //When
65
+        book.removeNumberFromEntry("Bob", "302-555-1111");
66
+        ArrayList<String> actualAfterDeletion = book.lookup("Bob");
67
+        //Then
68
+        Assert.assertEquals(expectedAfterDeletion, actualAfterDeletion);
69
+    }
70
+
71
+    @Test
72
+    public void testRemoveEntry() {
73
+
74
+        //Given
75
+        PhoneBook book = new PhoneBook();
76
+        ArrayList<String> bobNumber = new ArrayList<String>(Arrays.asList("302-555-2222","302-555-3456"));
77
+        book.addEntry("Bob", bobNumber);
78
+        //Expected
79
+        String expectedValue = "";
80
+        //When
81
+        book.removeEntry("Bob");
82
+        String actualValue = book.display();
83
+        //Then
84
+        Assert.assertEquals(expectedValue, actualValue);
85
+    }
86
+
87
+    @Test
88
+    public void testLookup(){
89
+        //To test if lookup returns all of the person's phone numbers
90
+        //Given
91
+        PhoneBook book = new PhoneBook();
92
+        ArrayList<String> expectedNumber = new ArrayList<String>(Arrays.asList("302-555-2223","302-555-3456"));
93
+        book.addEntry("Bob", expectedNumber);
94
+        //When
95
+        ArrayList<String> actualNumber = new ArrayList<String>(book.lookup("Bob"));
96
+        //Then
97
+        Assert.assertEquals(expectedNumber, actualNumber);
98
+    }
99
+
100
+    @Test
101
+    public void testLookup2(){
102
+        //To test if lookup picks out the right person when there are multiple entries
103
+        //Given
104
+        PhoneBook book = new PhoneBook();
105
+        ArrayList<String> bobNumber = new ArrayList<String>(Arrays.asList("302-555-2223"));
106
+        book.addEntry("Bob", bobNumber);
107
+        //Expected
108
+        ArrayList<String> expectedNumber = new ArrayList<String>(Arrays.asList("302-555-9988"));
109
+        book.addEntry("Frank", expectedNumber);
110
+        //When
111
+        ArrayList<String> actualNumber = new ArrayList<String> (book.lookup("Frank"));
112
+        //Then
113
+        Assert.assertEquals(expectedNumber, actualNumber);
114
+    }
115
+
116
+    @Test
117
+    public void testreverseLookup(){
118
+        // to test if right person is returned when searching by one of multiple phone numbers
119
+        //Given
120
+        PhoneBook book = new PhoneBook();
121
+        ArrayList<String> bobNumber = new ArrayList<String>(Arrays.asList("302-555-2223","302-555-5555"));
122
+        String expectedName = "Bob";
123
+        book.addEntry(expectedName, bobNumber);
124
+        //When
125
+        String actualName = book.reverseLookup("302-555-2223");
126
+        //Then
127
+        Assert.assertEquals(expectedName, actualName);
128
+    }
129
+
130
+
131
+    @Test
132
+    public void testDisplay() {
133
+        //Given:
134
+        PhoneBook book = new PhoneBook();
135
+        ArrayList<String> bobNumber = new ArrayList<String>(Arrays.asList("302-555-1234","302-556-1245"));
136
+        book.addEntry("Bob", bobNumber);
137
+        ArrayList<String> frankNumber = new ArrayList<String>(Arrays.asList("777-555-1111"));
138
+        book.addEntry("Frank", frankNumber);
139
+        //Expected
140
+        String expectedNameNumber = "Bob " + bobNumber.toString() + "\n" + "Frank " + frankNumber.toString() + "\n";
141
+        //When
142
+        String actualNameNumber = book.display();
143
+        //Then
144
+        Assert.assertEquals(expectedNameNumber, actualNameNumber);
145
+    }
146
+
7 147
 }