Browse Source

display done

Vincent Sima 6 years ago
parent
commit
6258393dd7
3 changed files with 125 additions and 46 deletions
  1. 54
    4
      PhoneBook.java
  2. 46
    33
      PhoneBookTest.java
  3. 25
    9
      package.bluej

+ 54
- 4
PhoneBook.java View File

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

+ 46
- 33
PhoneBookTest.java View File

@@ -1,42 +1,55 @@
1
-
2
-
1
+import java.util.*;
3 2
 import static org.junit.Assert.*;
4
-import org.junit.After;
5
-import org.junit.Before;
6 3
 import org.junit.Test;
7 4
 
8
-/**
9
- * The test class PhoneBookTest.
10
- *
11
- * @author  (your name)
12
- * @version (a version number or a date)
13
- */
14
-public class PhoneBookTest
5
+
6
+public class PhoneBookTest 
15 7
 {
16
-    /**
17
-     * Default constructor for test class PhoneBookTest
18
-     */
19
-    public PhoneBookTest()
20
-    {
8
+    @Test
9
+    public void addTest(){
10
+    PhoneBook phoneBook = new PhoneBook();
11
+    phoneBook.add("vince","333-333-3333");
12
+    int actual = phoneBook.size();
13
+    int expected = 1;
14
+    assertEquals(actual,expected);
21 15
     }
22 16
 
23
-    /**
24
-     * Sets up the test fixture.
25
-     *
26
-     * Called before every test case method.
27
-     */
28
-    @Before
29
-    public void setUp()
30
-    {
17
+    @Test
18
+    public void removeTest(){
19
+    PhoneBook phoneBook = new PhoneBook();
20
+    phoneBook.add("vince","333-333-3333");
21
+    phoneBook.remove("vince");
22
+    int actual = phoneBook.size();
23
+    int expected = 0;
24
+    assertEquals(actual,expected);
31 25
     }
32
-
33
-    /**
34
-     * Tears down the test fixture.
35
-     *
36
-     * Called after every test case method.
37
-     */
38
-    @After
39
-    public void tearDown()
40
-    {
26
+    
27
+    @Test
28
+    public void lookupTest(){
29
+    PhoneBook phoneBook = new PhoneBook();
30
+    phoneBook.add("vince","333-333-3333");
31
+    String actual =  phoneBook.lookup("vince");
32
+    String expected = "333-333-3333";
33
+    assertEquals(actual,expected);
34
+    }
35
+        @Test
36
+    public void lookupReverseTest(){
37
+    PhoneBook phoneBook = new PhoneBook();
38
+    phoneBook.add("vince","333-333-3333");
39
+    String actual =  phoneBook.reverseLookup("333-333-3333");
40
+    String expected = "vince";
41
+    assertEquals(actual,expected);
42
+    }
43
+    
44
+            @Test
45
+    public void displayTest(){
46
+    PhoneBook phoneBook = new PhoneBook();
47
+    phoneBook.add("vince","333-333-3333");
48
+    phoneBook.add("bill","444-444-4444");
49
+    phoneBook.add("john","555-555-5555");
50
+    String actual =  phoneBook.display();
51
+    String expected = "bill : 444-444-4444\njohn : 555-555-5555\nvince : 333-333-3333\n";
52
+    assertEquals(actual,expected);
41 53
     }
54
+    
42 55
 }

+ 25
- 9
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
2
+dependency1.from=PhoneBookTest
3
+dependency1.to=PhoneBook
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=705
6
+editor.fx.0.width=800
7
+editor.fx.0.x=123
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=33
16
+package.editor.y=99
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