Parcourir la source

second commit

mbowe4 il y a 6 ans
Parent
révision
1fb39fe832

+ 48
- 0
src/main/java/com/zipcodewilmington/phonebook/Contact.java Voir le fichier

@@ -0,0 +1,48 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+
4
+
5
+import java.util.ArrayList;
6
+import java.util.List;
7
+import java.util.TreeMap;
8
+
9
+public class Contact {
10
+
11
+    private String name;
12
+    private ArrayList<String> phoneNumbers;
13
+
14
+
15
+    // Constructor
16
+    Contact(String name) {
17
+        this.name = name;
18
+        phoneNumbers = new ArrayList<String>();
19
+    }
20
+
21
+    // Getter
22
+    public String getName() {
23
+        return name;
24
+    }
25
+
26
+    // Getter
27
+    public ArrayList<String> getPhoneNumbers() {
28
+        return phoneNumbers;
29
+
30
+    }
31
+
32
+    public void addPhoneNumber(String phoneNumber) {
33
+        phoneNumbers.add(phoneNumber);
34
+    }
35
+
36
+    public void removePhoneNumber(String phoneNumber) {
37
+        phoneNumbers.remove(phoneNumber);
38
+    }
39
+
40
+
41
+
42
+
43
+
44
+}
45
+
46
+
47
+
48
+

+ 0
- 28
src/main/java/com/zipcodewilmington/phonebook/Person.java Voir le fichier

@@ -1,28 +0,0 @@
1
-package com.zipcodewilmington.phonebook;
2
-
3
-import java.util.HashMap;
4
-
5
-public class Person {
6
-
7
-    private String name;
8
-    private String phoneNumber;
9
-
10
-
11
-        public Person(String name, String phoneNumber) {
12
-
13
-            this.name = name;
14
-            this.phoneNumber = phoneNumber;
15
-        }
16
-
17
-        public String getName() {
18
-            return name;
19
-        }
20
-
21
-
22
-        public String getPhoneNumber() {
23
-            return phoneNumber;
24
-        }
25
-
26
-
27
-
28
-}

+ 53
- 14
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Voir le fichier

@@ -1,5 +1,7 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.Collection;
3 5
 import java.util.HashMap;
4 6
 import java.util.TreeMap;
5 7
 
@@ -10,32 +12,69 @@ import java.util.TreeMap;
10 12
 
