Browse Source

finished part 1

Eric Foster 6 years ago
parent
commit
8558c33810
3 changed files with 169 additions and 23 deletions
  1. 35
    1
      PhoneBook.java
  2. 101
    22
      PhoneBookTest.java
  3. 33
    0
      PhoneNumbers.java

+ 35
- 1
PhoneBook.java View File

@@ -1,7 +1,41 @@
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
+    SortedMap<String, String> phoneBook = new TreeMap<String, String>();
10
+    
11
+    public void add(String name, String phoneNumber){
12
+        phoneBook.put(name, phoneNumber);
13
+    }
14
+    
15
+    public void remove(String name){
16
+        phoneBook.remove(name);
17
+    }
18
+    
19
+    public String lookup(String name){
20
+        return phoneBook.get(name);
21
+    }
22
+    
23
+    public String reverseLookup(String phoneNumber){
24
+        for(Map.Entry<String, String> e : phoneBook.entrySet()){
25
+            if(e.getValue().equals(phoneNumber)){
26
+                return e.getKey();
27
+            }
28
+        }
29
+        return null;
30
+    }
31
+    
32
+    public void display(){
33
+        System.out.printf("%-20s%-12s\n", "Name", "Phone Number");
34
+        for(Map.Entry<String, String> e : phoneBook.entrySet()){
35
+            String printName = e.getKey();
36
+            String printNumber = e.getValue();
37
+            System.out.printf(" %-20s %-12s\n", printName, printNumber);
38
+        }
39
+        System.out.printf("-----End of the phone book-----\n");
40
+    }
7 41
 }

+ 101
- 22
PhoneBookTest.java View File

@@ -1,5 +1,4 @@
1 1
 
2
-
3 2
 import static org.junit.Assert.*;
4 3
 import org.junit.After;
5 4
 import org.junit.Before;
@@ -13,30 +12,110 @@ import org.junit.Test;
13 12
  */
14 13
 public class PhoneBookTest
15 14
 {
16
-    /**
17
-     * Default constructor for test class PhoneBookTest
18
-     */
19
-    public PhoneBookTest()
20
-    {
15
+    @Test
16
+    public void testAdd(){
17
+        //given
18
+        PhoneBook phoneBook = new PhoneBook();
19
+
20
+        //actual
21
+        phoneBook.add("eric", "3025884804");
22
+        String actual = phoneBook.lookup("eric");
23
+
24
+        //expected
25
+        String expected = "3025884804";
26
+
27
+        //test
28
+        assertEquals(expected, actual);
29
+    }
30
+
31
+    @Test
32
+    public void testAddMultiple(){
33
+        //given
34
+        PhoneBook phoneBook = new PhoneBook();
35
+
36
+        //actual
37
+        phoneBook.add("eric", "3025884804");
38
+        phoneBook.add("john", "3025844804");
39
+        phoneBook.add("jane", "3025884704");
40
+        String actual = phoneBook.lookup("eric");
41
+        String actual1 = phoneBook.lookup("john");
42
+        String actual2 = phoneBook.lookup("jane");
43
+
44
+        //expected
45
+        String expected = "3025884804";
46
+        String expected1 = "3025844804";
47
+        String expected2 = "3025884704";
48
+
49
+        //test
50
+        assertEquals(expected, actual);
51
+    }
52
+
53
+    @Test
54
+    public void testRemove(){
55
+        //given
56
+        PhoneBook phoneBook = new PhoneBook();
57
+
58
+        //actual
59
+        phoneBook.add("eric", "3025884804");
60
+        phoneBook.remove("eric");
61
+        String actual = phoneBook.lookup("eric");
62
+
63
+        //expected
64
+        String expected = null;
65
+
66
+        //test
67
+        assertEquals(expected, actual);
21 68
     }
22 69
 
23
-    /**
24
-     * Sets up the test fixture.
25
-     *
26
-     * Called before every test case method.
27
-     */
28
-    @Before
29
-    public void setUp()
30
-    {
70
+    @Test
71
+    public void testRemoveMultiple(){
72
+        //given
73
+        PhoneBook phoneBook = new PhoneBook();
74
+
75
+        //actual
76
+        phoneBook.add("eric", "3025884804");
77
+        phoneBook.add("john", "3025844804");
78
+        phoneBook.add("jane", "3025884704");
79
+        phoneBook.remove("john");
80
+        phoneBook.remove("eric");
81
+        String actual = phoneBook.lookup("john");
82
+        String actual1 = phoneBook.lookup("eric");
83
+        String actual2 = phoneBook.lookup("jane");
84
+
85
+        //expected
86
+        String expected = null;
87
+        String expected1 = null;
88
+        String expected2 = "3025884704";
89
+
90
+        //test
91
+        assertEquals(expected, actual);
92
+        assertEquals(expected1, actual1);
93
+        assertEquals(expected2, actual2);
31 94
     }
32 95
 
33
-    /**
34
-     * Tears down the test fixture.
35
-     *
36
-     * Called after every test case method.
37
-     */
38
-    @After
39
-    public void tearDown()
40
-    {
96
+    @Test
97
+    public void testReverseLookup(){
98
+        //given
99
+        PhoneBook phoneBook = new PhoneBook();
100
+
101
+        //actual
102
+        phoneBook.add("eric", "3025884804");
103
+        phoneBook.add("john", "3025844804");
104
+        phoneBook.add("jane", "3025884704");
105
+        String actual = phoneBook.reverseLookup("3025884804");
106
+        String actual1 = phoneBook.reverseLookup("3025844804");
107
+        String actual2 = phoneBook.reverseLookup("3025884704");
108
+        String actual3 = phoneBook.reverseLookup("4025884704");
109
+
110
+        //expected
111
+        String expected = "eric";
112
+        String expected1 = "john";
113
+        String expected2 = "jane";
114
+        String expected3 = null;
115
+
116
+        //test
117
+        assertEquals(expected, actual);
118
+        assertEquals(expected1, actual1);
119
+        assertEquals(expected2, actual2);
41 120
     }
42 121
 }

+ 33
- 0
PhoneNumbers.java View File

@@ -0,0 +1,33 @@
1
+
2
+/**
3
+ * Write a description of class PhoneNumbers here.
4
+ *
5
+ * @author (your name)
6
+ * @version (a version number or a date)
7
+ */
8
+public class PhoneNumbers
9
+{
10
+    // instance variables - replace the example below with your own
11
+    private int x;
12
+
13
+    /**
14
+     * Constructor for objects of class PhoneNumbers
15
+     */
16
+    public PhoneNumbers()
17
+    {
18
+        // initialise instance variables
19
+        x = 0;
20
+    }
21
+
22
+    /**
23
+     * An example of a method - replace this comment with your own
24
+     *
25
+     * @param  y  a sample parameter for a method
26
+     * @return    the sum of x and y
27
+     */
28
+    public int sampleMethod(int y)
29
+    {
30
+        // put your code here
31
+        return x + y;
32
+    }
33
+}