donnaj 6 年之前
父節點
當前提交
19d6742a6a
共有 3 個檔案被更改,包括 140 行新增19 行删除
  1. 40
    1
      PhoneBook.java
  2. 75
    9
      PhoneBookTest.java
  3. 25
    9
      package.bluej

+ 40
- 1
PhoneBook.java 查看文件

@@ -1,7 +1,46 @@
1
- 
1
+import java.util.TreeMap;
2
+import java.util.Scanner; 
3
+import java.util.*;
2 4
 
3 5
 /**
4 6
  * Created by leon on 1/23/18.
5 7
  */
6 8
 public class PhoneBook {
9
+   // private String name;
10
+   // private String phoneNumber;
11
+    private Map<String, String> phoneBook;
12
+    
13
+public PhoneBook() {
14
+    // Create a TreeMap
15
+    phoneBook = new TreeMap<>();
16
+    
17
+    
18
+   
19
+}
20
+
21
+public  void addListings(String name, String phoneNumber) {
22
+    phoneBook.put(name, phoneNumber);
23
+  
24
+}
25
+
26
+public int size() {
27
+    return phoneBook.size();
28
+}
29
+
30
+public void removeListings(String name, String phoneNumber) {
31
+    phoneBook.remove(name, phoneNumber);
32
+}
33
+
34
+
35
+public void lookupByKey(String name, String phoneNumber) {
36
+     System.out.println(phoneBook.get(name));
37
+}
38
+
39
+public void lookupByValue(String name, String phoneNumber) {
40
+    System.out.println(phoneBook.get(phoneNumber));
41
+}
42
+
43
+public void display() {
44
+    System.out.println(phoneBook);
45
+}
7 46
 }

+ 75
- 9
PhoneBookTest.java 查看文件

@@ -1,5 +1,4 @@
1 1
 
2
-
3 2
 import static org.junit.Assert.*;
4 3
 import org.junit.After;
5 4
 import org.junit.Before;
@@ -13,6 +12,7 @@ import org.junit.Test;
13 12
  */
14 13
 public class PhoneBookTest
15 14
 {
15
+    private PhoneBook phoneBook;
16 16
     /**
17 17
      * Default constructor for test class PhoneBookTest
18 18
      */
@@ -28,15 +28,81 @@ public class PhoneBookTest
28 28
     @Before
29 29
     public void setUp()
30 30
     {
31
+       phoneBook = new PhoneBook();
32
+    }
33
+    
34
+    @Test
35
+    public void testAdd() {
36
+        //Given
37
+        String name = "J";
38
+        String phoneNumber = "2121112222";
39
+        
40
+        //Then
41
+        phoneBook.addListings(name, phoneNumber);
42
+        int expectedSize = 1;
43
+        int actualSize = phoneBook.size();
44
+        
45
+        //
46
+        assertEquals(expectedSize, actualSize);
47
+    }
48
+    
49
+    @Test
50
+    public void testRemove() {
51
+        //Given
52
+        String name = "J";
53
+        String phoneNumber = "2121112222";
54
+        
55
+        //Then
56
+        phoneBook.addListings(name, phoneNumber);
57
+        int expectedSize = 1;
58
+        int actualSize = phoneBook.size();
59
+        
60
+        //
61
+        assertEquals(expectedSize, actualSize);
31 62
     }
32 63
 
33
-    /**
34
-     * Tears down the test fixture.
35
-     *
36
-     * Called after every test case method.
37
-     */
38
-    @After
39
-    public void tearDown()
40
-    {
64
+    @Test
65
+    public void testLookup() {
66
+        //Given
67
+        String name = "J";
68
+        String phoneNumber = "2121112222";
69
+        
70
+        //Then
71
+        phoneBook.addListings(name, phoneNumber);
72
+        int expectedSize = 1;
73
+        int actualSize = phoneBook.size();
74
+        
75
+        //
76
+        assertEquals(expectedSize, actualSize);
77
+    }
78
+    
79
+    @Test
80
+    public void testReverseLookup() {
81
+        //Given
82
+        String name = "J";
83
+        String phoneNumber = "2121112222";
84
+        
85
+        //Then
86
+        phoneBook.addListings(name, phoneNumber);
87
+        int expectedSize = 1;
88
+        int actualSize = phoneBook.size();
89
+        
90
+        //
91
+        assertEquals(expectedSize, actualSize);
92
+    }
93
+    
94
+    @Test
95
+    public void testReverseLookupWhenNotFound() {
96
+        //Given
97
+        String name = "J";
98
+        String phoneNumber = "2121112222";
99
+        
100
+        //Then
101
+        phoneBook.addListings(name, phoneNumber);
102
+        int expectedSize = 1;
103
+        int actualSize = phoneBook.size();
104
+        
105
+        //
106
+        assertEquals(expectedSize, actualSize);
41 107
     }
42 108
 }

+ 25
- 9
package.bluej 查看文件

@@ -1,20 +1,23 @@
1 1
 #BlueJ package file
2
-editor.fx.0.height=0
3
-editor.fx.0.width=0
4
-editor.fx.0.x=0
5
-editor.fx.0.y=0
2
+dependency1.from=PhoneBookTest
3
+dependency1.to=PhoneBook
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=714
6
+editor.fx.0.width=800
7
+editor.fx.0.x=240
8
+editor.fx.0.y=23
6 9
 objectbench.height=101
7 10
 objectbench.width=461
8 11
 package.divider.horizontal=0.6
9 12
 package.divider.vertical=0.8007380073800738
10 13
 package.editor.height=427
11 14
 package.editor.width=674
12
-package.editor.x=427
13
-package.editor.y=143
15
+package.editor.x=386
16
+package.editor.y=135
14 17
 package.frame.height=600
15 18
 package.frame.width=800
16
-package.numDependencies=0
17
-package.numTargets=1
19
+package.numDependencies=1
20
+package.numTargets=2
18 21
 package.showExtends=true
19 22
 package.showUses=true
20 23
 project.charset=UTF-8
@@ -23,4 +26,17 @@ readme.name=@README
23 26
 readme.width=47
24 27
 readme.x=10
25 28
 readme.y=10
26
-
29
+target1.height=50
30
+target1.name=PhoneBookTest
31
+target1.showInterface=false
32
+target1.type=UnitTestTargetJunit4
33
+target1.width=120
34
+target1.x=100
35
+target1.y=10
36
+target2.height=50
37
+target2.name=PhoneBook
38
+target2.showInterface=false
39
+target2.type=ClassTarget
40
+target2.width=100
41
+target2.x=70
42
+target2.y=70