Jessica Campbell 6 years ago
parent
commit
6fab03933c

+ 55
- 75
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -1,101 +1,81 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
-import java.util.Map;
4
-import java.util.Set;
5
-import java.util.TreeMap;
6
-import java.util.TreeSet;
3
+import java.util.*;
7 4
 
8 5
 /**
9 6
  * Created by leon on 1/23/18.
10 7
  */
11 8
 public class PhoneBook {
9
+    //instance vairable
10
+    private Map<String, List<String>> contacts;
12 11
 
13
-    private TreeMap<String String> contacts;
14
-
12
+    //constructor has to be the same as class w a capital letter
15 13
     public PhoneBook() {
16
-
17
-        contacts = new TreeMap<>();
18
-    }
19
-    public void add (String name, String number){
20
-        contacts.put(name, number);
21
-    }
22
-    public String lookup (String name){
23
-        return contacts.get(name);
24
-    }
25
-    public void remove (String name){
26
-        contacts.remove(name);
27
-    }
28
-    public String display (){
29
-        StringBuilder sb = new StringBuilder();
30
-        for (Map.Entry<String, String>entry : contacts.entrySet()){
31
-            String name = entry.getKey();
32
-            String number = entry.getValue();
33
-            sb.append(name + "     " + number + "\n");
34
-        }
35
-        return sb.toString();
36
-    }
37
-    public String reverseLookup (String number){
38
-        for (Map.Entry <String, String>entry: contacts.entrySet()){
39
-            if (entry.getValue().equals(number){
40
-            }
41
-            return null;
42
-        }
14
+        contacts = new TreeMap<String, List<String>>();
43 15
     }
44 16
 
17
+    public Boolean add(String name, String number) {
18
+        // contains key is to add a phone number to an already existing person
19
+        if (contacts.containsKey(name)) {
20
+            contacts.get(name).add(number);
21
+            return true;
22
+        }
23
+        List<String> newNum = new ArrayList<String>();
24
+        newNum.add(number);
25
+        contacts.put(name, newNum);
26
+        return true;
45 27
 
28
+    }
46 29
 
30
+    public boolean delete(String name) {
31
+        return contacts.remove(name) != null;
32
+    }
47 33
 
34
+    public String lookup(String name) {
35
+        return join(contacts.get(name));
48 36
 
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-    public void addAContact(String name, String number) {
67
-        tmap.put(name, number);
68 37
     }
69 38
 
70
-    public String listOfAllNames(){
71
-        StringBuilder result = new StringBuilder();
72
-        for (Map.Entry<String, String> entry: tmap.entrySet()){
73
-            result.append(String.format("%-20s %8s\n", entry.getKey(), entry.getValue()));
74
-            //formats the way a string will print out on console^^^^^^
39
+    public String reverseLookup(String number) {
40
+        for (String nameKey : contacts.keySet()) {
41
+            List<String> numForName = contacts.get(nameKey);
42
+            if (numForName.contains(number)) {
43
+                return nameKey;
44
+            }
75 45
         }
76
-        return String.valueOf(result);
46
+        return "";
77 47
     }
78 48
 
79
-    public void removeAContact(String name) {
80
-        tmap.remove(name);
81
-    }
82
-
83
-    public String lookupContactName(String name) {
84
-        if (tmap.containsKey(name)) {
85
-            return tmap.get(name);
86
-        } else {
87
-            return "This Name Does Not Exist";
49
+    public String listAllNames() {
50
+        StringBuilder list = new StringBuilder();
51
+        Formatter formatList = new Formatter(list);
52
+        for (String nameKey : contacts.keySet()) {
53
+            formatList.format("Name %s%n", nameKey);
88 54
         }
55
+        return list.toString();
89 56
     }
90 57
 
91
-
92
-
93
-    public reverseLookUpNumber (String number){
94
-
58
+    public String listAllNamesAndNumbers() {
59
+        StringBuilder list = new StringBuilder();
60
+        Formatter formatList = new Formatter(list);
61
+        for (String nameKey : contacts.keySet()) {
62
+            formatList.format("Name %s%n Number: %-10s%n", nameKey, join(contacts.get(nameKey)));
95 63
         }
64
+        return list.toString();
96 65
 
66
+    }
97 67
 
98
-}
99
-
100
-
101
-
68
+    public static String join(List<String> listNum){
69
+        if (listNum.size() == 1){
70
+            return listNum.get(0);
71
+        }
72
+        StringBuilder sb = new StringBuilder();
73
+        for (int i = 0; i < listNum.size(); i++){
74
+            sb.append(listNum.get(i));
75
+            if (i< listNum.size()-1){
76
+                sb.append(", ");
77
+            }
78
+        }
79
+        return sb.toString();
80
+    }
81
+}

+ 59
- 51
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

@@ -1,5 +1,6 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import com.sun.org.apache.xpath.internal.operations.Bool;
3 4
 import org.junit.Assert;
4 5
 import org.junit.Before;
5 6
 import org.junit.Test;
@@ -9,69 +10,76 @@ import java.util.TreeMap;
9 10
 /**
10 11
  * Created by leon on 1/23/18.
11 12
  */
13
+
12 14
 public class PhoneBookTest {
13
-    // constructor
14
-    String name = "Jess";
15
-    String number = "1234567";
16
-    String nameA = "Alice";
17
-    String numberA = "1111111";
18
-    String nameB = "Sean";
19
-    String numberB = "2222222";
20
-
21
-    private PhoneBook phoneBook = new PhoneBook();
22
-    // instance
23
-
24
-
25
-   @Before
26
-   public void setUp(){
27
-       phoneBook = new PhoneBook();
28
-   }
29
-
30
-   @Test
31
-   public boolean testAddAContact() {
32
-    //instance of new phone book
33
-//    phoneBook.addAContact(name, number);
34
-//    Assert.assertTrue(phoneBook.addAContact(name, number).equals(name, number));
35
-       TreeMap.containsKey(String);
36
-       Assert.assertTrue(phoneBook.addAContact(name));
37
-
38
-   }
39
-@Test
40
-    public void removeAContact(String name){
41
-       //Given
42
-        String expected = "Null";
43
-
44
-        //When
45
-        phoneBook.addAContact(expected);
46
-        tmap.add(name);
47
-        //first must add a name to map
48
-
49
-        phoneBook.lookupContactName(expected);
50
-        String actual = phoneBook.removeAContact();
51
-        tmap.remove(name);
52
-        //Then
53
-        Assert.assertTrue(expected, actual);
15
+    private static PhoneBook phoneBookTest;
16
+
17
+    public void setUp() {
18
+        phoneBookTest = new PhoneBook();
19
+    }
20
+
21
+    @Test
22
+    public void testAdd() {
23
+        PhoneBook phoneBook = new PhoneBook();
24
+        phoneBook.add("Jess", "3333333");
25
+        String expected = phoneBook.lookup("Jess");
26
+        Assert.assertEquals(expected, "3333333");
54 27
     }
28
+
55 29
     @Test
56
-    public void testLookupContactName (){
57
-       //Given
58
-       phoneBook.lookupContactName("Jess");
59
-       String expected = "1234567";
30
+    public void testDelete() {
31
+        PhoneBook phoneBook = new PhoneBook();
32
+        phoneBook.add("Kat", "2342233");
33
+        Boolean actual = phoneBook.delete("Kat");
34
+        Assert.assertTrue(actual);
35
+    }
60 36
 
61
-       //When
62
-       String actual = phoneBook.lookupContactName("Jess");
37
+    @Test
38
+    public void testLookup() {
39
+        PhoneBook phoneBook = new PhoneBook();
40
+        phoneBook.add("Sean", "556665");
41
+        String actual = phoneBook.lookup("Sean");
42
+        Assert.assertEquals("556665", actual);
43
+    }
63 44
 
64
-       //Then
65
-       Assert.assertEquals(expected, actual);
45
+    // actual is what method in Phonebook class gives us back
46
+    @Test
47
+    public void testReverseLookup() {
48
+        PhoneBook phoneBook = new PhoneBook();
49
+        phoneBook.add("Sean", "556665");
50
+        String actual = phoneBook.reverseLookup("556665");
51
+        Assert.assertEquals("Sean", actual);
66 52
     }
67 53
 
68 54
     @Test
69
-    public void printOut(){
70
-        System.out.println(phoneBook.listOfAllNames());
55
+    public void testListAllNames() {
56
+        PhoneBook phoneBook = new PhoneBook();
57
+        phoneBook.add("Sean", "556665");
58
+        phoneBook.add("Joe", "9766847");
59
+        phoneBook.add("Bree", "993255");
60
+        String expected = "Name Bree\nName Joe\nName Sean\n";
61
+        String actual = phoneBook.listAllNames();
62
+        Assert.assertEquals(expected, actual);
63
+    }
71 64
 
65
+    @Test
66
+    public void testListAllNamesAndNumbers() {
67
+        PhoneBook phoneBook = new PhoneBook();
68
+        phoneBook.add("Sean", "5566651111");
69
+        phoneBook.add("Joe", "9766847111");
70
+        phoneBook.add("Bree", "9932551111");
71
+        String expected = "Name Bree\n Number: 9932551111\n" +
72
+                          "Name Joe\n Number: 9766847111\n" +
73
+                            "Name Sean\n Number: 5566651111\n";
74
+        String actual = phoneBook.listAllNamesAndNumbers();
75
+        Assert.assertEquals(expected, actual);
72 76
     }
73 77
 }
74 78
 
75 79
 
76 80
 
77 81
 
82
+
83
+
84
+
85
+