Browse Source

Merge c79c69a07719120f7359ae38b18ba3a1c8d73acb into 74a9673e36c7d25c28315cb54247a91792f8fbc0

Luis J. Romero 6 years ago
parent
commit
0c9bc70ef7
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>

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

@@ -0,0 +1,57 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+import java.util.ArrayList;
4
+
5
+public class Person {
6
+
7
+    private String name;
8
+    protected ArrayList<String> phoneNumberList;
9
+
10
+    public Person(String name, String... phoneNumbers) {
11
+        this.name = name;
12
+        this.phoneNumberList = new ArrayList<String>();
13
+        for (String phoneNumber : phoneNumbers) {
14
+            this.phoneNumberList.add(phoneNumber);
15
+        }
16
+    }
17
+
18
+    public void setName(String name) {
19
+        this.name = name;
20
+    }
21
+
22
+    public String getName() {
23
+        return name;
24
+    }
25
+
26
+    public ArrayList<String> getPhoneNumberListArray() {
27
+        return phoneNumberList;
28
+    }
29
+
30
+    public String getPhoneNumberListString() {
31
+
32
+        StringBuilder sb = new StringBuilder();
33
+
34
+        for (String phoneNumber : phoneNumberList) {
35
+            sb.append("\t" + phoneNumber + "\n");
36
+        }
37
+        return sb.toString();
38
+    }
39
+
40
+    public void addPhoneNumbers(String... phoneNumbersToAdd) {
41
+        for (String phoneNumber : phoneNumbersToAdd) {
42
+            this.phoneNumberList.add(phoneNumber);
43
+        }
44
+
45
+    }
46
+
47
+    public void removePhoneNumbers(String... phoneNumbersToRemove) {
48
+        for (String phoneNumber : phoneNumbersToRemove) {
49
+            this.phoneNumberList.remove(phoneNumber);
50
+        }
51
+
52
+    }
53
+
54
+    // method to return phone number list?
55
+
56
+
57
+}

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

@@ -1,7 +1,67 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.Map;
4
+import java.util.TreeMap;
5
+import java.util.ArrayList;
6
+import java.util.Set;
7
+
3 8
 /**
4 9
  * Created by leon on 1/23/18.
5 10
  */
6 11
 public class PhoneBook {
12
+
13
+    protected TreeMap<String, Person> treeMap;
14
+    protected ArrayList<String> entryList;
15
+    protected Set<String> keySet;
16
+
17
+    public PhoneBook() {
18
+        this.treeMap = new TreeMap<String, Person>();
19
+        this.entryList = new ArrayList<String>();
20
+        this.keySet = treeMap.keySet();
21
+        //this.hashMap.put("Name_empty", new Person("Name_empty", "Phone_number_empty"));
22
+    }
23
+
24
+    //PART I
25
+    public Person lookup(String name) {
26
+        return treeMap.get(name);
27
+    }
28
+
29
+    public void addEntry(String name, String... phoneNumbers) {
30
+        treeMap.put(name, new Person(name, phoneNumbers));
31
+    }
32
+
33
+    // Changed method name for Part III
34
+    public void removeRecord(String name) {
35
+        treeMap.remove(name);
36
+    }
37
+
38
+    public Set<String> getKeySet() { //names and phone numbers
39
+        return treeMap.keySet();
40
+        //return all persons
41
+    }
42
+
43
+    public StringBuilder displayAllEntries() { //names and phone numbers
44
+        StringBuilder sb = new StringBuilder();
45
+
46
+        for (String key : treeMap.keySet()) {
47
+            Person p = treeMap.get(key);
48
+            sb.append(p.getName() + "\n");
49
+            sb.append(p.getPhoneNumberListString() + "\n");
50
+        }
51
+
52
+        return sb;
53
+        //call getentries
54
+    }
55
+
56
+    //PART II
57
+    public String reverseLookup(String phoneNumber) { //lookup names from phone numbers
58
+        for (String name : treeMap.keySet()) {
59
+            if (treeMap.get(name).phoneNumberList.contains(phoneNumber) == true) {
60
+                return name;
61
+                //break;
62
+            }
63
+        }
64
+        return "No_name";
65
+    }
66
+
7 67
 }

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

