Browse Source

Not sure if previous went through but this is up to part 2

Brian He 6 years ago
parent
commit
f0146f78a9

+ 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>

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

@@ -1,7 +1,56 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
-/**
4
- * Created by leon on 1/23/18.
5
- */
3
+import java.util.Map;
4
+import java.util.TreeMap;
5
+import java.util.ArrayList;
6
+
6 7
 public class PhoneBook {
8
+
9
+    TreeMap<String, String> phoneBookList = new TreeMap<String, String>();
10
+
11
+    public void addEntry(String name, String number) {
12
+        phoneBookList.put(name, number);
13
+        //Look to see if person exists
14
+        //Get array list value
15
+    }
16
+
17
+    public void removeEntry(String name, String number) {
18
+        phoneBookList.remove(name, number);
19
+    }
20
+
21
+    public String lookUp(String name) {
22
+        return phoneBookList.get(name);
23
+    }
24
+
25
+    public String listNames() {
26
+        return phoneBookList.keySet().toString();
27
+    }
28
+
29
+    public String listAll() {
30
+
31
+        StringBuilder sb = new StringBuilder();
32
+        for(Map.Entry<String, String> entry : phoneBookList.entrySet()) {
33
+            sb.append(entry.getKey() + entry.getValue());
34
+            sb.append("\n");
35
+        }
36
+        return sb.toString();
37
+
38
+    }
39
+
40
+    public String reverseLookUp(String number) {
41
+        for(Map.Entry<String, String>entry : phoneBookList.entrySet()) {
42
+            if(entry.getValue() == number) {
43
+                return entry.getKey();
44
+            }
45
+        }
46
+        return "That number is not in the phone book";
47
+    }
48
+
49
+    public static void main(String[] args) {
50
+        PhoneBook pb1 = new PhoneBook();
51
+        pb1.addEntry("Brian", "12345678");
52
+        pb1.addEntry("Lawrence", "23458984");
53
+        pb1.addEntry("Anthony", "12381927");
54
+
55
+    }
7 56
 }

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

@@ -1,7 +1,110 @@
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 = number;
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 = number;
33
+        String actual = phoneBookTest.lookUp(name);
34
+        Assert.assertEquals(expected, actual);
35
+    }
36
+
37
+    @Test
38
+    public void removeEntryTest() {
39
+        String name = "Person 1";
40
+        String number = "8942418";
41
+        phoneBookTest.removeEntry(name, number);
42
+        String expected = null;
43
+        String actual = phoneBookTest.lookUp(name);
44
+        Assert.assertEquals(expected, actual);
45
+
46
+    }
47
+
48
+    @Test
49
+    public void removeEntryTest2() {
50
+        String name = "Person 2";
51
+        String number = "12387874";
52
+        phoneBookTest.removeEntry(name, number);
53
+        String expected = null;
54
+        String actual = phoneBookTest.lookUp(name);
55
+        Assert.assertEquals(expected, actual);
56
+    }
57
+
58
+    @Test
59
+    public void listNamesTest() {
60
+        String name = "Person 1";
61
+        String number = "1234567";
62
+        phoneBookTest.addEntry(name, number);
63
+        String name2 = "Person 2";
64
+        String number2 = "2345678";
65
+        phoneBookTest.addEntry(name2, number2);
66
+        String name3 = "Person 3";
67
+        String number3 = "34567898";
68
+        phoneBookTest.addEntry(name3, number3);
69
+        String expected = "[Person 1, Person 2, Person 3]";
70
+        String actual = phoneBookTest.listNames();
71
+        Assert.assertEquals(expected, actual);
72
+
73
+    }
74
+
75
+    @Test
76
+    public void listAllTest() {
77
+        String name = "Person 1";
78
+        String number = "1234567";
79
+        phoneBookTest.addEntry(name, number);
80
+        String name2 = "Person 2";
81
+        String number2 = "2345678";
82
+        phoneBookTest.addEntry(name2, number2);
83
+        String name3 = "Person 3";
84
+        String number3 = "34567898";
85
+        phoneBookTest.addEntry(name3, number3);
86
+        String expected = "Person 11234567\nPerson 22345678\nPerson 334567898\n";
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
+    }
7 110
 }