Browse Source

completed

David Thornley 6 years ago
parent
commit
86bb03296e
3 changed files with 149 additions and 37 deletions
  1. 57
    1
      PhoneBook.java
  2. 63
    23
      PhoneBookTest.java
  3. 29
    13
      package.bluej

+ 57
- 1
PhoneBook.java View File

@@ -1,7 +1,63 @@
1
- 
1
+import java.util.HashSet;
2
+import java.util.Iterator;
3
+import java.util.Map;
4
+import java.util.Map.Entry;
5
+import java.util.Set;
6
+import java.util.TreeMap;
2 7
 
3 8
 /**
4 9
  * Created by leon on 1/23/18.
5 10
  */
6 11
 public class PhoneBook {
12
+    String name;
13
+    String number = ""; 
14
+    Map<String, String> yellowPages = new TreeMap<String, String>();
15
+
16
+    public PhoneBook(){
17
+    }
18
+    public void add(String name, String phoneNumber){
19
+        yellowPages.put(name, phoneNumber);
20
+    }
21
+
22
+    public void remove(String name) {
23
+        yellowPages.remove(name);
24
+    }
25
+
26
+    public String lookUp(String name) {
27
+        String number = yellowPages.get(name);
28
+        return number;
29
+    }
30
+
31
+    public int getSize(){
32
+
33
+        return yellowPages.size();
34
+    }
35
+
36
+    public String reverseLookUp(String value) {
37
+        String key = "";
38
+        
39
+        for(Map.Entry entry : yellowPages.entrySet())
40
+        if(value.equals(entry.getValue())){
41
+        key = entry.getKey().toString();
42
+        
43
+        }
44
+        
45
+        return key;
46
+    }
47
+    
48
+    public String display() {
49
+        Set set = yellowPages.entrySet();
50
+        Iterator it = set.iterator();
51
+        
52
+        StringBuilder answerBuild = new StringBuilder();
53
+        
54
+        while(it.hasNext()) {
55
+        Map.Entry x = (Map.Entry)it.next();
56
+        answerBuild.append(x.getKey() + " : " + x.getValue());
57
+        
58
+        }
59
+        
60
+        String answer = answerBuild.toString();
61
+        return answer;
62
+    }
7 63
 }

+ 63
- 23
PhoneBookTest.java View File

@@ -13,30 +13,70 @@ 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
+   @Test
17
+   public void phoneBooktest(){
18
+       PhoneBook yellowPages = new PhoneBook();
19
+       
20
+       
21 21
     }
22
-
23
-    /**
24
-     * Sets up the test fixture.
25
-     *
26
-     * Called before every test case method.
27
-     */
28
-    @Before
29
-    public void setUp()
30
-    {
22
+    
23
+   @Test
24
+   public void addTest(){
25
+    //Given 
26
+    PhoneBook yellowPages = new PhoneBook();
27
+    yellowPages.add("Jenny", "123-867-5309");
28
+    
29
+    //Then:
30
+    String actual = yellowPages.lookUp("Jenny");
31
+    String expected = "123-867-5309";
32
+    
33
+    //Assert:
34
+    
35
+    assertEquals(expected, actual);
36
+    
31 37
     }
32
-
33
-    /**
34
-     * Tears down the test fixture.
35
-     *
36
-     * Called after every test case method.
37
-     */
38
-    @After
39
-    public void tearDown()
40
-    {
38
+    
39
+    @Test
40
+    public void removeTest() {
41
+    //Given:
42
+    PhoneBook yellowPages = new PhoneBook();
43
+    yellowPages.add("Jenny", "123-867-5309");
44
+    yellowPages.remove("Jenny");
45
+    //Then:
46
+    int actual = yellowPages.getSize();
47
+    int expected = 0;
48
+    //Assert:
49
+    assertEquals(expected, actual);
50
+    
51
+    }
52
+    
53
+    @Test
54
+    public void reverseLookUpTest() {
55
+   //Given:
56
+   PhoneBook yellowPages = new PhoneBook();
57
+    yellowPages.add("Jenny", "123-867-5309");
58
+   //Then:
59
+   String actual = yellowPages.reverseLookUp("123-867-5309");
60
+   String expected = "Jenny";
61
+    //Assert:
62
+    assertEquals(expected, actual);
63
+    
64
+    }
65
+    
66
+    @Test
67
+    public void displayTest(){
68
+    //Given:
69
+    
70
+    PhoneBook yellowPages = new PhoneBook();
71
+    yellowPages.add("Jenny", "123-867-5309");
72
+    
73
+    
74
+    //Then:
75
+    
76
+    String actual = yellowPages.display();
77
+    String expected ="Jenny : 123-867-5309";
78
+    
79
+    //Assert:
80
+    assertEquals(expected, actual);
41 81
     }
42 82
 }

+ 29
- 13
package.bluej View File

@@ -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
6
-objectbench.height=101
7
-objectbench.width=461
2
+dependency1.from=PhoneBookTest
3
+dependency1.to=PhoneBook
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=41
9
+objectbench.height=164
10
+objectbench.width=776
8 11
 package.divider.horizontal=0.6
9
-package.divider.vertical=0.8007380073800738
10
-package.editor.height=427
12
+package.divider.vertical=0.6845018450184502
13
+package.editor.height=364
11 14
 package.editor.width=674
12
-package.editor.x=427
13
-package.editor.y=143
15
+package.editor.x=110
16
+package.editor.y=157
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=120
35
+target1.y=90
36
+target2.height=50
37
+target2.name=PhoneBook
38
+target2.showInterface=false
39
+target2.type=ClassTarget
40
+target2.width=100
41
+target2.x=100
42
+target2.y=130