Jordan Elderidge 6 years ago
parent
commit
2c4feb5d9f

+ 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>RELEASE</version>
15
+        </dependency>
16
+    </dependencies>
10 17
 
11 18
 
12 19
 </project>

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

@@ -1,7 +1,133 @@
1 1
 package com.zipcodewilmington.phonebook;
2
+import java.util.*;
3
+import java.util.Arrays;
2 4
 
3 5
 /**
4 6
  * Created by leon on 1/23/18.
5 7
  */
6 8
 public class PhoneBook {
7
-}
9
+
10
+    private Map<String, List<String>> phoneBookRecords;
11
+
12
+    public PhoneBook() {
13
+        phoneBookRecords = new TreeMap<String, List<String>>();
14
+    }
15
+
16
+    /**
17
+     * adds a name and corresponding phone number to phone book
18
+     *
19
+     * @param name
20
+     * @param number
21
+     */
22
+    public Boolean nameAndNumber(String name, String number) {
23
+        if (phoneBookRecords.containsKey(name)) {
24
+            phoneBookRecords.get(name).add(number);
25
+            return true;
26
+        }
27
+        List<String> newNumber = new ArrayList<String>();
28
+        newNumber.add(number);
29
+        phoneBookRecords.put(name, newNumber);
30
+        return true;
31
+    }
32
+
33
+    /**
34
+     * uses name to retrieve and delete both name and phone number
35
+     *
36
+     * @param name
37
+     */
38
+    public boolean deleteNameAndNumber(String name) {
39
+        return phoneBookRecords.remove(name) != null;
40
+    }
41
+
42
+
43
+    /**
44
+     * retrieves phone number by name
45
+     *
46
+     * @param name
47
+     */
48
+    public String returnNumber(String name) {
49
+        return join(phoneBookRecords.get(name));
50
+    }
51
+
52
+    /**
53
+     * retrieves name by phone number
54
+     *
55
+     * @param number
56
+     */
57
+    public String returnName(String number) {
58
+        for (String nameKey : phoneBookRecords.keySet()) {
59
+            List<String> name4Number = phoneBookRecords.get(nameKey);
60
+            if(name4Number.contains(number)){
61
+                return nameKey;
62
+            }
63
+        }
64
+        return "";
65
+    }
66
+
67
+    /**
68
+     * returns alphabetical list of all names in phoneBook
69
+     */
70
+    public String listAllNames() {
71
+        StringBuilder list = new StringBuilder();
72
+        Formatter listNames = new Formatter(list);
73
+        for (String nameKey : phoneBookRecords.keySet()) {
74
+            listNames.format("Name: %s%n", nameKey);
75
+        }
76
+        return list.toString();
77
+    }
78
+
79
+    /**
80
+     * returns alphabetical list of all records in phoneBook
81
+     */
82
+    public String listAllNamesAndNumbers() {
83
+        StringBuilder list = new StringBuilder();
84
+        Formatter listNameNNumbers = new Formatter(list);
85
+        for (String nameKey : phoneBookRecords.keySet()) {
86
+            listNameNNumbers.format("Name: %s PhoneNumber: %-10s%n", nameKey, join(phoneBookRecords.get(nameKey)));
87
+        }
88
+        return list.toString();
89
+    }
90
+
91
+    /**
92
+     * joins a list by a delimiter
93
+     * @param listNum
94
+     * @return String
95
+     */
96
+    public static String join(List<String> listNum){
97
+
98
+        if(listNum.size() == 1){
99
+            return listNum.get(0);
100
+        }
101
+        StringBuilder sb = new StringBuilder();
102
+        for ( int i = 0; i < listNum.size(); i++)
103
+        {
104
+            sb.append(listNum.get(i));
105
+            if (i < listNum.size()-1){
106
+                sb.append(", ");
107
+            }
108
+        }
109
+        return sb.toString();
110
+    }
111
+
112
+    public static void main(String[] args) {
113
+
114
+        PhoneBook lancasterPeople = new PhoneBook();
115
+        lancasterPeople.nameAndNumber("Javez", "3024785643");
116
+        lancasterPeople.nameAndNumber("Nick", "3029876543");
117
+        lancasterPeople.nameAndNumber("Jake", "3024556789");
118
+        lancasterPeople.nameAndNumber("Jayden", "3028990078");
119
+        lancasterPeople.nameAndNumber("Alex", "3025557896");
120
+        lancasterPeople.nameAndNumber("Logan", "3023324566");
121
+        lancasterPeople.nameAndNumber("Lance", "3021245432");
122
+
123
+
124
+        String x = lancasterPeople.listAllNamesAndNumbers();
125
+        System.out.println(x);
126
+
127
+        String y = lancasterPeople.listAllNames();
128
+        System.out.println(y);
129
+
130
+
131
+    }
132
+
133
+}

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

@@ -1,7 +1,114 @@
1 1
 package com.zipcodewilmington.phonebook;
2
+import org.junit.Assert;
3
+import org.junit.Test;
2 4
 
3 5
 /**
4 6
  * Created by leon on 1/23/18.
5 7
  */
