Browse Source

Phonebook tests complete

Keith Brinker 6 years ago
parent
commit
a545be64d1

+ 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>RELEASE</version>
15
+        </dependency>
16
+    </dependencies>
10 17
 
11 18
 
12 19
 </project>

+ 18
- 0
src/main/java/com/zipcodewilmington/phonebook/Main.java View File

@@ -0,0 +1,18 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+public class Main {
4
+
5
+    public static void main(String[] args) {
6
+
7
+        PhoneBook tester = new PhoneBook();
8
+        tester.add("Keith", "555-666-7777");
9
+        tester.add("John", "555-612-7777");
10
+        tester.add("Mike", "555-623-7777");
11
+        tester.add("Steve", "555-645-7777");
12
+       String s = tester.listNames();
13
+        System.out.println(s);
14
+
15
+        s = tester.listNamesAndNumbers();
16
+        System.out.println(s);
17
+    }
18
+}

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

@@ -1,7 +1,58 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.TreeMap;
4
+import java.util.Set;
5
+
3 6
 /**
4 7
  * Created by leon on 1/23/18.
5 8
  */
6 9
 public class PhoneBook {
10
+
11
+        String name;
12
+        String phonenum;
13
+        TreeMap<String, String> contacts = new TreeMap<String, String>();
14
+
15
+
16
+
17
+public PhoneBook() {
18
+}
19
+
20
+ public void add(String name, String phonenum){
21
+       contacts.put(name, phonenum);
22
+ }
23
+
24
+ public void deleter(String name){
25
+        contacts.remove(name);
26
+ }
27
+
28
+ public String lookup(String name){
29
+        return contacts.get(name);
30
+
31
+ }
32
+
33
+ public String listNames(){
34
+    StringBuilder namesnumbers = new StringBuilder();
35
+        Set<String> keys = contacts.keySet();
36
+        for(String key: keys)
37
+
38
+        {
39
+
40
+            namesnumbers.append(key+"\n");
41
+        }
42
+        return namesnumbers.toString();
43
+ }
44
+
45
+    public String listNamesAndNumbers(){
46
+        StringBuilder namesnumbers = new StringBuilder();
47
+        Set<String> keys = contacts.keySet();
48
+        for(String key: keys)
49
+
50
+        {
51
+
52
+            namesnumbers.append(key+" "+contacts.get(key)+"\n");
53
+        }
54
+        return namesnumbers.toString();
55
+    }
56
+
57
+
7 58
 }

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

@@ -1,7 +1,81 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import java.util.TreeMap;
3 8
 /**
4 9
  * Created by leon on 1/23/18.
5 10
  */
11
+
6 12
 public class PhoneBookTest {
13
+
14
+    PhoneBook testbook;
15
+    @Before
16
+    public void setUp(){
17
+        testbook = new PhoneBook();
18
+    }
19
+
20
+    @Test
21
+    public void testPhoneBookAddName() {
22
+        // Given
23
+        PhoneBook testbook = new PhoneBook();
24
+       testbook.add("Keith", "555-666-7777");
25
+
26
+      String testNumber = testbook.lookup("Keith");
27
+      Assert.assertTrue(testNumber.equals("555-666-7777"));
28
+
29
+    }
30
+@Test
31
+    public void testPhoneBookDelete(){
32
+        PhoneBook testbook = new PhoneBook();
33
+        testbook.add("Keith", "555-666-7777");
34
+        testbook.deleter("Keith");
35
+        String testnumber = testbook.lookup("Keith");
36
+        Assert.assertEquals(null, testnumber);
37
+    }
38
+@Test
39
+    public void testPhoneBookLookUp(){
40
+        PhoneBook testbook = new PhoneBook();
41
+        testbook.add("Keith", "555-666-7777");
42
+        String testnumber = testbook.lookup("Keith");
43
+        Assert.assertEquals("555-666-7777", testnumber);
44
+    }
45
+
46
+    @Test
47
+    public void testPhoneBookList(){
48
+
49
+        PhoneBook testbook = new PhoneBook();
50
+        testbook.add("Keith", "555-666-7777");
51
+        testbook.add("John", "555-612-7777");
52
+        testbook.add("Mike", "555-623-7777");
53
+        testbook.add("Steve", "555-645-7777");
54
+
55
+        String expected = "John 555-612-7777\n" +
56
+                "Keith 555-666-7777\n" +
57
+                "Mike 555-623-7777\n" +
58
+                "Steve 555-645-7777\n";
59
+        String actual = testbook.listNamesAndNumbers();
60
+        Assert.assertEquals(expected, actual);
61
+    }
62
+
63
+    @Test
64
+    public void testPhoneBookNames(){
65
+
66
+        PhoneBook testbook = new PhoneBook();
67
+        testbook.add("Keith", "555-666-7777");
68
+        testbook.add("John", "555-612-7777");
69
+        testbook.add("Mike", "555-623-7777");
70
+        testbook.add("Steve", "555-645-7777");
71
+
72
+        String expected = "John\n" +
73
+                "Keith\n" +
74
+                "Mike\n" +
75
+                "Steve\n";
76
+        String actual = testbook.listNames();
77
+        Assert.assertEquals(expected, actual);
78
+    }
79
+
80
+
7 81
 }