#36 WJB3003 William Brown's Phonebook

Aperto
WJB3003 vorrebbe unire 1 commit da WJB3003/CR-MicroLabs-Composition-PhoneBook:master a master

+ 55
- 2
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Vedi File

@@ -1,7 +1,60 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import apple.laf.JRSUIUtils;
4
+
5
+import java.util.ArrayList;
6
+import java.util.Set;
7
+import java.util.TreeMap;
8
+
3 9
 /**
4
- * Created by leon on 1/23/18.
10
+ * Created by William on 10/28/2018.
5 11
  */
6 12
 public class PhoneBook {
7
-}
13
+
14
+    //TREEMAP
15
+    public TreeMap<String, ArrayList<String>> phonebook  = new TreeMap<>();
16
+
17
+    //CHECKS IF IT HAS AN ENTRY
18
+    public boolean hasEntry(String name){
19
+        if(phonebook.containsKey(name)){
20
+            return true;
21
+        }
22
+        return false;
23
+    }
24
+
25
+    //LOOKUP NUMBER
26
+    public ArrayList<String> lookUp(String name) {
27
+        if(hasEntry(name)){
28
+            return phonebook.get(name);
29
+        }
30
+        return null;
31
+    }
32
+
33
+    //PRINTS OUT NAME AND NUMBERS IN TREEMAP
34
+    public TreeMap<String, ArrayList<String>> listNamesAndNumbers() {
35
+        return phonebook;
36
+    }
37
+
38
+    //LIST ONLY NAMES IN TREEMAP
39
+    public Set<String> listNames() {
40
+        return phonebook.keySet();
41
+    }
42
+
43
+    //ADDS CONTACT TO THE TREEMAP
44
+    public void addContact(String name, ArrayList<String> number) {
45
+        phonebook.put(name, number);
46
+    }
47
+
48
+    //REMOVES CONTACT FROM THE TREE
49
+    public void removeContact(String name, ArrayList<String> number){
50
+        name = null;
51
+        number = null;
52
+    }
53
+
54
+    //CHECKS THE SIZE
55
+    public int size(){
56
+        return phonebook.size();
57
+    }
58
+
59
+
60
+}

+ 118
- 2
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Vedi File

@@ -1,7 +1,123 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+import java.util.ArrayList;
7
+
3 8
 /**
4
- * Created by leon on 1/23/18.
9
+ * Created by William on 10/28/2018.
5 10
  */
6 11
 public class PhoneBookTest {
7
-}
12
+
13
+    @Test
14
+    public void testHasEntry(){
15
+        //Given
16
+        PhoneBook book = new PhoneBook();
17
+        String name = "AA";
18
+        ArrayList<String> phoneNumber = new ArrayList<>();
19
+
20
+        phoneNumber.add("211-111-2222");
21
+
22
+        book.addContact(name, phoneNumber);
23
+
24
+        //When
25
+        boolean actualOutput = book.hasEntry(name);
26
+
27
+        //Then
28
+        Assert.assertTrue(actualOutput);
29
+    }
30
+
31
+    @Test
32
+    public void testAdd(){
33
+        //Given
34
+        PhoneBook book = new PhoneBook();
35
+        String name = "AA";
36
+        ArrayList<String> phoneNumber = new ArrayList<>();
37
+        phoneNumber.add("211-111-2222");
38
+
39
+        //When
40
+        book.addContact(name, phoneNumber);
41
+
42
+        //Then
43
+        Assert.assertEquals(1, book.size());
44
+    }
45
+
46
+    @Test
47
+    public void testRemove(){
48
+        //Given
49
+        PhoneBook book = new PhoneBook();
50
+        String name = "AA";
51
+        ArrayList<String> phoneNumber = new ArrayList<>();
52
+        phoneNumber.add("211-111-2222");
53
+
54
+        //When
55
+        book.removeContact(name, phoneNumber);
56
+
57
+        //Then
58
+        Assert.assertEquals(0, book.size());
59
+    }
60
+
61
+    @Test
62
+    public void testLookUp(){
63
+        //Given
64
+        PhoneBook book = new PhoneBook();
65
+        String name = "AA";
66
+        ArrayList<String> phoneNumber = new ArrayList<>();
67
+        phoneNumber.add("221-111-2222");
68
+        ArrayList<String> expected = new ArrayList<String>();
69
+        expected.add("221-111-2222");
70
+        //When
71
+        book.addContact("AA", phoneNumber);
72
+        book.lookUp(name);
73
+
74
+        //Then
75
+        Assert.assertEquals(expected, book.lookUp("AA"));
76
+    }
77
+
78
+    @Test
79
+    public void testSize(){
80
+        //Given
81
+        PhoneBook book = new PhoneBook();
82
+        ArrayList<String> phoneNumber = new ArrayList<>();
83
+        String name = "AA";
84
+        phoneNumber.add("211-111-2222");
85
+
86
+        book.addContact(name, phoneNumber);
87
+
88
+        //When
89
+        int actualOutput = book.size();
90
+        int expectedOutput = 1;
91
+
92
+        //Then
93
+        Assert.assertEquals(expectedOutput, actualOutput);
94
+    }
95
+
96
+    @Test
97
+    public void testList(){
98
+
99
+        PhoneBook phoneBook = new PhoneBook();
100
+        ArrayList<String> phoneNumber = new ArrayList<>();
101
+        String name = "AA";
102
+        phoneNumber.add("221-111-2222");
103
+
104
+        //When
105
+        phoneBook.addContact("AA", phoneNumber);
106
+
107
+        phoneBook.listNamesAndNumbers();
108
+    }
109
+
110
+    @Test
111
+    public void testNameList(){
112
+
113
+        PhoneBook phoneBook = new PhoneBook();
114
+        ArrayList<String> phoneNumber = new ArrayList<>();
115
+        String name = "AA";
116
+        phoneNumber.add("221-111-2222");
117
+
118
+        //When
119
+        phoneBook.addContact("AA", phoneNumber);
120
+
121
+        phoneBook.listNames();
122
+    }
123
+}