11 13
 public class PhoneBook {
12 14
 
13
-    TreeMap<String, String> contacts;
15
+    private TreeMap<String, Contact> contactsMap;
14 16
 
15
-    public PhoneBook () {
16
-        contacts = new TreeMap();
17
+    // Constructor
18
+    public PhoneBook() {
19
+        contactsMap = new TreeMap<String, Contact>();
17 20
     }
18 21
 
19
-    public String addPerson(String name, String phoneNumber) {
20
-        return contacts.put(name, phoneNumber);
22
+
23
+    public void addContact(String name, String phoneNumber) {
24
+        //Create new contact instance, call addPhoneNumber method
25
+        Contact contact = new Contact(name);
26
+        contact.addPhoneNumber(phoneNumber);
27
+        // Call getName method to add key, contact is value
28
+        contactsMap.put(contact.getName(), contact);
21 29
     }
22 30
 
23
-    public String lookupPhoneNumber(String name) {
24
-        return contacts.get(name);
31
+
32
+    public int getNumberOfContacts() {
33
+        return contactsMap.size();
25 34
     }
26 35
 
27
-    public String removePerson(String name) {
28
-        return contacts.remove(name);
36
+
37
+    public void removeContact(String name) {
38
+        contactsMap.remove(name);
29 39
     }
30 40
 
31
-    public void  listNamesAndNums(String name, String phoneNumber) {
32 41
 
33
-        for (String key : contacts.keySet()){
34
-            String value = contacts.get(key);
35
-            System.out.println(key + ": " + value);
36
-        }
42
+    public ArrayList lookupByName(String name) {
43
+        return contactsMap.get(name).getPhoneNumbers();
44
+    }
45
+
37 46
 
47
+    public Collection listAllContacts() {
48
+        return contactsMap.values();
38 49
     }
39 50
 
51
+    public String lookupByNumber(String phoneNumber) {
52
+        // from phone book
53
+        // into contactsMap
54
+        // into Contact object
55
+        // into Contact object's arraylist
56
+        // evaluate values in arraylist
57
+
58
+        Collection<Contact> contacts = contactsMap.values();
59
+        for (Contact contact : contacts) {
60
+            ArrayList<String> phoneNumbers = contact.getPhoneNumbers();
61
+            if(phoneNumbers.contains(phoneNumber)) {
62
+                return contact.getName();
63
+            }
64
+        }
65
+    }
66
+
67
+
68
+//
69
+//    public void listNamesAndNums(String name, String phoneNumber) {
70
+//
71
+//        for (String key : contacts.keySet()){
72
+//            String value = contacts.get(key);
73
+//            System.out.println(key + ": " + value);
74
+//        }
75
+//
76
+//    }
77
+//
78
+
40 79
 
41 80
 }

+ 62
- 0
src/test/java/com/zipcodewilmington/phonebook/ContactTests.java Voir le fichier

@@ -0,0 +1,62 @@
1
+package com.zipcodewilmington.phonebook;
2
+import org.junit.Assert;
3
+import org.junit.Before;
4
+import org.junit.Test;
5
+
6
+import java.util.ArrayList;
7
+
8
+public class ContactTests {
9
+
10
+    @Test
11
+    public void getNameTest() {
12
+
13
+        Contact contact1 = new Contact("Bob");
14
+
15
+        String actualName = contact1.getName();
16
+        Assert.assertEquals("Bob", actualName);
17
+    }
18
+
19
+    @Test
20
+    public void getPhoneNumbersTest() {
21
+
22
+        Contact contact1 = new Contact("Madeline");
23
+        Assert.assertTrue(contact1.getPhoneNumbers().isEmpty());
24
+    }
25
+
26
+    @Test
27
+    public void addPhoneNumbersTest() {
28
+
29
+    // Actual:
30
+    // Create new contact instance
31
+        Contact contact1 = new Contact("Madeline");
32
+
33
+    // Call addPhoneNumber method to add phone numbers to instance
34
+        contact1.addPhoneNumber("234234");
35
+        contact1.addPhoneNumber("767884");
36
+
37
+    // Call getMethod to see if numbers were actually added
38
+        ArrayList<String> actualPhoneNumbers = contact1.getPhoneNumbers();
39
+
40
+    // Expected:
41
+    // Create new ArrayList to test
42
+        ArrayList expectedPhoneNumbers = new ArrayList<String>();
43
+
44
+    // Add phone numbers to new ArrayList
45
+        expectedPhoneNumbers.add("234234");
46
+        expectedPhoneNumbers.add("767884");
47
+
48
+        Assert.assertEquals( expectedPhoneNumbers, actualPhoneNumbers);
49
+
50
+    }
51
+
52
+    @Test
53
+    public void removePhoneNumber() {
54
+        Contact contact1 = new Contact("Jerry");
55
+        contact1.addPhoneNumber("89089");
56
+
57
+        ArrayList<String> expectedPhoneNumbers = new ArrayList<String>();
58
+        expectedPhoneNumbers.remove("89089");
59
+
60
+    }
61
+
62
+}

+ 143
- 21
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Voir le fichier

@@ -1,8 +1,13 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+
3 4
 import org.junit.Assert;
4 5
 import org.junit.Test;
5 6
 
7
+import java.util.ArrayList;
8
+import java.util.Collection;
9
+import java.util.TreeMap;
10
+
6 11
 
7 12
 /**
8 13
  * Created by leon on 1/23/18.
@@ -10,36 +15,153 @@ import org.junit.Test;
10 15
 public class PhoneBookTest {
11 16
 
12 17
     @Test
13
-    public void testConstructor() {
14
-        // Given
15
-        String expectedName = "";
16
-        String expectedPhoneNumber = "";
18
+    public void addContactTest() {
19
+
20
+        PhoneBook testPhoneBook = new PhoneBook();
21
+
22
+        Contact contact1 = new Contact("Jerry");
23
+        contact1.addPhoneNumber("1234");
24
+
25
+        // Actual
26
+        testPhoneBook.addContact("Jerry", "43234");
27
+
28
+    }
29
+
30
+    @Test
31
+    public void getNumberOfContactsTest() {
32
+
33
+        PhoneBook testPhoneBook = new PhoneBook();
34
+
35
+        testPhoneBook.addContact("Jerry", "78998");
36
+        testPhoneBook.addContact("Newman", "90980");
37
+        testPhoneBook.addContact("Elaine", "432345");
38
+
39
+        int expectedNumberOfContacts = 3;
40
+        //int actualNumberOfContacts =
41
+    }
42
+
43
+    @Test
44
+    public void lookupByNameTest() {
45
+
46
+        PhoneBook testPhoneBook = new PhoneBook();
47
+        testPhoneBook.addContact("Madeline", "898098");
48
+
49
+
50
+        ArrayList expectedLookupByNameResult = new ArrayList();
51
+        ArrayList actualLookupByNameResult = testPhoneBook.lookupByName("Madeline");
52
+
53
+    }
54
+
55
+    @Test
56
+    public void lookupByNumberTest() {
57
+
58
+        PhoneBook testPhoneBook = new PhoneBook();
59
+
17 60
 
18
-        // When
19
-        Person person = new Person(expectedName, expectedPhoneNumber);
20 61
 
21
-        // Then
22
-        String actualName = person.getName();
23
-        String actualPhoneNumber = person.getPhoneNumber();
24 62
 
25
-        Assert.assertEquals(expectedName, actualName);
26
-        Assert.assertEquals(expectedPhoneNumber, actualPhoneNumber);
27 63
     }
28 64
 
29 65
     @Test
30
-    public void testConstructorWithNameAndPhonNumber() {
66
+    public void removeContactTest() {
31 67
         // Given
32
-        String expectedName = "Leon";
33
-        String expectedPhoneNumber = "123";
68
+        String personNameToRemove = "Bob";
69
+        int expectedListSize = 0;
34 70
 
35 71
         // When
36
-        Person person = new Person(expectedName, expectedPhoneNumber);
72
+        PhoneBook testPhoneBook = new PhoneBook();
73
+        testPhoneBook.addContact(personNameToRemove, "23423");
74
+        testPhoneBook.removeContact(personNameToRemove);
75
+
76
+
77
+
78
+        Assert.assertEquals(expectedListSize, testPhoneBook.getNumberOfContacts());
79
+    }
80
+
81
+    @Test
82
+    public void listAllContactsTest() {
83
+
84
+        PhoneBook testPhoneBook = new PhoneBook();
85
+        testPhoneBook.addContact("Kramer", "23423");
86
+
87
+        Collection actualContactsList = testPhoneBook.listAllContacts();
88
+
89
+
90
+        // Actual
91
+        // Create object
92
+        Collection expectedContactsList = new ArrayList<Contact>();
93
+        Contact contact1 = new Contact("Kramer");
94
+
95
+
96
+        contact1.addPhoneNumber("23423");
97
+
98
+        // add contacts to expectedList
99
+        expectedContactsList.add(contact1);
100
+
101
+
102
+        Assert.assertEquals(expectedContactsList, actualContactsList);
103
+        }
104
+
105
+
37 106
 
38
-        // Then
39
-        String actualPhoneNumber = person.getPhoneNumber();
40
-        String actualName = person.getName();
41 107
 
42
-        Assert.assertEquals(expectedPhoneNumber, actualPhoneNumber);
43
-        Assert.assertEquals(expectedName, actualName);
44 108
     }
45
-}
109
+
110
+
111
+    //addContact ---- done
112
+    //removeContact ----- done
113
+    //lookupByName ----- done
114
+    //lookupByNumber
115
+    //addPhoneNumber ---- done
116
+    //removePhoneNumber ---- done
117
+    //list all entries (names & phone numbers) ----- done
118
+
119
+
120
+//    @Test
121
+//    public void testGetName() {
122
+//        // Given
123
+//        String expectedName = "Bob";
124
+//
125
+//
126
+//        // When
127
+//        Person person = new Person("Bob", ;
128
+//
129
+//        // Then
130
+//        String actualName = person.getName();
131
+//        Assert.assertEquals(expectedName, actualName);
132
+//
133
+//    }
134
+
135
+
136
+    //    @Test
137
+//    public void testGetPhoneNumber() {
138
+//        // Given
139
+//        String expectedName = "Madeline";
140
+//        String expectedPhoneNumber = "123";
141
+//
142
+//        // When
143
+//        Person person = new Person(expectedName, expectedPhoneNumber);
144
+//
145
+//        // Then
146
+//        String actualPhoneNumber = person.getPhoneNumber();
147
+//        String actualName = person.getName();
148
+//
149
+//        Assert.assertEquals(expectedPhoneNumber, actualPhoneNumber);
150
+//        Assert.assertEquals(expectedName, actualName);
151
+//    }
152
+//
153
+
154
+//
155
+//
156
+//
157
+//    @Test
158
+//    public void testLookup() {
159
+//        PhoneBook testPB = new PhoneBook();
160
+//        testPB.addPerson("bob", "123");
161
+//
162
+//        String actualPhoneNumber = testPB.lookupPhoneNumber("Bob");
163
+//        Assert.assertEquals("123", actualPhoneNumber);
164
+//
165
+//    }
166
+
167
+