@@ -0,0 +1,100 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+import com.sun.tools.doclets.formats.html.SourceToHTMLConverter;
4
+import org.junit.Assert;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+import javax.sound.midi.Soundbank;
9
+import java.util.Arrays;
10
+
11
+/**
12
+ * Created by Luis J. Romero on 2/12/2018
13
+ */
14
+
15
+public class PersonTest {
16
+
17
+    private static Person person;
18
+
19
+    @Before
20
+    public void setup() {
21
+        this.person = new Person("John Smith", "(111) 222-3333");
22
+    }
23
+
24
+    @Test
25
+    public void setNameTest() {
26
+        String expected = "John Smith";
27
+        person.setName(expected);
28
+        String actual = person.getName();
29
+        Assert.assertEquals(expected, actual);
30
+    }
31
+
32
+    @Test
33
+    public void getNameTest() {
34
+        String expected = "John Smith";
35
+        String actual = person.getName();
36
+        Assert.assertEquals(expected, actual);
37
+    }
38
+
39
+    @Test
40
+    public void getPhoneNumberListArrayTest() {
41
+        this.person = new Person("Adam", "(000) 000-0000", "(011) 111-1111");
42
+
43
+        String expectedPhoneNumber1 = "(000) 000-0000";
44
+        String expectedPhoneNumber2 = "(011) 111-1111";
45
+
46
+        String actualPhoneNumber1 = person.getPhoneNumberListArray().get(0);
47
+        String actualPhoneNumber2 = person.getPhoneNumberListArray().get(1);
48
+
49
+        Assert.assertEquals(expectedPhoneNumber1, actualPhoneNumber1);
50
+        Assert.assertEquals(expectedPhoneNumber2, actualPhoneNumber2);
51
+    }
52
+
53
+    @Test
54
+    public void getPhoneNumberListArrayStringTest() {
55
+        this.person = new Person("Adam", "(000) 000-0000", "(011) 111-1111");
56
+
57
+        String expectedPhoneNumbers = "\t" + "(000) 000-0000" + "\n" +
58
+                                      "\t" + "(011) 111-1111" + "\n";
59
+        String actualPhoneNumbers = person.getPhoneNumberListString();
60
+
61
+//        System.out.println(expectedPhoneNumbers);
62
+//        System.out.println(person.getPhoneNumberListString());
63
+
64
+        Assert.assertEquals(expectedPhoneNumbers, actualPhoneNumbers);
65
+    }
66
+
67
+
68
+    @Test
69
+    public void addPhoneNumbersTest() {
70
+        this.person = new Person("John Smith", "(000) 000-0000");
71
+        String expectedPhoneNumberToAdd1 = "(111) 111-1111";
72
+        String expectedPhoneNumberToAdd2 = "(222) 222-2222";
73
+
74
+        this.person.addPhoneNumbers(expectedPhoneNumberToAdd1, expectedPhoneNumberToAdd2);
75
+
76
+        String actualPhoneNumberAdded1 = this.person.phoneNumberList.get(1);
77
+        String actualPhoneNumberAdded2 = this.person.phoneNumberList.get(2);
78
+
79
+        Assert.assertEquals(expectedPhoneNumberToAdd1, actualPhoneNumberAdded1);
80
+        Assert.assertEquals(expectedPhoneNumberToAdd2, actualPhoneNumberAdded2);
81
+
82
+    }
83
+
84
+    @Test
85
+    public void removePhoneNumbersTest() {
86
+        this.person = new Person("John Smith", "(000) 000-0000", "(111) 111-1111", "(222) 222-2222");
87
+        String expectedPhoneNumberToDelete1 = "(111) 111-1111";
88
+        String expectedPhoneNumberToDelete2 = "(222) 222-2222";
89
+
90
+        this.person.removePhoneNumbers(expectedPhoneNumberToDelete1, expectedPhoneNumberToDelete2);
91
+
92
+        boolean isPhoneNumberInList1 = this.person.phoneNumberList.contains(expectedPhoneNumberToDelete1);
93
+        boolean isPhoneNumberInList2 = this.person.phoneNumberList.contains(expectedPhoneNumberToDelete2);
94
+
95
+        Assert.assertFalse(isPhoneNumberInList1);
96
+        Assert.assertFalse(isPhoneNumberInList2);
97
+    }
98
+
99
+}
100
+

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

@@ -1,7 +1,134 @@
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.Arrays;
8
+
3 9
 /**
4 10
  * Created by leon on 1/23/18.
5 11
  */
