Browse Source

finished lab

William Simkins 6 years ago
parent
commit
d27f9b990c
2 changed files with 60 additions and 29 deletions
  1. 37
    1
      PhoneBook.java
  2. 23
    28
      PhoneBookTest.java

+ 37
- 1
PhoneBook.java View File

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

+ 23
- 28
PhoneBookTest.java View File

@@ -1,42 +1,37 @@
1 1
 
2
-
3 2
 import static org.junit.Assert.*;
4 3
 import org.junit.After;
5 4
 import org.junit.Before;
6 5
 import org.junit.Test;
7 6
 
8
-/**
9
- * The test class PhoneBookTest.
10
- *
11
- * @author  (your name)
12
- * @version (a version number or a date)
13
- */
14 7
 public class PhoneBookTest
15 8
 {
16
-    /**
17
-     * Default constructor for test class PhoneBookTest
18
-     */
19
-    public PhoneBookTest()
20
-    {
9
+    @Test
10
+    public void testAdd(){
11
+        PhoneBook phonebook = new PhoneBook();
12
+        String expected = "1234567890"; 
13
+        phonebook.add("tyler",expected);
14
+        String actual = phonebook.lookUp("tyler");
15
+        assertEquals(actual, expected);
16
+
21 17
     }
22 18
 
23
-    /**
24
-     * Sets up the test fixture.
25
-     *
26
-     * Called before every test case method.
27
-     */
28
-    @Before
29
-    public void setUp()
30
-    {
19
+    @Test
20
+    public void testLookUp(){
21
+        PhoneBook phonebook = new PhoneBook();
22
+        String expected = "1234567890";
23
+        phonebook.add("tyler", expected);
24
+        String actual = phonebook.lookUp("tyler");
25
+        assertEquals(expected, actual);
31 26
     }
32 27
 
33
-    /**
34
-     * Tears down the test fixture.
35
-     *
36
-     * Called after every test case method.
37
-     */
38
-    @After
39
-    public void tearDown()
40
-    {
28
+    @Test
29
+    public void testReverse(){
30
+        PhoneBook phonebook = new PhoneBook();
31
+        String expected = "tyler";
32
+        phonebook.add("tyler", "1234567890");
33
+        String actual = phonebook.reverseLookUp("1234567890");
34
+        assertEquals(expected, actual);
41 35
     }
36
+
42 37
 }