Browse Source

Merge 1a80ebb1622e9178b64e78c3069af3afd417cbee into 74a9673e36c7d25c28315cb54247a91792f8fbc0

Brian 6 years ago
parent
commit
475c6dbdf0
No account linked to committer's email

+ 19
- 0
pom.xml View File

@@ -7,6 +7,25 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>phonebok</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.8</source>
17
+                    <target>1.8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
22
+    <dependencies>
23
+        <dependency>
24
+            <groupId>junit</groupId>
25
+            <artifactId>junit</artifactId>
26
+            <version>4.12</version>
27
+        </dependency>
28
+    </dependencies>
10 29
 
11 30
 
12 31
 </project>

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

@@ -0,0 +1,28 @@
1
+/*package com.zipcodewilmington.phonebook;
2
+
3
+public class Person {
4
+    public final String name;
5
+    public final PhoneNumbers phoneNumbers;
6
+
7
+    public Person(String name, PhoneNumbers phoneNumbers) {
8
+        this.name = name;
9
+        this.phoneNumbers = phoneNumbers;
10
+    }
11
+
12
+    public void add(String phoneNumber) {
13
+        phoneNumbers.addToContacts(phoneNumber);
14
+    }
15
+
16
+    public void remove(String phoneNumber) {
17
+        phoneNumbers.removeFromContacts(phoneNumber);
18
+    }
19
+
20
+    public String getName() {
21
+        return name;
22
+    }
23
+
24
+    public PhoneNumbers getPhoneNumber() {
25
+        return phoneNumbers;
26
+    }
27
+}
28
+*/

+ 56
- 3
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -1,7 +1,60 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
-/**
4
- * Created by leon on 1/23/18.
5
- */
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+import java.util.Map;
6
+import java.util.TreeMap;
7
+
6 8
 public class PhoneBook {
9
+
10
+    TreeMap<String, List<String>> phoneBookList = new TreeMap<String, List<String>>();
11
+
12
+    public void addEntry(String name, String ...phoneNumber) {
13
+        List<String> numberList = new ArrayList<String>();
14
+        for(String number : phoneNumber) {
15
+            numberList.add(number);
16
+        }
17
+        phoneBookList.put(name, numberList);
18
+    }
19
+
20
+    public void removeEntry(String name) {
21
+        phoneBookList.remove(name);
22
+    }
23
+
24
+    public String lookUp(String name) {
25
+        return phoneBookList.get(name).toString();
26
+    }
27
+
28
+    public String listNames() {
29
+        return phoneBookList.keySet().toString();
30
+    }
31
+
32
+    public String listAll() {
33
+
34
+        StringBuilder sb = new StringBuilder();
35
+        for(Map.Entry<String, List<String>> entry : phoneBookList.entrySet()) {
36
+            sb.append(entry.getKey() + entry.getValue());
37
+            sb.append("\n");
38
+        }
39
+        return sb.toString();
40
+    }
41
+
42
+    public String reverseLookUp(String number) {
43
+        for(Map.Entry<String, List<String>> entry : phoneBookList.entrySet()) {
44
+            if(entry.getValue().contains(number)) {
45
+                return entry.getKey();
46
+            }
47
+        }
48
+        return "That number is not in the phone book";
49
+    }
50
+
51
+    public void removeOneNumber(String name, String number) {
52
+        int i = 0;
53
+        for (String phoneNumber : phoneBookList.get(name)) {
54
+            if(phoneNumber == number) {
55
+                phoneBookList.get(name).remove(i);
56
+            }
57
+            i++;
58
+        }
59
+    }
7 60
 }

+ 17
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneNumbers.java View File

@@ -0,0 +1,17 @@
1
+/*package com.zipcodewilmington.phonebook;
2
+
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+
6
+public class PhoneNumbers {
7
+    List<String> contactList = new ArrayList<String>();
8
+
9
+    public void addToContacts(String phoneNumber) {
10
+        contactList.add(phoneNumber);
11
+    }
12
+
13
+    public void removeFromContacts(String phoneNumber) {
14
+        contactList.remove(phoneNumber);
15
+    }
16
+}
17
+*/

+ 122
- 3
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

