Jamez-s 6 years ago
parent
commit
021e93962c
3 changed files with 128 additions and 11 deletions
  1. 46
    1
      PhoneBook.java
  2. 57
    1
      PhoneBookTest.java
  3. 25
    9
      package.bluej

+ 46
- 1
PhoneBook.java View File

@@ -1,7 +1,52 @@
1
- 
1
+import java.util.*;
2 2
 
3 3
 /**
4 4
  * Created by leon on 1/23/18.
5 5
  */
6 6
 public class PhoneBook {
7
+  String name;
8
+  String phoneNumber;
9
+  TreeMap<String, String> phoneBook = new TreeMap<>();
10
+
11
+  public PhoneBook(TreeMap<String, String> phoneBook) {
12
+    this.phoneBook = phoneBook;
13
+  }
14
+
15
+  public PhoneBook(){}
16
+
17
+  public void add(String name, String phoneNumber){
18
+    phoneBook.put(name, phoneNumber);
19
+  }
20
+
21
+  public void remove(String name){
22
+    phoneBook.remove(name);
23
+  }
24
+
25
+  public String lookup(String name){
26
+    return phoneBook.get(name);
27
+  }
28
+
29
+  public String reverseLookup(String phoneNumber) {
30
+    for (Map.Entry<String, String> e : phoneBook.entrySet()) {
31
+      String name = e.getKey();
32
+      String thisNumber = e.getValue();
33
+
34
+      if (thisNumber == phoneNumber) {
35
+        return name;
36
+      }
37
+    }
38
+    return "Name not found";
39
+  }
40
+
41
+  public String display() {
42
+    StringBuilder sb = new StringBuilder();
43
+
44
+    for (Map.Entry<String, String> e : phoneBook.entrySet()){
45
+      String name = e.getKey();
46
+      String thisNumber = e.getValue();
47
+
48
+      sb.append(name).append(" ").append(thisNumber).append("\n");
49
+    }
50
+    return sb.toString();
51
+  }
7 52
 }

+ 57
- 1
PhoneBookTest.java View File

@@ -1,10 +1,12 @@
1 1
 
2 2
 
3
-import static org.junit.Assert.*;
3
+import org.junit.Assert;
4 4
 import org.junit.After;
5 5
 import org.junit.Before;
6 6
 import org.junit.Test;
7 7
 
8
+import java.util.*;
9
+
8 10
 /**
9 11
  * The test class PhoneBookTest.
10 12
  *
@@ -13,6 +15,8 @@ import org.junit.Test;
13 15
  */
14 16
 public class PhoneBookTest
15 17
 {
18
+
19
+    TreeMap<String, String> phoneBook = new TreeMap<>();
16 20
     /**
17 21
      * Default constructor for test class PhoneBookTest
18 22
      */
@@ -28,6 +32,7 @@ public class PhoneBookTest
28 32
     @Before
29 33
     public void setUp()
30 34
     {
35
+      PhoneBook pb = new PhoneBook(phoneBook);
31 36
     }
32 37
 
33 38
     /**
@@ -38,5 +43,56 @@ public class PhoneBookTest
38 43
     @After
39 44
     public void tearDown()
40 45
     {
46
+
47
+    }
48
+
49
+    // add entries
50
+    @Test
51
+    public void testOne() { 
52
+      String expected = "Dog 222-444-5555\nZebra 111-222-3333\n";
53
+
54
+      PhoneBook pb = new PhoneBook(phoneBook);
55
+      pb.add("Zebra", "111-222-3333");
56
+      pb.add("Dog", "222-444-5555");
57
+
58
+      String actual = pb.display();
59
+
60
+      Assert.assertEquals(expected, actual);
61
+    }
62
+    @Test
63
+    public void testTwo() {
64
+      String expected = "Dog 222-444-5555\n";
65
+      PhoneBook pb = new PhoneBook(phoneBook);
66
+        
67
+      pb.add("Zebra", "111-222-3333");
68
+      pb.add("Dog", "222-444-5555");
69
+      pb.remove("Zebra");
70
+        
71
+      String actual = pb.display();
72
+
73
+      Assert.assertEquals(expected, actual);  
74
+    }
75
+    @Test
76
+    public void TestThree() {
77
+      String expected = "222-444-5555";
78
+      PhoneBook pb = new PhoneBook(phoneBook);
79
+        
80
+      pb.add("Zebra", "111-222-3333");
81
+      pb.add("Dog", "222-444-5555");
82
+      
83
+      String actual = pb.lookup("Dog");
84
+        
85
+      Assert.assertEquals(expected, actual);
86
+    }
87
+    @Test
88
+    public void TestFour() {
89
+      String expected = "Dog";
90
+      PhoneBook pb = new PhoneBook(phoneBook);
91
+        
92
+      pb.add("Zebra", "111-222-3333");
93
+      pb.add("Dog", "222-444-5555");
94
+      
95
+      String actual = pb.reverseLookup("222-444-5555");
96
+      Assert.assertEquals(expected, actual);
41 97
     }
42 98
 }

+ 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=722
6
+editor.fx.0.width=800
7
+editor.fx.0.x=590
8
+editor.fx.0.y=136
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=35
16
+package.editor.y=158
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