Browse Source

committing Phone-book

chitraBegerhotta 6 years ago
parent
commit
9eed2db49e
3 changed files with 91 additions and 9 deletions
  1. 34
    1
      PhoneBook.java
  2. 34
    1
      PhoneBookTest.java
  3. 23
    7
      package.bluej

+ 34
- 1
PhoneBook.java View File

@@ -1,7 +1,40 @@
1 1
  
2
-
2
+import java.util.*;
3 3
 /**
4 4
  * Created by leon on 1/23/18.
5 5
  */
6 6
 public class PhoneBook {
7
+    //private String name;
8
+    //private String phoneNum;
9
+    TreeMap<String, String> phoneListByName = new TreeMap();
10
+    TreeMap<String, String> phoneListByNum = new TreeMap();
11
+    
12
+    public void add(String name, String phoneNumber){
13
+        phoneListByName.put(name, phoneNumber);
14
+        phoneListByNum.put(phoneNumber, name);
15
+        
16
+    }
17
+    public void remove(String name){
18
+        String phoneNumber = lookUp(name);
19
+        phoneListByName.remove(name);
20
+        phoneListByNum.remove(phoneNumber);
21
+    }
22
+    public String lookUp(String name){
23
+       return phoneListByName.get(name); 
24
+       
25
+    }
26
+    public String reverseLookUp(String phoneNumber){
27
+       return phoneListByNum.get(phoneNumber); 
28
+       
29
+    }
30
+    public void display(){
31
+        Set set = phoneListByName.entrySet();
32
+        //Set set2 = phoneListByNum.entrySet();
33
+        Iterator i = set.iterator();
34
+        
35
+        while(i.hasNext()){
36
+            Map.Entry name = (Map.Entry)i.next();
37
+            System.out.println(name.getKey() + ": " + name.getValue()) ;
38
+        }
39
+    }
7 40
 }

+ 34
- 1
PhoneBookTest.java View File

@@ -13,6 +13,8 @@ import org.junit.Test;
13 13
  */
14 14
 public class PhoneBookTest
15 15
 {
16
+    
17
+    PhoneBook testObj;
16 18
     /**
17 19
      * Default constructor for test class PhoneBookTest
18 20
      */
@@ -28,6 +30,7 @@ public class PhoneBookTest
28 30
     @Before
29 31
     public void setUp()
30 32
     {
33
+        testObj = new PhoneBook();
31 34
     }
32 35
 
33 36
     /**
@@ -39,4 +42,34 @@ public class PhoneBookTest
39 42
     public void tearDown()
40 43
     {
41 44
     }
42
-}
45
+    @Test
46
+    public void testadd(){
47
+     String expected = "4545666667"; 
48
+     //PhoneBook phoneBook = new PhoneBook();
49
+     testObj.add("john",expected);
50
+     String actual = testObj.lookUp("john");
51
+     assertEquals(actual, expected);
52
+    }
53
+    @Test
54
+    public void testremoveAndLookUp(){
55
+    String expected = "5454";
56
+    testObj.add("gei", "5454");
57
+    assertEquals(expected,testObj.lookUp("gei"));
58
+    
59
+    String expectedAfterDelete = null;
60
+    testObj.remove("gei");
61
+    assertEquals(expectedAfterDelete, testObj.lookUp("gei"));
62
+      
63
+    }
64
+    @Test
65
+    public void testreverseLookUp(){
66
+    String expected = "John";
67
+    testObj.add("John", "4343");
68
+    assertEquals(expected, testObj.reverseLookUp("4343"));
69
+    
70
+    
71
+    
72
+    }
73
+     
74
+    }
75
+

+ 23
- 7
package.bluej View File

@@ -1,8 +1,11 @@
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=722
6
+editor.fx.0.width=800
7
+editor.fx.0.x=240
8
+editor.fx.0.y=25
6 9
 objectbench.height=101
7 10
 objectbench.width=461
8 11
 package.divider.horizontal=0.6
@@ -13,8 +16,8 @@ package.editor.x=427
13 16
 package.editor.y=143
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