Browse Source

Merge 6fab03933c69d09c630d3733f01af07fd0ce3efc into 74a9673e36c7d25c28315cb54247a91792f8fbc0

Jessica Campbell 6 years ago
parent
commit
227d60713e
No account linked to committer's email

+ 19
- 0
pom.xml View File

@@ -7,6 +7,25 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>phonebok</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.7</source>
17
+                    <target>1.7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
22
+    <dependencies>
23
+        <dependency>
24
+            <groupId>junit</groupId>
25
+            <artifactId>junit</artifactId>
26
+            <version>RELEASE</version>
27
+        </dependency>
28
+    </dependencies>
10 29
 
11 30
 
12 31
 </project>

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

@@ -1,7 +1,81 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.*;
4
+
3 5
 /**
4 6
  * Created by leon on 1/23/18.
5 7
  */
6 8
 public class PhoneBook {
7
-}
9
+    //instance vairable
10
+    private Map<String, List<String>> contacts;
11
+
12
+    //constructor has to be the same as class w a capital letter
13
+    public PhoneBook() {
14
+        contacts = new TreeMap<String, List<String>>();
15
+    }
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;
27
+
28
+    }
29
+
30
+    public boolean delete(String name) {
31
+        return contacts.remove(name) != null;
32
+    }
33
+
34
+    public String lookup(String name) {
35
+        return join(contacts.get(name));
36
+
37
+    }
38
+
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
+            }
45
+        }
46
+        return "";
47
+    }
48
+
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);
54
+        }
55
+        return list.toString();
56
+    }
57
+
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)));
63
+        }
64
+        return list.toString();
65
+
66
+    }
67
+
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
+}

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

@@ -1,7 +1,85 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import com.sun.org.apache.xpath.internal.operations.Bool;
4
+import org.junit.Assert;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+import java.util.TreeMap;
9
+
3 10
 /**
4 11
  * Created by leon on 1/23/18.
5 12
  */
13
+
6 14
 public class PhoneBookTest {
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");
27
+    }
28
+
29
+    @Test
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
+    }
36
+
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
+    }
44
+
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);
52
+    }
53
+
54
+    @Test
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
+    }
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);
76
+    }
7 77
 }
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+