6 8
 public class PhoneBookTest {
9
+
10
+    private static PhoneBook PhoneBookTest;
11
+
12
+    public void setUp() {
13
+        PhoneBookTest = new PhoneBook();
14
+    }
15
+
16
+    @Test
17
+    public void recordShouldAddToPhoneBook() {
18
+        PhoneBook phoneBookContact = new PhoneBook();
19
+        phoneBookContact.nameAndNumber ("Nick", "3029876543");
20
+        String expectedValue = phoneBookContact.returnNumber("Nick");
21
+        Assert.assertEquals(expectedValue, "3029876543");
22
+    }
23
+
24
+    @Test
25
+    public void recordShouldAddMultipleNumbersToPerson() {
26
+        PhoneBook phoneBookContact = new PhoneBook();
27
+        phoneBookContact.nameAndNumber ("Nick", "3029876543");
28
+        phoneBookContact.nameAndNumber ("Nick", "3029876541");
29
+        phoneBookContact.nameAndNumber ("Nick", "3029876546");
30
+        String actualValue = phoneBookContact.returnNumber("Nick");
31
+        Assert.assertEquals( "3029876543, 3029876541, 3029876546", actualValue);
32
+    }
33
+
34
+    @Test
35
+    public void ShouldRetrieveNumbersByName() {
36
+        PhoneBook phoneBookContact= new PhoneBook();
37
+        phoneBookContact.nameAndNumber ("Nick", "3029876543");
38
+        String expectedValue = phoneBookContact.returnNumber("Nick");
39
+        Assert.assertEquals(expectedValue, "3029876543");
40
+    }
41
+
42
+    @Test
43
+    public void ShouldRetrieveMultipleNumbersByName() {
44
+        PhoneBook phoneBookContact = new PhoneBook();
45
+        phoneBookContact.nameAndNumber("Jake", "3024556789");
46
+        phoneBookContact.nameAndNumber("Jake", "3024556788");
47
+        phoneBookContact.nameAndNumber("Jake", "3024556787");
48
+        String actualValue = phoneBookContact.returnNumber("Jake");
49
+        Assert.assertEquals( "3024556789, 3024556788, 3024556787", actualValue);
50
+    }
51
+
52
+
53
+    @Test
54
+    public void receiveNameUsingNumber() {
55
+        PhoneBook phoneBookContact = new PhoneBook();
56
+        phoneBookContact.nameAndNumber ("Nick", "3029876543");
57
+        String expectedValue = phoneBookContact.returnName("3029876543");
58
+        Assert.assertEquals(expectedValue, "Nick");
59
+    }
60
+
61
+
62
+    @Test
63
+    public void recordShouldDeleteFromPhoneBook() {
64
+        PhoneBook phoneBookContact = new PhoneBook();
65
+        phoneBookContact.nameAndNumber ("Nick", "3029876543");
66
+        phoneBookContact.deleteNameAndNumber("Nick");
67
+        String expectedValue = phoneBookContact.listAllNames();
68
+        Assert.assertEquals(expectedValue, "");
69
+    }
70
+
71
+    @Test
72
+    public void DeletePersonFromPhoneBookWithMoreThanOneNumber() {
73
+        PhoneBook phoneBookContact = new PhoneBook();
74
+        phoneBookContact.nameAndNumber ("Javez", "3024785643");
75
+        phoneBookContact.nameAndNumber ("Javez", "3024785642");
76
+        phoneBookContact.nameAndNumber ("Javez", "3024785656");
77
+        phoneBookContact.deleteNameAndNumber("Javez");
78
+        String expectedValue = phoneBookContact.listAllNames();
79
+        Assert.assertEquals(expectedValue, "");
80
+    }
81
+
82
+
83
+    @Test
84
+    public void listAllNamesAlphabeticalOrder() {
85
+        PhoneBook phoneBookContact= new PhoneBook();
86
+        phoneBookContact.nameAndNumber("Alex", "3025557896");
87
+        phoneBookContact.nameAndNumber("Jake", "3024556789");
88
+        phoneBookContact.nameAndNumber ("Javez", "3024785643");
89
+        phoneBookContact.nameAndNumber ("Jordan", "3028990078");
90
+        phoneBookContact.nameAndNumber("Lance", "3021245432");
91
+        String actual = phoneBookContact.listAllNames();
92
+        String testList = "Name: Alex\nName: Jake\nName: Javez\nName: Jordan\nName: Lance\n";
93
+        Assert.assertEquals(testList, actual);
94
+    }
95
+
96
+
97
+    @Test
98
+    public void listAllNamesAndNumbers() {
99
+
100
+        PhoneBook phoneBookContact = new PhoneBook();
101
+        phoneBookContact.nameAndNumber ("Javez", "3024785643");
102
+        phoneBookContact.nameAndNumber("Alex", "3025557896");
103
+        phoneBookContact.nameAndNumber("Jake", "3024556789");
104
+        phoneBookContact.nameAndNumber ("Nick", "3029876543");
105
+        phoneBookContact.nameAndNumber("Lance", "3021245432");
106
+        String actual = phoneBookContact.listAllNames();
107
+        String testList = "Name: Alex\nName: Jake\nName: Javez\nName: Lance\nName: Nick\n";
108
+        Assert.assertEquals(testList, actual);
109
+    }
110
+
111
+
7 112
 }
113
+
114
+