浏览代码

passed test cases

Eric Foster 6 年前
父节点
当前提交
6d41c7f24a
共有 4 个文件被更改,包括 192 次插入94 次删除
  1. 59
    20
      PhoneBook.java
  2. 93
    24
      PhoneBookTest.java
  3. 0
    33
      PhoneNumbers.java
  4. 40
    17
      package.bluej

+ 59
- 20
PhoneBook.java 查看文件

@@ -1,39 +1,78 @@
1
-import java.util.*; 
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);
7
+    Map<String, List<String>> phoneBook = new TreeMap<String, List<String>>();
8
+
9
+    public void addEntry(String name, String... phoneNumbers){
10
+        phoneBook.put(name, Arrays.asList(phoneNumbers));
13 11
     }
14
-    
15
-    public void remove(String name){
12
+
13
+    public void removeEntry(String name){
16 14
         phoneBook.remove(name);
17 15
     }
18
-    
16
+
17
+    public void addNumberToEntry(String name, String... phoneNumbers){
18
+        List<String> listToUpdate = new ArrayList<>(phoneBook.get(name));
19
+        for (String aNumber : phoneNumbers){
20
+            listToUpdate.add(aNumber);
21
+        }
22
+        String[] updatedNumbers = listToUpdate.toArray(new String[listToUpdate.size()]);
23
+        addEntry(name, updatedNumbers);
24
+    }
25
+
26
+    public void removeNumberFromEntry(String name, String... phoneNumbers){
27
+        List<String> listToUpdate = new ArrayList<>(phoneBook.get(name));
28
+        Iterator<String> i = listToUpdate.iterator();
29
+        while(i.hasNext()){
30
+            String aNumberInList = i.next();
31
+            for (String aNumberToDelete : phoneNumbers){
32
+                if (aNumberInList.equals(aNumberToDelete)){
33
+                    i.remove();
34
+                }
35
+            }
36
+        }
37
+        String[] updatedNumbers = listToUpdate.toArray(new String[listToUpdate.size()]);
38
+        addEntry(name, updatedNumbers);
39
+    }
40
+
19 41
     public String lookup(String name){
20
-        return phoneBook.get(name);
42
+        if(phoneBook.get(name)==null){
43
+            return null;
44
+        }
45
+        StringBuilder numbers = new StringBuilder();
46
+        int counter = 0;
47
+        for (String aNumber : phoneBook.get(name)){
48
+            numbers.append(aNumber);
49
+            counter++;
50
+            if(counter != phoneBook.get(name).size()){
51
+                numbers.append(", ");
52
+            }
53
+        }
54
+        return numbers.toString();
21 55
     }
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();
56
+
57
+    public String reverseLookup(String... phoneNumbers){
58
+        for(Map.Entry<String, List<String>> e : phoneBook.entrySet()){
59
+            String name = e.getKey();
60
+            for(String aNumberInList : phoneBook.get(name)){
61
+                for (String aNumberToLookup : phoneNumbers){
62
+                    if(aNumberInList.equals(aNumberToLookup)){
63
+                        return e.getKey();
64
+                    }
65
+                }
27 66
             }
28 67
         }
29 68
         return null;
30 69
     }
31
-    
70
+
32 71
     public void display(){
33
-        System.out.printf("%-20s%-12s\n", "Name", "Phone Number");
34
-        for(Map.Entry<String, String> e : phoneBook.entrySet()){
72
+        System.out.printf("%-20s%-12s\n", "Name", "Phone Number(s)");
73
+        for(Map.Entry<String, List<String>> e : phoneBook.entrySet()){
35 74
             String printName = e.getKey();
36
-            String printNumber = e.getValue();
75
+            List<String> printNumber = e.getValue();
37 76
             System.out.printf(" %-20s %-12s\n", printName, printNumber);
38 77
         }
39 78
         System.out.printf("-----End of the phone book-----\n");

+ 93
- 24
PhoneBookTest.java 查看文件

@@ -1,4 +1,4 @@
1
-
1
+import java.util.*; 
2 2
 import static org.junit.Assert.*;
3 3
 import org.junit.After;
4 4
 import org.junit.Before;
@@ -13,37 +13,37 @@ import org.junit.Test;
13 13
 public class PhoneBookTest
14 14
 {
15 15
     @Test
16
-    public void testAdd(){
16
+    public void testAddEntry(){
17 17
         //given
18 18
         PhoneBook phoneBook = new PhoneBook();
19 19
 
20 20
         //actual
21
-        phoneBook.add("eric", "3025884804");
21
+        phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
22 22
         String actual = phoneBook.lookup("eric");
23 23
 
24 24
         //expected
25
-        String expected = "3025884804";
25
+        String expected = "3025884804, 3025884805, 3025884806";
26 26
 
27 27
         //test
28 28
         assertEquals(expected, actual);
29 29
     }
30 30
 
31 31
     @Test
32
-    public void testAddMultiple(){
32
+    public void testAddEntryMultiple(){
33 33
         //given
34 34
         PhoneBook phoneBook = new PhoneBook();
35 35
 
36 36
         //actual
37
-        phoneBook.add("eric", "3025884804");
38
-        phoneBook.add("john", "3025844804");
39
-        phoneBook.add("jane", "3025884704");
37
+        phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
38
+        phoneBook.addEntry("john", "3025844804", "3025884705");
39
+        phoneBook.addEntry("jane", "3025884704");
40 40
         String actual = phoneBook.lookup("eric");
41 41
         String actual1 = phoneBook.lookup("john");
42 42
         String actual2 = phoneBook.lookup("jane");
43 43
 
44 44
         //expected
45
-        String expected = "3025884804";
46
-        String expected1 = "3025844804";
45
+        String expected = "3025884804, 3025884805, 3025884806";
46
+        String expected1 = "3025844804, 3025884705";
47 47
         String expected2 = "3025884704";
48 48
 
49 49
         //test
@@ -51,13 +51,13 @@ public class PhoneBookTest
51 51
     }
52 52
 
53 53
     @Test
54
-    public void testRemove(){
54
+    public void testRemoveEntry(){
55 55
         //given
56 56
         PhoneBook phoneBook = new PhoneBook();
57 57
 
58 58
         //actual
59
-        phoneBook.add("eric", "3025884804");
60
-        phoneBook.remove("eric");
59
+        phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
60
+        phoneBook.removeEntry("eric");
61 61
         String actual = phoneBook.lookup("eric");
62 62
 
63 63
         //expected
@@ -68,16 +68,16 @@ public class PhoneBookTest
68 68
     }
69 69
 
70 70
     @Test
71
-    public void testRemoveMultiple(){
71
+    public void testRemoveEntryMultiple(){
72 72
         //given
73 73
         PhoneBook phoneBook = new PhoneBook();
74 74
 
75 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");
76
+        phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
77
+        phoneBook.addEntry("john", "3025844804", "3025884705");
78
+        phoneBook.addEntry("jane", "3025884704");
79
+        phoneBook.removeEntry("john");
80
+        phoneBook.removeEntry("eric");
81 81
         String actual = phoneBook.lookup("john");
82 82
         String actual1 = phoneBook.lookup("eric");
83 83
         String actual2 = phoneBook.lookup("jane");
@@ -99,11 +99,11 @@ public class PhoneBookTest
99 99
         PhoneBook phoneBook = new PhoneBook();
100 100
 
101 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");
102
+        phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
103
+        phoneBook.addEntry("john", "3025844804", "3025884705");
104
+        phoneBook.addEntry("jane", "3025884704");
105
+        String actual = phoneBook.reverseLookup("3025884804", "3025884805", "3025884806");
106
+        String actual1 = phoneBook.reverseLookup("3025844804", "3025884705");
107 107
         String actual2 = phoneBook.reverseLookup("3025884704");
108 108
         String actual3 = phoneBook.reverseLookup("4025884704");
109 109
 
@@ -117,5 +117,74 @@ public class PhoneBookTest
117 117
         assertEquals(expected, actual);
118 118
         assertEquals(expected1, actual1);
119 119
         assertEquals(expected2, actual2);
120
+        assertEquals(expected3, actual3);
121
+    }
122
+    
123
+    @Test
124
+    public void testAddNumberToEntry(){
125
+        //given
126
+        PhoneBook phoneBook = new PhoneBook();
127
+
128
+        //actual
129
+        phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
130
+        phoneBook.addNumberToEntry("eric", "3025884807");
131
+        String actual = phoneBook.lookup("eric");
132
+
133
+        //expected
134
+        String expected = "3025884804, 3025884805, 3025884806, 3025884807";
135
+
136
+        //test
137
+        assertEquals(expected, actual);
138
+    }
139
+
140
+    @Test
141
+    public void testAddNumberToEntryMultiple(){
142
+        //given
143
+        PhoneBook phoneBook = new PhoneBook();
144
+
145
+        //actual
146
+        phoneBook.addEntry("john", "3025884804", "3025884705");
147
+        phoneBook.addNumberToEntry("john", "3025884788", "3025884789");
148
+        String actual = phoneBook.lookup("john");
149
+
150
+        //expected
151
+        String expected = "3025884804, 3025884705, 3025884788, 3025884789";
152
+
153
+        //test
154
+        assertEquals(expected, actual);
155
+    }
156
+
157
+    @Test
158
+    public void testRemoveNumberFromEntry(){
159
+        //given
160
+        PhoneBook phoneBook = new PhoneBook();
161
+
162
+        //actual
163
+        phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
164
+        phoneBook.removeNumberFromEntry("eric", "3025884805");
165
+        String actual = phoneBook.lookup("eric");
166
+
167
+        //expected
168
+        String expected = "3025884804, 3025884806";
169
+
170
+        //test
171
+        assertEquals(expected, actual);
172
+    }
173
+
174
+    @Test
175
+    public void testRemoveNumberFromEntryMultiple(){
176
+        //given
177
+        PhoneBook phoneBook = new PhoneBook();
178
+
179
+        //actual
180
+        phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806", "3025884807");
181
+        phoneBook.removeNumberFromEntry("eric", "3025884804", "3025884806");
182
+        String actual = phoneBook.lookup("eric");
183
+
184
+        //expected
185
+        String expected = "3025884805, 3025884807";
186
+
187
+        //test
188
+        assertEquals(expected, actual);
120 189
     }
121 190
 }

+ 0
- 33
PhoneNumbers.java 查看文件

@@ -1,33 +0,0 @@
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
-}

+ 40
- 17
package.bluej 查看文件

@@ -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
6
-objectbench.height=101
7
-objectbench.width=461
8
-package.divider.horizontal=0.6
9
-package.divider.vertical=0.8007380073800738
10
-package.editor.height=427
11
-package.editor.width=674
12
-package.editor.x=427
13
-package.editor.y=143
14
-package.frame.height=600
15
-package.frame.width=800
16
-package.numDependencies=0
17
-package.numTargets=1
2
+dependency1.from=PhoneBookTest
3
+dependency1.to=PhoneBook
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=713
6
+editor.fx.0.width=800
7
+editor.fx.0.x=-122
8
+editor.fx.0.y=-943
9
+objectbench.height=123
10
+objectbench.width=381
11
+package.divider.horizontal=0.5914634146341463
12
+package.divider.vertical=0.7644927536231884
13
+package.editor.height=415
14
+package.editor.width=550
15
+package.editor.x=46
16
+package.editor.y=-873
17
+package.frame.height=610
18
+package.frame.width=676
19
+package.numDependencies=1
20
+package.numTargets=3
18 21
 package.showExtends=true
19 22
 package.showUses=true
20 23
 project.charset=UTF-8
@@ -23,4 +26,24 @@ 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=PhoneNumbers
31
+target1.showInterface=false
32
+target1.type=ClassTarget
33
+target1.width=120
34
+target1.x=240
35
+target1.y=70
36
+target2.height=50
37
+target2.name=PhoneBookTest
38
+target2.showInterface=false
39
+target2.type=UnitTestTargetJunit4
40
+target2.width=120
41
+target2.x=100
42
+target2.y=10
43
+target3.height=50
44
+target3.name=PhoneBook
45
+target3.showInterface=false
46
+target3.type=ClassTarget
47
+target3.width=100
48
+target3.x=70
49
+target3.y=70