Browse Source

finished phonebook

Tommy Rogers 6 years ago
parent
commit
16499ac810
2 changed files with 153 additions and 23 deletions
  1. 63
    1
      PhoneBook.java
  2. 90
    22
      PhoneBookTest.java

+ 63
- 1
PhoneBook.java View File

@@ -1,7 +1,69 @@
1
- 
1
+import java.util.TreeMap;
2
+import java.util.ArrayList;
3
+import java.util.Iterator;
4
+import java.util.Map;
5
+import java.util.List;
6
+import java.util.Arrays;
2 7
 
3 8
 /**
4 9
  * Created by leon on 1/23/18.
5 10
  */
6 11
 public class PhoneBook {
12
+    private String name;
13
+    private String number;
14
+    Map<String,List<String>> phoneBook =new TreeMap<String, List<String>>();
15
+    List<String> numberSet = new ArrayList<String>();
16
+
17
+    public PhoneBook(){
18
+
19
+    }
20
+    public void addToPhoneBook(String name, String number){
21
+        ArrayList <String> temp = new ArrayList<>();
22
+        temp.add(number);
23
+        phoneBook.put(name, temp);
24
+
25
+    }
26
+    
27
+    public void addToPhoneBookIndividualNumbers(String name, String number){
28
+        numberSet.add(number);
29
+        phoneBook.put(name, numberSet);
30
+
31
+    }
32
+
33
+    public void removeFromPhoneBook(String name){
34
+
35
+        phoneBook.remove(name);
36
+
37
+    }
38
+
39
+    public void removeFromPhoneBookIndividualNumbers(String name, String number){
40
+        phoneBook.get(name).remove(number);
41
+
42
+    }
43
+
44
+    public String lookupFromPhoneBook(String name){
45
+        numberSet = phoneBook.get(name);
46
+        return numberSet.toString();
47
+    }
48
+
49
+    public String reverseLookupFromPhoneBook(String number){
50
+        String answerKey = null;
51
+        for(String key: phoneBook.keySet()){
52
+            List<String> test = phoneBook.get(key);
53
+            for(String s: test){
54
+                if(number.equals(s)){
55
+                    return key;
56
+                }
57
+
58
+            }
59
+        }
60
+        return "nothing is here";
61
+    }
62
+
63
+    public String displayFromPhoneBook(){
64
+
65
+        return phoneBook.entrySet().toString();
66
+
67
+    }
68
+
7 69
 }

+ 90
- 22
PhoneBookTest.java View File

@@ -1,4 +1,4 @@
1
-
1
+import java.util.TreeMap;
2 2
 
3 3
 import static org.junit.Assert.*;
4 4
 import org.junit.After;
@@ -13,30 +13,98 @@ import org.junit.Test;
13 13
  */
14 14
 public class PhoneBookTest
15 15
 {
16
-    /**
17
-     * Default constructor for test class PhoneBookTest
18
-     */
19
-    public PhoneBookTest()
20
-    {
16
+    
17
+    PhoneBook phoneBook =new PhoneBook();
18
+    private String name;
19
+    private String number;
20
+    @Test
21
+    public void testAddToPhoneBook1(){
22
+
23
+    phoneBook.addToPhoneBook("Zebra", "111-222-333");;
24
+    
25
+    String expectedString = "[Zebra=[111-222-333]]";
26
+    String actualString = phoneBook.displayFromPhoneBook();
27
+    
28
+    assertEquals(expectedString, actualString);
21 29
     }
30
+    
31
+    @Test
32
+    public void testAddToPhoneBookIndividualNumbers(){
22 33
 
23
-    /**
24
-     * Sets up the test fixture.
25
-     *
26
-     * Called before every test case method.
27
-     */
28
-    @Before
29
-    public void setUp()
30
-    {
34
+    phoneBook.addToPhoneBookIndividualNumbers("Zebra", "111-222-333");
35
+    phoneBook.addToPhoneBookIndividualNumbers("Zebra", "222-444-4444");
36
+    
37
+    String expectedString = "[Zebra=[111-222-333, 222-444-4444]]";
38
+    String actualString = phoneBook.displayFromPhoneBook();
39
+    
40
+    assertEquals(expectedString, actualString);
41
+    
42
+    
31 43
     }
44
+    
45
+    @Test
46
+    public void testRemoveFromPhoneBook1(){
47
+            PhoneBook phoneBook3 =new PhoneBook();
48
+    phoneBook3.addToPhoneBook("Zebra", "111-222-3333");
49
+    phoneBook3.addToPhoneBook("Dog", "222-444-4444");
50
+    phoneBook3.addToPhoneBook("Cat", "555-666-7777");
51
+    phoneBook3.addToPhoneBook("Alligator", "888-999-1010");
52
+    phoneBook3.addToPhoneBook("Wolf", "111-121-1313");
53
+    phoneBook3.removeFromPhoneBook("Dog");
32 54
 
33
-    /**
34
-     * Tears down the test fixture.
35
-     *
36
-     * Called after every test case method.
37
-     */
38
-    @After
39
-    public void tearDown()
40
-    {
55
+    String expectedString = "[Alligator=[888-999-1010], Cat=[555-666-7777], Wolf=[111-121-1313], Zebra=[111-222-3333]]";
56
+    
57
+    String actualString = phoneBook3.displayFromPhoneBook();
58
+    
59
+    assertEquals(expectedString, actualString);
60
+    
61
+    
62
+    }
63
+    
64
+    @Test
65
+    public void  testRemoveFromPhoneBookIndividualNumbers(){
66
+    phoneBook.addToPhoneBook("Zebra", "111-222-3333");
67
+    phoneBook.addToPhoneBookIndividualNumbers("Zebra", "222-444-4444");
68
+    phoneBook.removeFromPhoneBookIndividualNumbers("Zebra", "111-222-3333");
69
+    
70
+    String expectedString = "[Zebra=[222-444-4444]]";
71
+    String actualString = phoneBook.displayFromPhoneBook();
72
+    assertEquals(expectedString, actualString);
73
+    }
74
+    
75
+    @Test
76
+    public void testLookupFromPhoneBook(){
77
+                    PhoneBook phoneBook4 =new PhoneBook();
78
+    phoneBook4.addToPhoneBook("Zebra", "111-222-3333");
79
+    phoneBook4.addToPhoneBook("Dog", "222-444-4444");
80
+    phoneBook4.addToPhoneBook("Cat", "555-666-7777");
81
+    phoneBook4.addToPhoneBook("Alligator", "888-999-1010");
82
+    phoneBook4.addToPhoneBook("Wolf", "111-121-1313");
83
+    
84
+    String expectedString = "[555-666-7777]";
85
+    
86
+    
87
+    String actualString = phoneBook4.lookupFromPhoneBook("Cat");
88
+    
89
+    assertEquals(expectedString, actualString);
90
+    
91
+    
92
+    }
93
+    
94
+    @Test
95
+    public void testReverseLookup(){
96
+    phoneBook.addToPhoneBook("Zebra", "111-222-3333");
97
+    phoneBook.addToPhoneBook("Dog", "222-444-4444");
98
+    phoneBook.addToPhoneBook("Cat", "555-666-7777");
99
+    phoneBook.addToPhoneBook("Alligator", "888-999-1010");
100
+    phoneBook.addToPhoneBook("Wolf", "111-121-1313");
101
+    String expectedString = "Wolf";
102
+    
103
+    
104
+    String actualString = phoneBook.reverseLookupFromPhoneBook("111-121-1313");
105
+    
106
+    assertEquals(expectedString, actualString);
107
+    
108
+    
41 109
     }
42 110
 }