Browse Source

idk why i haven't done this before

rjsmall90 6 years ago
parent
commit
755deda14a
3 changed files with 106 additions and 47 deletions
  1. 36
    13
      PhoneBook.java
  2. 66
    30
      PhoneBookTest.java
  3. 4
    4
      package.bluej

+ 36
- 13
PhoneBook.java View File

@@ -1,5 +1,6 @@
1 1
 import java.util.Scanner;
2 2
 import java.util.ArrayList;
3
+import java.util.*;
3 4
 
4 5
 /**
5 6
  * Created by leon on 1/23/18.
@@ -7,23 +8,45 @@ import java.util.ArrayList;
7 8
 public class PhoneBook {
8 9
     private String name;
9 10
     private String phoneNumber;
10
-    
11
+    TreeMap<String, String>phonebook = new TreeMap<>(); 
12
+
11 13
     public PhoneBook()
12
-    {
13
-        
14
+    {        
14 15
     }
15 16
     
16
-    public String add(String n, String number)
17
-    {
18
-        n = name;
19
-        number = phoneNumber;
20
-        
21
-        return null;
17
+    public PhoneBook(String name, String phoneNumber) {
18
+    this.name = name; this.phoneNumber = phoneNumber; 
19
+    }
20
+
21
+    public PhoneBook (TreeMap<String, String>phonebook) {
22
+        this.phonebook = phonebook;
23
+    }
24
+
25
+    public void add(String name, String phoneNumber) {
26
+        phonebook.put(name, phoneNumber); 
27
+    }
28
+
29
+    public void remove(String name) {
30
+        phonebook.remove(name); 
22 31
     }
23 32
     
24
-    public String lookUp(String n)
25
-    {
26
-        n = name;
27
-        return null;
33
+    public void removeRecord() {
34
+     phonebook.clear();   
28 35
     }
36
+
37
+    public Collection<String> lookup (String name) {
38
+        return phonebook.values();   
39
+    }
40
+
41
+    public Collection<String> reverseLookup(String phoneNumber) {
42
+        return phonebook.keySet();
43
+    }
44
+
45
+    public TreeMap<String, String> display() {
46
+        return phonebook; 
47
+    }
48
+
49
+    public void entries () {
50
+
51
+    } 
29 52
 }

+ 66
- 30
PhoneBookTest.java View File

@@ -2,6 +2,8 @@ import static org.junit.Assert.*;
2 2
 import org.junit.After;
3 3
 import org.junit.Before;
4 4
 import org.junit.Test;
5
+import java.util.*;
6
+import java.util.stream.Collectors;
5 7
 
6 8
 /**
7 9
  * The test class PhoneBookTest.
@@ -11,45 +13,79 @@ import org.junit.Test;
11 13
  */
12 14
 public class PhoneBookTest
13 15
 {
14
-    /**
15
-     * Default constructor for test class PhoneBookTest
16
-     */
17
-    public PhoneBookTest()
16
+    TreeMap<String, String> phonebook = new TreeMap<>(); 
17
+    PhoneBook pb = new PhoneBook(phonebook);
18
+
19
+    @Test
20
+    public void testAdd()
18 21
     {
22
+        //plug in a String name here using methods from main class
23
+        String name = "Ryan"; 
24
+        String phoneNumber= "123-456-7890"; 
25
+        pb.add(name, phoneNumber);
26
+        assertFalse(phonebook.isEmpty());
27
+
19 28
     }
20
-    private PhoneBook phoneBook;
21
-
22
-    /**
23
-     * Sets up the test fixture.
24
-     *
25
-     * Called before every test case method.
26
-     */
27
-    @Before
28
-    public void setUp()
29
-    {
30
-        phoneBook = new PhoneBook();
29
+
30
+    @Test
31
+    public void testRemove() 
32
+    {   
33
+        String name = "Ryan"; 
34
+        String phoneNumber= "123-456-7890"; 
35
+
36
+        pb.add(name, phoneNumber);
37
+        pb.remove(name);
38
+        assertTrue(phonebook.isEmpty());
39
+
40
+    }
41
+    
42
+       @Test
43
+    public void testRemoveRecord()
44
+    {   
45
+        String name = "Ryan"; 
46
+        String phoneNumber= "123-456-7890"; 
47
+
48
+        pb.add(name, phoneNumber);
49
+        pb.remove(name);
50
+        assertTrue(phonebook.isEmpty());
51
+
31 52
     }
32 53
 
33 54
     @Test
34
-    public void testAdd()
55
+    public void testLookup()
35 56
     {
36
-        //plug in a String name here using methods from main class
37 57
         String name = "Ryan"; 
38
-        String expectedPhoneNumber= "123-456-7890"; 
39
-        phoneBook.add(name, expectedPhoneNumber);
40
-        String actualPhoneNumber = phoneBook.lookUp(name);
41
-        
42
-        assertEquals(actualPhoneNumber, expectedPhoneNumber);
43
-        
58
+        String phoneNumber= "123-456-7890"; 
59
+        pb.add(name, phoneNumber);
60
+        Collection<String> expected = phonebook.values(); 
61
+
62
+        assertEquals(expected, pb.lookup(name));
44 63
     }
45 64
 
46
-    /**
47
-     * Tears down the test fixture.
48
-     *
49
-     * Called after every test case method.
50
-     */
51
-    @After
52
-    public void tearDown()
65
+    @Test
66
+    public void testReverseLookup() {
67
+        String name = "Ryan"; 
68
+        String phoneNumber= "123-456-7890"; 
69
+        pb.add(name, phoneNumber);
70
+        Collection<String> expected = phonebook.keySet(); 
71
+
72
+        assertEquals(expected, pb.reverseLookup(phoneNumber));
73
+    }
74
+
75
+    @Test
76
+    public void testDisplay() 
77
+    {       
78
+        String name = "Ryan"; 
79
+        String phoneNumber= "123-456-7890"; 
80
+        pb.add(name, phoneNumber);
81
+        TreeMap<String, String> expected = phonebook; 
82
+        
83
+        assertEquals(expected, pb.display());
84
+    }
85
+    
86
+    @Test
87
+    public void testEntries()
53 88
     {
89
+        assertEquals(null, 1);  
54 90
     }
55 91
 }

+ 4
- 4
package.bluej View File

@@ -2,10 +2,10 @@
2 2
 dependency1.from=PhoneBookTest
3 3
 dependency1.to=PhoneBook
4 4
 dependency1.type=UsesDependency
5
-editor.fx.0.height=722
6
-editor.fx.0.width=800
7
-editor.fx.0.x=240
8
-editor.fx.0.y=26
5
+editor.fx.0.height=0
6
+editor.fx.0.width=0
7
+editor.fx.0.x=0
8
+editor.fx.0.y=0
9 9
 objectbench.height=101
10 10
 objectbench.width=461
11 11
 package.divider.horizontal=0.6