6 12
 public class PhoneBookTest {
13
+
14
+    private static PhoneBook phonebook;
15
+
16
+    @Before
17
+    public void setup() {
18
+        this.phonebook = new PhoneBook() ;
19
+    }
20
+
21
+    @Test
22
+    public void defaultConstructorTest() {
23
+        Assert.assertNotNull(this.phonebook);
24
+    }
25
+
26
+
27
+    @Test
28
+    public void lookupTest() {
29
+        Person expectedPerson = new Person("Jane", "(111) 222-3333");
30
+        this.phonebook.addEntry("Jane", "(111) 222-3333");
31
+
32
+        String actualPersonName = expectedPerson.getName();
33
+        String actualPersonPhoneNumber = expectedPerson.phoneNumberList.get(0);
34
+
35
+        boolean actualPersonNameExists = this.phonebook.treeMap.containsKey("Jane");
36
+
37
+        Assert.assertTrue(actualPersonNameExists);
38
+    }
39
+
40
+
41
+    @Test
42
+    public void addEntryTest() {
43
+        this.phonebook.addEntry("Adam", "(000) 000-0000", "(111) 111-1111");
44
+
45
+        boolean actualPersonName = this.phonebook.treeMap.containsKey("Adam");
46
+        boolean actualPhoneNumber = this.phonebook.treeMap.containsValue(this.phonebook.treeMap.get("Adam"));
47
+        Assert.assertTrue(actualPersonName);
48
+        Assert.assertTrue(actualPhoneNumber);
49
+
50
+        String expectedPhoneNumber1 = "(000) 000-0000";
51
+        String expectedPhoneNumber2 = "(111) 111-1111";
52
+
53
+        String actualPhoneNumber1 = this.phonebook.treeMap.get("Adam").phoneNumberList.get(0);
54
+        String actualPhoneNumber2 = this.phonebook.treeMap.get("Adam").phoneNumberList.get(1);
55
+
56
+        Assert.assertEquals(expectedPhoneNumber1, actualPhoneNumber1);
57
+        Assert.assertEquals(expectedPhoneNumber2, actualPhoneNumber2);
58
+
59
+    }
60
+
61
+    @Test
62
+    public void removeRecordTest() {
63
+        this.phonebook.addEntry("Bob", "(333) 444-5555");
64
+        this.phonebook.removeRecord("Bob");
65
+
66
+        boolean actualPersonName = this.phonebook.treeMap.containsKey("Bob");
67
+        boolean actualPersonObject = this.phonebook.treeMap.containsValue(this.phonebook.treeMap.get("Bob"));
68
+
69
+        Assert.assertFalse(actualPersonName);
70
+        Assert.assertFalse(actualPersonObject);
71
+    }
72
+
73
+    @Test
74
+    public void getKeySetTest() {
75
+        this.phonebook.addEntry("Frank", "(000) 000-0000", "(100) 000-0000");
76
+        this.phonebook.addEntry("Gabriel", "(100) 000-0000", "(110) 000-0000");
77
+
78
+        System.out.println("getAllEntriesTest:");
79
+        String expectedKeySet = "[Frank, Gabriel]";
80
+        String actualKeySet = this.phonebook.getKeySet().toString();
81
+
82
+        Assert.assertEquals(expectedKeySet, actualKeySet);
83
+    }
84
+
85
+    @Test
86
+    public void displayAllEntriesTest() {
87
+        this.phonebook.addEntry("Herbert", "(200) 000-0000", "(210) 000-0000");
88
+        this.phonebook.addEntry("Isabel", "(300) 000-0000", "(310) 000-0000");
89
+
90
+        String expectedEntries = "Herbert" + "\n" +
91
+                                 "\t" + "(200) 000-0000" + "\n" +
92
+                                 "\t" + "(210) 000-0000" + "\n" +
93
+                                 "Isabel" + "\n" +
94
+                                 "\t" + "(300) 000-0000" + "\n" +
95
+                                 "\t" + "(310) 000-0000" + "\n";
96
+        String actualEntries = this.phonebook.displayAllEntries().toString();
97
+
98
+//        System.out.println("displayAllEntriesTest:");
99
+//        System.out.println(expectedEntries);
100
+//        System.out.println(actualEntries);
101
+
102
+        Assert.assertEquals(expectedEntries, actualEntries);
103
+    }
104
+
105
+    @Test
106
+    public void reverseLookupTest() {
107
+
108
+        // This method should allow you to look up names by the phone number associated with them.
109
+
110
+        // Given
111
+        String givenPhoneNumber = "(000) 111-2222";
112
+
113
+        // When
114
+        this.phonebook.addEntry("Ava", "(000) 111-2222");
115
+        Person expectedPerson = new Person("Ava", "(000) 111-2222");
116
+
117
+        String expectedName = "Ava";
118
+        String expectedPhoneNumber = "(000) 111-2222";
119
+
120
+        String actualNameFromPhoneBook = "Ava";
121
+        boolean actualPhoneNumberFromPhoneBook = this.phonebook.treeMap.get(actualNameFromPhoneBook).phoneNumberList.contains(expectedPhoneNumber);
122
+
123
+        Assert.assertEquals(expectedName, this.phonebook.reverseLookup(givenPhoneNumber));
124
+        // Then
125
+
126
+
127
+
128
+    }
129
+
7 130
 }
131
+
132
+
133
+
134
+