Browse Source

Merge 6e7269c33d1cedf3ad082719ed8a1d88696c0c38 into 74a9673e36c7d25c28315cb54247a91792f8fbc0

kfennimore 6 years ago
parent
commit
d00ea04d0c
No account linked to committer's email

+ 9
- 1
pom.xml View File

@@ -8,5 +8,13 @@
8 8
     <artifactId>phonebok</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
-
11
+    <dependencies>
12
+    <!-- https://mvnrepository.com/artifact/junit/junit -->
13
+    <dependency>
14
+        <groupId>junit</groupId>
15
+        <artifactId>junit</artifactId>
16
+        <version>4.12</version>
17
+        <scope>test</scope>
18
+    </dependency>
19
+    </dependencies>
12 20
 </project>

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

@@ -0,0 +1,32 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+public class Person {
4
+
5
+    private String name;
6
+    private String number;
7
+    private String number2;
8
+
9
+    public Person(String name, String number) {
10
+        this.name = name;
11
+        this.number = number;
12
+        this.number2 = number2;
13
+    }
14
+
15
+    public String getName() {
16
+        return name;
17
+    }
18
+
19
+    public void setName(String name) {
20
+        this.name = name;
21
+    }
22
+
23
+    public String getNumber() {
24
+        return number;
25
+    }
26
+
27
+    public void setNumber(String number) {
28
+        this.number = number;
29
+    }
30
+
31
+}
32
+

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

@@ -1,7 +1,56 @@
1 1
 package com.zipcodewilmington.phonebook;
2
+import java.util.*;
2 3
 
3 4
 /**
4
- * Created by leon on 1/23/18.
5
+ * Created by Kay.
5 6
  */
6 7
 public class PhoneBook {
7
-}
8
+
9
+    TreeMap<String, List<String>> phMap;
10
+
11
+    public PhoneBook() {
12
+
13
+        this.phMap = new TreeMap<String, List<String>>();
14
+    }
15
+
16
+    public void add(String name, String 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
+        }
25
+    }
26
+
27
+    public void remove(String name) {
28
+
29
+        phMap.remove(name);
30
+    }
31
+
32
+    public List<String> lookup(String name) {
33
+
34
+        return phMap.get(name);
35
+
36
+    }
37
+
38
+    public Set listNames() {
39
+
40
+        return phMap.keySet();
41
+    }
42
+
43
+    public Set listRecords() {
44
+
45
+        return phMap.entrySet();
46
+    }
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
+}

+ 38
- 0
src/test/java/com/zipcodewilmington/phonebook/PersonTest.java View File

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

+ 139
- 2
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

@@ -1,7 +1,144 @@
1 1
 package com.zipcodewilmington.phonebook;
2
+import org.junit.Assert;
3
+import org.junit.Test;
4
+import java.util.*;
2 5
 
3 6
 /**
4
- * Created by leon on 1/23/18.
7
+ * Created by Kay.
5 8
  */
6 9
 public class PhoneBookTest {
7
-}
10
+//instantiating new object for each test so that they are not reliant on each other. unique...
11
+    @Test
12
+    public void addTest() {
13
+        PhoneBook phTest = new PhoneBook();
14
+        String name = "Kay";
15
+        String number = "1234567890";
16
+        phTest.add(name, number);
17
+        Assert.assertTrue(phTest.lookup(name).contains(number));
18
+    }
19
+
20
+    @Test
21
+    public void addFailTest() {
22
+        PhoneBook phTest = new PhoneBook();
23
+        String name = "Kay";
24
+        String number = "1234567890";
25
+        phTest.add(name, number);
26
+        Assert.assertTrue(phTest.lookup("Pony") == null);
27
+    }
28
+
29
+    @Test
30
+    public void removeTest() {
31
+        PhoneBook phTest = new PhoneBook();
32
+        String name = "Bob";
33
+        String number = "3456784523";
34
+        //adding a test value key pair to test for removal
35
+        phTest.add(name, number);
36
+        Assert.assertTrue(phTest.lookup(name).contains(number));
37
+        //after confirmation that it has been added, it needs to be removed
38
+        phTest.remove(name);
39
+        Assert.assertTrue(phTest.lookup(name) == null);
40
+
41
+    }
42
+
43
+    @Test
44
+    public void lookupTest() {
45
+        PhoneBook phTest = new PhoneBook();
46
+        String name = "John";
47
+        String number = "8765930";
48
+        phTest.add(name, number);
49
+        Assert.assertTrue(phTest.lookup(name).contains(number));
50
+    }
51
+
52
+
53
+    @Test
54
+    public void listNameTest() {
55
+        PhoneBook phTest = new PhoneBook();
56
+        phTest.add("Joe", "12334");
57
+        phTest.add("Kay", "45873423");
58
+        phTest.add("Elisha", "234234");
59
+        phTest.add("Nico", "24234");
60
+
61
+        //Set is used because it is a method of tree map HAD TO IMPORT
62
+        Set<String> names = phTest.listNames();
63
+        Assert.assertTrue(names.size() == 4);
64
+        Assert.assertTrue(names.contains("Joe"));
65
+        Assert.assertTrue(names.contains("Kay"));
66
+        Assert.assertTrue(names.contains("Elisha"));
67
+        Assert.assertTrue(names.contains("Nico"));
68
+    }
69
+
70
+    @Test
71
+    public void listNamesFailTest() {
72
+        PhoneBook phTest = new PhoneBook();
73
+        phTest.add("Cat", "12334");
74
+        phTest.add("Dog", "45873423");
75
+
76
+
77
+        //Set is used because it is a method of tree map HAD TO IMPORT
78
+        Set<String> names = phTest.listNames();
79
+        Assert.assertTrue(names.size() == 2);
80
+        Assert.assertFalse(names.contains("Joe"));
81
+        Assert.assertFalse(names.contains("Kay"));
82
+    }
83
+
84
+    @Test
85
+    public void listRecordsTest() {
86
+        PhoneBook phTest = new PhoneBook();
87
+        //added two new entries to test
88
+        phTest.add("Dumbo", "487542");
89
+        phTest.add("Blumbo", "1800234583");
90
+
91
+        //turned into an array to iterate through.
92
+        Object[] array = phTest.listRecords().toArray();
93
+        //to.Array returns Object[] array
94
+        for (int i = 0; i < array.length; i++) {
95
+            //Object that was in array was Map.Entry: a record on map (key value pair)
96
+            Map.Entry<String, List<String>> record = (Map.Entry) array[i];
97
+            //0 index = Laura because treemap is sorted alphabetically
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
+    }
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));
138
+        }
139
+
140
+
141
+    }
142
+
143
+
144
+    }