Selaa lähdekoodia

Merge da4a50859783ca132bdbd18fae3208617e019816 into 74a9673e36c7d25c28315cb54247a91792f8fbc0

ktecle 6 vuotta sitten
vanhempi
commit
9ecbbdca66
No account linked to committer's email

+ 7
- 1
pom.xml Näytä tiedosto

@@ -8,5 +8,11 @@
8 8
     <artifactId>phonebok</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
-
11
+    <dependencies>
12
+        <dependency>
13
+            <groupId>junit</groupId>
14
+            <artifactId>junit</artifactId>
15
+            <version>4.12</version>
16
+        </dependency>
17
+    </dependencies>
12 18
 </project>

+ 83
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Näytä tiedosto

@@ -1,7 +1,90 @@
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 {
9
+    Map<String, ArrayList<String>> phoneBook;
10
+
11
+    public PhoneBook() {
12
+        this.phoneBook = new TreeMap<String, ArrayList<String>>();
13
+    }
14
+
15
+    protected boolean add(String name, String phone) {
16
+        if (hasEntry(name)) {
17
+            getArrayListFor(name).add(phone);
18
+            return true;
19
+
20
+        } else {
21
+            ArrayList<String> contactNumbers = new ArrayList<String>();
22
+            contactNumbers.add(phone);
23
+            phoneBook.put(name, contactNumbers);
24
+        }
25
+
26
+        return true;
27
+    }
28
+
29
+    protected void remove(String name) {
30
+         phoneBook.remove(name);
31
+    }
32
+
33
+    protected String lookup(String name) {
34
+        String contactInfo = "";
35
+        ArrayList<String>phoneNumberLists = getArrayListFor(name);
36
+        for (String phoneNumbers:phoneNumberLists) {
37
+            contactInfo+=phoneNumbers + " ";
38
+        }
39
+        return contactInfo;
40
+    }
41
+
42
+    protected String listNames() {
43
+        String names = "";
44
+        for (String a : phoneBook.keySet()) {
45
+            names += a + "\n";
46
+        }
47
+
48
+        return names;
49
+    }
50
+
51
+    protected String listNamesAndNumbers() {
52
+        StringBuilder namesAndNumbers = new StringBuilder();
53
+        for (String keys : phoneBook.keySet()) {
54
+            namesAndNumbers.append(keys+"==>");
55
+            ArrayList<String> contact = getArrayListFor(keys);
56
+            for (String num : contact) {
57
+                namesAndNumbers.append(num+",");
58
+            }
59
+            namesAndNumbers.append("\n");
60
+        }
61
+        return namesAndNumbers.toString();
62
+    }
63
+
64
+    protected String reverseLookUp(String number) {
65
+        for (String keys : phoneBook.keySet()) {
66
+            for (int i = 0; i < getArrayListFor(keys).size(); i++) {
67
+                if (getArrayListFor(keys).get(i).equals(number))
68
+                    return keys;
69
+            }
70
+        }
71
+        return null;
72
+    }
73
+    protected boolean hasEntry(String name){
74
+        if(phoneBook.containsKey(name)){
75
+            return true;
76
+        }
77
+        return false;
78
+    }
79
+    protected ArrayList<String> getArrayListFor(String name){
80
+        return phoneBook.get(name);
81
+    }
82
+    protected void removeIndividualNumbers(String name,String number){
83
+        getArrayListFor(name).remove(number);
84
+    }
85
+
86
+    protected void removeRecord() {
87
+
88
+        phoneBook.clear();
89
+    }
7 90
 }

+ 114
- 0
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Näytä tiedosto

@@ -1,7 +1,121 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.After;
4
+import org.junit.Assert;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+
3 9
 /**
4 10
  * Created by leon on 1/23/18.
5 11
  */