@@ -1,7 +1,126 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
-/**
4
- * Created by leon on 1/23/18.
5
- */
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
6 7
 public class PhoneBookTest {
8
+
9
+    private static PhoneBook phoneBookTest;
10
+
11
+    @Before
12
+    public void createPhoneBook() {
13
+        this.phoneBookTest = new PhoneBook();
14
+    }
15
+
16
+    @Test
17
+    public void testAddEntry() {
18
+        String name = "Person 1";
19
+        String number = "1234567";
20
+        phoneBookTest.addEntry(name, number);
21
+        String expected = "[1234567]";
22
+        String actual = phoneBookTest.lookUp(name);
23
+        Assert.assertEquals(expected, actual);
24
+
25
+    }
26
+
27
+    @Test
28
+    public void testAddEntry2() {
29
+        String name = "Person 2";
30
+        String number = "87654321";
31
+        phoneBookTest.addEntry(name, number);
32
+        String expected = "[87654321]";
33
+        String actual = phoneBookTest.lookUp(name);
34
+        Assert.assertEquals(expected, actual);
35
+    }
36
+
37
+    @Test(expected = NullPointerException.class)
38
+    public void removeEntryTest() {
39
+        String name = "Person 1";
40
+        String number = "8942418";
41
+        phoneBookTest.addEntry(name, number);
42
+        phoneBookTest.removeEntry(name);
43
+        phoneBookTest.lookUp(name);
44
+
45
+    }
46
+
47
+    @Test(expected = NullPointerException.class)
48
+    public void removeEntryTest2() {
49
+        String name = "Person 2";
50
+        String number = "12387874";
51
+        phoneBookTest.addEntry(name, number);
52
+        phoneBookTest.removeEntry(name);
53
+        phoneBookTest.lookUp(name);
54
+    }
55
+
56
+    @Test
57
+    public void listNamesTest() {
58
+        String name = "Person 1";
59
+        String number = "1234567";
60
+        phoneBookTest.addEntry(name, number);
61
+        String name2 = "Person 2";
62
+        String number2 = "2345678";
63
+        phoneBookTest.addEntry(name2, number2);
64
+        String name3 = "Person 3";
65
+        String number3 = "34567898";
66
+        phoneBookTest.addEntry(name3, number3);
67
+        String expected = "[Person 1, Person 2, Person 3]";
68
+        String actual = phoneBookTest.listNames();
69
+        Assert.assertEquals(expected, actual);
70
+
71
+    }
72
+
73
+    @Test
74
+    public void listAllTest() {
75
+        String name = "Person 1";
76
+        String number = "1234567";
77
+        String name2 = "Person 2";
78
+        String number2 = "2345678";
79
+        String name3 = "Person 3";
80
+        String number3 = "34567898";
81
+        String number4 = "5343677";
82
+        phoneBookTest.addEntry(name, number);
83
+        phoneBookTest.addEntry(name2, number2);
84
+        phoneBookTest.addEntry(name3, number3, number4);
85
+        String expected = "Person 1[1234567]\nPerson 2[2345678]\nPerson 3[34567898, 5343677]\n";
86
+        System.out.println(expected);
87
+        String actual = phoneBookTest.listAll();
88
+        Assert.assertEquals(expected, actual);
89
+    }
90
+
91
+    @Test
92
+    public void reverseLookUpTest() {
93
+        String name = "Person 1";
94
+        String number = "1234567";
95
+        phoneBookTest.addEntry(name, number);
96
+        String expected = "Person 1";
97
+        String actual = phoneBookTest.reverseLookUp(number);
98
+        Assert.assertEquals(expected, actual);
99
+    }
100
+
101
+    @Test
102
+    public void reverseLookUpTest2() {
103
+        String name = null;
104
+        String number = null;
105
+        String expected = "That number is not in the phone book";
106
+        String actual = phoneBookTest.reverseLookUp(number);
107
+        Assert.assertEquals(expected, actual);
108
+
109
+    }
110
+
111
+    @Test
112
+    public void removeOneNumberTest() {
113
+        String name = "Brian";
114
+        String number = "1234567";
115
+        String number2 = "2345678";
116
+        String number3 = "3456789";
117
+        phoneBookTest.addEntry(name, number, number2, number3);
118
+        phoneBookTest.removeOneNumber(name, number2);
119
+        String expected = "[1234567, 3456789]";
120
+        String actual = phoneBookTest.lookUp(name);
121
+        Assert.assertEquals(expected, actual);
122
+
123
+
124
+    }
125
+
7 126
 }