Browse Source

phone book

Ben Blinebury 6 years ago
parent
commit
54ebe47005
3 changed files with 137 additions and 10 deletions
  1. 55
    1
      PhoneBook.java
  2. 63
    3
      PhoneBookTest.java
  3. 19
    6
      package.bluej

+ 55
- 1
PhoneBook.java View File

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

+ 63
- 3
PhoneBookTest.java View File

16
     /**
16
     /**
17
      * Default constructor for test class PhoneBookTest
17
      * Default constructor for test class PhoneBookTest
18
      */
18
      */
19
-    public PhoneBookTest()
20
-    {
19
+    public PhoneBookTest(){
20
+        PhoneBook phoneBook = new PhoneBook();
21
     }
21
     }
22
 
22
 
23
     /**
23
     /**
29
     public void setUp()
29
     public void setUp()
30
     {
30
     {
31
     }
31
     }
32
-
32
+    @Test
33
+    public void assTest() {
34
+    
35
+        PhoneBook phoneBook = new PhoneBook();
36
+        
37
+        //given
38
+        phoneBook.add("Ben", "358-1032");
39
+        
40
+        //then
41
+        
42
+        String actual = phoneBook.lookup("Ben");
43
+        String expected = "358-1032";
44
+        
45
+        //assert
46
+        
47
+        assertEquals(expected, actual);
48
+    
49
+    }
50
+    @Test
51
+    public void removeTest() {
52
+        PhoneBook phoneBook = new PhoneBook();
53
+        
54
+        //given
55
+        phoneBook.add("Ben", "358-1032");
56
+        phoneBook.remove("Ben");
57
+        
58
+        //then
59
+        int actual = phoneBook.getSize();
60
+        int expected = 0;
61
+        
62
+        //assert
63
+        assertEquals(expected, actual);
64
+    }
65
+    @Test
66
+    public void reverseLookupTest() {
67
+        PhoneBook phoneBook = new PhoneBook();
68
+        
69
+        //given
70
+        phoneBook.add("Ben", "358-1032");
71
+        
72
+        //then
73
+        String actual = phoneBook.reverseLookup("358-1032");
74
+        String expected = "Ben";
75
+        
76
+        //assert
77
+        assertEquals(expected, actual);
78
+    }
79
+    @Test
80
+    public void displayTest() {
81
+        PhoneBook phoneBook = new PhoneBook();
82
+        
83
+        //given
84
+        phoneBook.add("Ben", "358-1032");
85
+        
86
+        //then
87
+        String actual = phoneBook.display();
88
+        String expected = "Ben : 358-1032";
89
+        
90
+        //assert
91
+        assertEquals(expected, actual);
92
+    }
33
     /**
93
     /**
34
      * Tears down the test fixture.
94
      * Tears down the test fixture.
35
      *
95
      *

+ 19
- 6
package.bluej View File

1
 #BlueJ package file
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
+editor.fx.0.height=722
3
+editor.fx.0.width=800
4
+editor.fx.0.x=320
5
+editor.fx.0.y=53
6
 objectbench.height=101
6
 objectbench.height=101
7
 objectbench.width=461
7
 objectbench.width=461
8
 package.divider.horizontal=0.6
8
 package.divider.horizontal=0.6
14
 package.frame.height=600
14
 package.frame.height=600
15
 package.frame.width=800
15
 package.frame.width=800
16
 package.numDependencies=0
16
 package.numDependencies=0
17
-package.numTargets=1
17
+package.numTargets=2
18
 package.showExtends=true
18
 package.showExtends=true
19
 package.showUses=true
19
 package.showUses=true
20
 project.charset=UTF-8
20
 project.charset=UTF-8
23
 readme.width=47
23
 readme.width=47
24
 readme.x=10
24
 readme.x=10
25
 readme.y=10
25
 readme.y=10
26
-
26
+target1.height=50
27
+target1.name=PhoneBookTest
28
+target1.showInterface=false
29
+target1.type=UnitTestTargetJunit4
30
+target1.width=120
31
+target1.x=100
32
+target1.y=10
33
+target2.height=50
34
+target2.name=PhoneBook
35
+target2.showInterface=false
36
+target2.type=ClassTarget
37
+target2.width=100
38
+target2.x=70
39
+target2.y=70