12
+
13
+
6 14
 public class PhoneBookTest {
15
+
16
+    @Before
17
+    public void setUp() {
18
+
19
+    }
20
+
21
+    @After
22
+    public void tearDown() {
23
+    }
24
+
25
+    @Test
26
+    public void testLookup() {
27
+        PhoneBook phoneBookTest = new PhoneBook();
28
+        phoneBookTest.add("James", "555555555");
29
+        phoneBookTest.add("Charles", "2026783422");
30
+
31
+        String actual = phoneBookTest.lookup("Charles");
32
+        Assert.assertEquals("2026783422 ", actual);
33
+    }
34
+
35
+    @Test
36
+    public void testListNames() {
37
+        PhoneBook phoneBookTest = new PhoneBook();
38
+        phoneBookTest.add("John", "1234567890");
39
+        phoneBookTest.add("James", "555555555");
40
+        String expected = "James\n" +
41
+                "John\n";
42
+        String actual = phoneBookTest.listNames();
43
+        Assert.assertEquals(expected, actual);
44
+
45
+    }
46
+
47
+    @Test
48
+    public void testListNumbersAndNumbers() {
49
+        PhoneBook phoneBookTest = new PhoneBook();
50
+        phoneBookTest.add("John", "1234567890");
51
+        phoneBookTest.add("James", "5555555555");
52
+        phoneBookTest.add("James", "6666666666");
53
+        phoneBookTest.add("Mike", "5678901234");
54
+        String expected = "James==>" + "5555555555,6666666666,\n" +
55
+                "John==>" + "1234567890,\n" +
56
+                "Mike==>" + "5678901234,\n";
57
+        String actual = phoneBookTest.listNamesAndNumbers();
58
+        Assert.assertEquals(expected, actual);
59
+    }
60
+
61
+    @Test
62
+    public void testAdd() {
63
+        PhoneBook phoneBookTest = new PhoneBook();
64
+        boolean expected = true;
65
+        boolean actual = phoneBookTest.add("Peter", "7777777777");
66
+        Assert.assertEquals(expected, actual);
67
+
68
+    }
69
+
70
+    @Test
71
+    public void testRemove() {
72
+        PhoneBook phoneBookTest = new PhoneBook();
73
+        phoneBookTest.add("John", "1234567890");
74
+        phoneBookTest.add("Peter", "7777777777");
75
+        String ListPreRemoving = "John==>1234567890,\n" +
76
+                "Peter==>7777777777,\n";
77
+        phoneBookTest.remove("John");
78
+        String postRemoving = "Peter==>7777777777,\n";
79
+        String expected = postRemoving;
80
+        String actual = phoneBookTest.listNamesAndNumbers();
81
+        Assert.assertEquals(expected, actual);
82
+    }
83
+
84
+    @Test
85
+    public void testReverseLookup() {
86
+        PhoneBook phoneBookTest = new PhoneBook();
87
+        phoneBookTest.add("James", "555555555");
88
+
89
+        String actual = phoneBookTest.reverseLookUp("555555555");
90
+        Assert.assertEquals("James", actual);
91
+    }
92
+
93
+    @Test
94
+    public void testRemoveIndividualNumbers() {
95
+        PhoneBook phonebookeTest = new PhoneBook();
96
+        phonebookeTest.add("James", "2222222222");
97
+        phonebookeTest.add("James", "3333333333");
98
+
99
+        String expected = "James==>3333333333,\n";
100
+        phonebookeTest.removeIndividualNumbers("James", "2222222222");
101
+
102
+        String actual = phonebookeTest.listNamesAndNumbers();
103
+        Assert.assertEquals(expected, actual);
104
+    }
105
+
106
+    @Test
107
+    public void testRemoveRecord() {
108
+        PhoneBook phoneBookTest = new PhoneBook();
109
+        phoneBookTest.add("Efrem", "1234567890");
110
+        phoneBookTest.add("Brhane", "1010101010");
111
+
112
+        phoneBookTest.removeRecord();
113
+
114
+        String expected = "";
115
+        String actual = phoneBookTest.listNamesAndNumbers();
116
+
117
+        Assert.assertEquals(expected, actual);
118
+    }
119
+
120
+
7 121
 }