Selaa lähdekoodia

TreeMap version, basic functionality

Jared Norris 6 vuotta sitten
vanhempi
commit
f257fe7d6f
2 muutettua tiedostoa jossa 86 lisäystä ja 27 poistoa
  1. 41
    1
      PhoneBook.java
  2. 45
    26
      PhoneBookTest.java

+ 41
- 1
PhoneBook.java Näytä tiedosto

@@ -1,7 +1,47 @@
1 1
  
2
-
2
+import java.util.TreeMap;
3 3
 /**
4 4
  * Created by leon on 1/23/18.
5 5
  */
6 6
 public class PhoneBook {
7
+    private TreeMap<String, String> phoneBook;
8
+    private TreeMap<String, String> reversePhoneBook;
9
+    
10
+    public PhoneBook() {
11
+        phoneBook = new TreeMap<String, String>();
12
+        reversePhoneBook = new TreeMap<String, String>();
13
+    }
14
+    
15
+    public void add(String name, String phoneNumber) {
16
+        phoneBook.put(name, phoneNumber);
17
+        reversePhoneBook.put(phoneNumber, name);
18
+    }
19
+    
20
+    public void remove(String name) {
21
+        String phoneNumber = phoneBook.get(name);
22
+        phoneBook.remove(name);
23
+        reversePhoneBook.remove(phoneNumber);
24
+    }
25
+    
26
+    public String lookup(String name) {
27
+        return phoneBook.get(name) != null ? 
28
+                                   phoneBook.get(name)
29
+                                   : 
30
+                                   "N/A";
31
+    }
32
+    
33
+    public String reverseLookup(String phoneNumber) {
34
+        return reversePhoneBook.get(phoneNumber) != null ? 
35
+                                                 reversePhoneBook.get(phoneNumber)
36
+                                                 :
37
+                                                 "N/A";
38
+    }
39
+    
40
+    public TreeMap getPhoneBook() {
41
+        return phoneBook; 
42
+    }
43
+    
44
+    public TreeMap getReversePhoneBook() {
45
+        return reversePhoneBook;
46
+    }
7 47
 }

+ 45
- 26
PhoneBookTest.java Näytä tiedosto

@@ -5,38 +5,57 @@ import org.junit.After;
5 5
 import org.junit.Before;
6 6
 import org.junit.Test;
7 7
 
8
-/**
9
- * The test class PhoneBookTest.
10
- *
11
- * @author  (your name)
12
- * @version (a version number or a date)
13
- */
8
+
14 9
 public class PhoneBookTest
15 10
 {
16
-    /**
17
-     * Default constructor for test class PhoneBookTest
18
-     */
19
-    public PhoneBookTest()
11
+    @Test
12
+    public void testAdd() 
20 13
     {
14
+       PhoneBook test = new PhoneBook();
15
+       boolean expected = true;
16
+       String name = "Jenny";
17
+       String number = "(302)-867-5309";
18
+       test.add(name, number);
19
+       
20
+       boolean actual = test.getPhoneBook().containsKey(name);
21
+       assertEquals(expected, actual);  
21 22
     }
22
-
23
-    /**
24
-     * Sets up the test fixture.
25
-     *
26
-     * Called before every test case method.
27
-     */
28
-    @Before
29
-    public void setUp()
23
+    // vvvv I hope this is okay since I know the add method works now vvvv
24
+    @Test
25
+    public void testAddPhoneBookEntry_And_Lookup()
30 26
     {
27
+       PhoneBook test = new PhoneBook();
28
+       String name = "Jenny";
29
+       String expectedNumber = "(302)-867-5309";
30
+       test.add(name, expectedNumber);
31
+       
32
+       String actual = test.lookup(name); 
33
+       assertEquals(expectedNumber, actual);
31 34
     }
32
-
33
-    /**
34
-     * Tears down the test fixture.
35
-     *
36
-     * Called after every test case method.
37
-     */
38
-    @After
39
-    public void tearDown()
35
+    
36
+    @Test
37
+    public void testRemovePhoneBookEntry_And_Lookup()
40 38
     {
39
+       PhoneBook test = new PhoneBook();
40
+       String name = "Jenny";
41
+       String wrongName = "Tommy Tutone";
42
+       String number = "(302)-867-5309";
43
+       test.add(name, number);
44
+       
45
+       String actual = test.lookup(wrongName); 
46
+       assertEquals("N/A", actual);
41 47
     }
48
+    
49
+    @Test
50
+    public void testReverseLookup()
51
+    {
52
+       PhoneBook test = new PhoneBook();
53
+       String phoneNumber = "(302)-867-5309";
54
+       String expectedName = "Jenny";
55
+       test.add(expectedName, phoneNumber);
56
+       
57
+       String actual = test.reverseLookup(phoneNumber);
58
+       assertEquals(expectedName, actual);
59
+     
60
+    }   
42 61
 }