Browse Source

had to redo everything : (

April Rivera 6 years ago
parent
commit
fdd899640a

+ 54
- 0
src/main/java/com/zipcodewilmington/phonebook/Contact.java View File

@@ -0,0 +1,54 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+import java.util.ArrayList;
4
+
5
+public class Contact {
6
+
7
+
8
+    private String name;
9
+    private ArrayList<String> phoneNumbers;
10
+
11
+
12
+    public Contact(){
13
+        this.phoneNumbers = new ArrayList<>();
14
+    }
15
+
16
+
17
+    public Contact(String name, String phoneNumbers){
18
+        this.name = name;
19
+        this.phoneNumbers = new ArrayList<>();
20
+        addNumber(phoneNumbers);
21
+    }
22
+
23
+    public void addNumber(String number) {
24
+        getPhoneNumbers().add(number);
25
+    }
26
+
27
+    public void removeNumber(String number) {
28
+        getPhoneNumbers().remove(number);
29
+    }
30
+
31
+    public String getName() {
32
+        return name;
33
+    }
34
+
35
+    public void setName(String name) {
36
+        this.name = name;
37
+    }
38
+
39
+    public ArrayList<String> getPhoneNumbers() {
40
+        return phoneNumbers;
41
+    }
42
+    @Override
43
+    public String toString(){
44
+        return getName() + " " + getListOfPhoneNumbers();
45
+    }
46
+    public String getListOfPhoneNumbers(){
47
+        String listOfNumbers = "";
48
+        for(String number : getPhoneNumbers()){
49
+            listOfNumbers += number + " ";
50
+        }
51
+        return listOfNumbers.trim();
52
+    }
53
+
54
+}

+ 35
- 34
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -1,66 +1,67 @@
1 1
 package com.zipcodewilmington.phonebook;
2
+
2 3
 import java.util.*;
3 4
 import java.util.TreeMap;
4
-import java.util.Set;
5
+
5 6
 
6 7
 /**
7 8
  * Created by leon on 1/23/18.
8 9
  */
9 10
 public class PhoneBook {
10 11
 
11
-    String name;
12
-    String number;
13
-    ArrayList<String> numbersList = new ArrayList<>();
12
+    private TreeMap<String, Contact> phoneBook;
14 13
 
15 14
 
16
-    private TreeMap contactsList;
17
-
18 15
     public PhoneBook() {
19
-        this.contactsList = new TreeMap<>();
16
+        this.phoneBook = new TreeMap<>(String::compareToIgnoreCase);
17
+    }
18
+    public void addNewEntry(String name, String number){
19
+        getPhoneBook().put(name, new Contact(name, number));
20
+    }
21
+
22
+    public void removeEntry(String name){
23
+        getPhoneBook().remove(name);
24
+    }
25
+    public void removeNumber(String name, String number){
26
+        getPhoneBook().get(name).getPhoneNumbers().remove(number);
27
+    }
20 28
 
29
+    public ArrayList<String> lookUpContactPhoneNumbers(String name){
30
+        return getPhoneBook().get(name).getPhoneNumbers();
21 31
     }
22 32
 
23
-    public Map<String, ArrayList> getContactsList() {
24
-        return contactsList;
33
+    public void listAllNamesAndNumbers() {
34
+        System.out.println(toString());
25 35
 
26 36
     }
37
+    public String reverseLookUp(String number) {
38
+        for (Map.Entry<String, Contact> contactEntry : getPhoneBook().entrySet()) {
39
+            if (contactEntry.getValue().getPhoneNumbers().contains(number)) {
40
+                return contactEntry.getKey();
41
+            }
27 42
 
28
-    public String addContact(String name, String number) {
29
-        if (!contactsList.containsKey(name)) {
30
-            contactsList.put(name, number);
31 43
         }
32
-        return "";
44
+        return "Contact not found";
33 45
     }
34 46
 
35
-    public String removeContact(String name) {
36
-        if (contactsList.containsKey(name)) {
37
-            contactsList.remove(name);
47
+    @Override
48
+    public String toString(){
49
+        String toBeReturned = "";
50
+        for(Map.Entry<String, Contact> contactEntry : getPhoneBook().entrySet()){
51
+           toBeReturned += contactEntry.getValue().toString();
52
+           toBeReturned += " ";
38 53
         }
39
-        return "";
54
+        return toBeReturned.trim();
40 55
     }
56
+    public TreeMap<String, Contact> getPhoneBook(){
57
+        return phoneBook;
58
+}
41 59
 
42
-
43
-    public String listAllNamesAndNumbers() {
44
-        Set listingAll = contactsList.entrySet();
45
-        return listingAll.toString();
46
-        /*String getEntries = new String();
47
-        for (Object allEntries : listingAll) {
48
-            if(listingAll.equals());*/
49 60
         }
50 61
 
51 62
 
52 63
 
53
-    public String reverseLookUp() {
54
-        Set<String> listingReverse = contactsList.keySet();
55
-        for (String allEntries : listingReverse) {
56
-            if(listingReverse.contains(allEntries.equals(number))) {
57
-                return allEntries;
58
-            }
59 64
 
60
-        }
61
-        return "";
62
-    }
63
-}
64 65
 
65 66
 
66 67
 

+ 66
- 0
src/test/java/com/zipcodewilmington/phonebook/ContactTest.java View File

@@ -0,0 +1,66 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import static junit.framework.TestCase.assertTrue;
8
+import static org.junit.Assert.assertEquals;
9
+
10
+public class ContactTest {
11
+
12
+    Contact contact;
13
+
14
+    @Before
15
+    public void setUp(){
16
+        contact = new Contact();
17
+
18
+    }
19
+    @Test
20
+    public void addNumberTest(){
21
+        //Given
22
+        String number = "872-345-2298";
23
+        contact.addNumber(number);
24
+        //When
25
+        boolean numberHasBeenAdded = contact.getPhoneNumbers().contains(number);
26
+        //Then
27
+        assertTrue(numberHasBeenAdded);
28
+
29
+    }
30
+    @Test
31
+    public void removeNumberTest(){
32
+        //Given
33
+        String number = "872-345-2298";
34
+        contact.addNumber(number);
35
+        //When
36
+        contact.removeNumber(number);
37
+        boolean numberHasBeenRemoved = !contact.getPhoneNumbers().contains(number);
38
+        //Then
39
+        assertTrue(numberHasBeenRemoved);
40
+    }
41
+    @Test
42
+    public void toStringTest(){
43
+        //Given
44
+        String newName = "April";
45
+        String newNumber = "7584339876";
46
+        Contact contactUnderTest = new Contact(newName, newNumber);
47
+
48
+        String expectedString = "April 7584339876";
49
+        String actualString = contactUnderTest.toString();
50
+        assertEquals(expectedString, actualString);
51
+
52
+    }
53
+    @Test
54
+    public void getListOfPhoneNumbersTest(){
55
+        String newName = "April";
56
+        String newNumber = "7584339876";
57
+        Contact contactUnderTest = new Contact(newName, newNumber);
58
+        contactUnderTest.addNumber("3457765489");
59
+        String expectedListOfNumbers = "7584339876 3457765489";
60
+        String actualListOfNumbers = contactUnderTest.getListOfPhoneNumbers();
61
+        assertEquals(expectedListOfNumbers, actualListOfNumbers);
62
+
63
+    }
64
+
65
+
66
+}

+ 56
- 26
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

@@ -5,54 +5,84 @@ import org.junit.Before;
5 5
 import org.junit.Test;
6 6
 
7 7
 import java.util.ArrayList;
8
-import java.util.Map;
9
-import java.util.Set;
8
+
9
+import static junit.framework.TestCase.assertTrue;
10
+import static org.junit.Assert.assertEquals;
10 11
 
11 12
 
12 13
 /**
13 14
  * Created by leon on 1/23/18.
14 15
  */
15
-public class PhoneBookTest {
16
-
16
+ public class PhoneBookTest {
17 17
 
18 18
     PhoneBook phoneBook;
19 19
 
20 20
     @Before
21
-    public void setUp(){
21
+    public void setUp() {
22 22
         phoneBook = new PhoneBook();
23
+    }
23 24
 
25
+    @Test
26
+    public void addNewEntryTest(){
27
+        String name = "April";
28
+        String number = "6547783321";
29
+        phoneBook.addNewEntry(name, number);
30
+        boolean entryHasBeenCreated = phoneBook.getPhoneBook().containsKey(name);
31
+        assertTrue(entryHasBeenCreated);
24 32
     }
25 33
     @Test
26
-    public void addContactTest(){
27
-        phoneBook.addContact("Genya", "6578356767");
28
-        boolean genyaIsInPhoneBook = phoneBook.getContactsList().keySet().contains("Genya");
29
-        Assert.assertTrue(genyaIsInPhoneBook);
30
-
34
+    public void removeEntryTest(){
35
+        String name = "April";
36
+        String number = "6547783321";
37
+        phoneBook.addNewEntry(name, number);
38
+        phoneBook.removeEntry(name);
39
+        boolean entryHasBeenRemoved = !phoneBook.getPhoneBook().containsKey(name);
40
+        assertTrue(entryHasBeenRemoved);
31 41
     }
32 42
     @Test
33
-    public void removeContactTest(){
34
-        phoneBook.removeContact("Pete Jones");
35
-        boolean peteJonesHasBeenRemoved = !phoneBook.getContactsList().containsKey("Pete Jones");
36
-        Assert.assertTrue(peteJonesHasBeenRemoved);
43
+    public void removeNumberTest(){
44
+        String name = "April";
45
+        String number = "6547783321";
46
+        phoneBook.addNewEntry(name, number);
47
+        phoneBook.removeNumber(name, number);
48
+        boolean numberHasBeenRemovedFromContact = !phoneBook.getPhoneBook().get(name).getPhoneNumbers().contains(number);
49
+        assertTrue(numberHasBeenRemovedFromContact);
50
+    }
51
+    @Test
52
+    public void lookUpContactPhoneNumbersTest(){
53
+        String name = "April";
54
+        String number = "6547783321";
55
+        phoneBook.addNewEntry(name, number);
56
+        ArrayList<String> actualPhoneList = phoneBook.lookUpContactPhoneNumbers(name);
57
+        ArrayList<String> expectedPhoneList = new ArrayList<>();
58
+        expectedPhoneList.add(number);
59
+        assertEquals(expectedPhoneList, actualPhoneList);
37 60
     }
38
-
39 61
     @Test
40 62
     public void listAllNamesAndNumbersTest(){
63
+        String name = "April";
64
+        String number = "6547783321";
65
+        String secondName = "Genya";
66
+        String secondNumber = "8753349987";
67
+        phoneBook.addNewEntry(name, number);
68
+        phoneBook.addNewEntry(secondName, secondNumber);
69
+        String actual = phoneBook.toString();
70
+        String expected = "April 6547783321 Genya 8753349987";
71
+        assertEquals(expected, actual);
72
+        //Print all the things
41 73
         phoneBook.listAllNamesAndNumbers();
42
-        String expected = new String();
43
-        Set<Map.Entry<String, ArrayList>> actual = phoneBook.getContactsList().entrySet();
44
-        Assert.assertEquals(expected, actual);
45 74
     }
46
-
47 75
     @Test
48 76
     public void reverseLookUpTest(){
49
-        phoneBook.reverseLookUp();
50
-        String expected = new String();
51
-        Set<Map.Entry<String, ArrayList>> actual = phoneBook.getContactsList().entrySet();
52
-        Assert.assertEquals(expected, actual);
53
-
77
+        String name = "April";
78
+        String number = "6547783321";
79
+        String secondName = "Genya";
80
+        String secondNumber = "8753349987";
81
+        phoneBook.addNewEntry(name, number);
82
+        phoneBook.addNewEntry(secondName, secondNumber);
83
+        String actual = phoneBook.reverseLookUp(number);
84
+        String expected = "April";
85
+        assertEquals(expected, actual);
54 86
     }
55 87
 
56 88
 }
57
-
58
-