Browse Source

finished 2nd part of phonebook

Jamez-s 6 years ago
parent
commit
d20413a7d5
4 changed files with 171 additions and 153 deletions
  1. 46
    30
      .idea/workspace.xml
  2. 66
    61
      PhoneBook.java
  3. 52
    55
      PhoneBookTest.java
  4. 7
    7
      package.bluej

+ 46
- 30
.idea/workspace.xml View File

@@ -2,9 +2,7 @@
2 2
 <project version="4">
3 3
   <component name="ChangeListManager">
4 4
     <list default="true" id="cb125da0-90b8-46e4-b936-89bfc9d100db" name="Default" comment="">
5
-      <change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
6 5
       <change beforePath="$PROJECT_DIR$/PhoneBook.java" beforeDir="false" afterPath="$PROJECT_DIR$/PhoneBook.java" afterDir="false" />
7
-      <change beforePath="$PROJECT_DIR$/PhoneBookTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/PhoneBookTest.java" afterDir="false" />
8 6
       <change beforePath="$PROJECT_DIR$/package.bluej" beforeDir="false" afterPath="$PROJECT_DIR$/package.bluej" afterDir="false" />
9 7
     </list>
10 8
     <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@@ -15,21 +13,21 @@
15 13
     <option name="LAST_RESOLUTION" value="IGNORE" />
16 14
   </component>
17 15
   <component name="FileEditorManager">
18
-    <leaf>
19
-      <file leaf-file-name="PhoneBook.java" pinned="false" current-in-tab="false">
16
+    <leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
17
+      <file leaf-file-name="PhoneBook.java" pinned="false" current-in-tab="true">
20 18
         <entry file="file://$PROJECT_DIR$/PhoneBook.java">
21 19
           <provider selected="true" editor-type-id="text-editor">
22
-            <state relative-caret-position="135">
23
-              <caret line="9" selection-start-line="9" selection-end-line="9" />
20
+            <state relative-caret-position="213">
21
+              <caret line="18" lean-forward="true" selection-start-line="18" selection-end-line="18" />
24 22
             </state>
25 23
           </provider>
26 24
         </entry>
27 25
       </file>
28
-      <file leaf-file-name="PhoneBookTest.java" pinned="false" current-in-tab="true">
26
+      <file leaf-file-name="PhoneBookTest.java" pinned="false" current-in-tab="false">
29 27
         <entry file="file://$PROJECT_DIR$/PhoneBookTest.java">
30 28
           <provider selected="true" editor-type-id="text-editor">
31
-            <state relative-caret-position="300">
32
-              <caret line="25" selection-start-line="25" selection-end-line="25" />
29
+            <state relative-caret-position="93">
30
+              <caret line="18" column="38" selection-start-line="18" selection-start-column="38" selection-end-line="18" selection-end-column="38" />
33 31
               <folding>
34 32
                 <element signature="e#581#582#0" expanded="true" />
35 33
                 <element signature="e#634#635#0" expanded="true" />
@@ -48,6 +46,8 @@
48 46
       <list>
49 47
         <option value="$PROJECT_DIR$/pets" />
50 48
         <option value="$APPLICATION_CONFIG_DIR$/scratches/scratch_1.java" />
49
+        <option value="$PROJECT_DIR$/PhoneBook.java" />
50
+        <option value="$PROJECT_DIR$/PhoneBookTest.java" />
51 51
       </list>
52 52
     </option>
53 53
   </component>
@@ -67,8 +67,8 @@
67 67
     <packageJsonPaths />
68 68
   </component>
69 69
   <component name="ProjectFrameBounds">
70
-    <option name="x" value="202" />
71
-    <option name="y" value="167" />
70
+    <option name="x" value="39" />
71
+    <option name="y" value="107" />
72 72
     <option name="width" value="1401" />
73 73
     <option name="height" value="721" />
74 74
   </component>
@@ -107,8 +107,8 @@
107 107
           <select />
108 108
         </subPane>
109 109
       </pane>
110
-      <pane id="AndroidView" />
111 110
       <pane id="Scope" />
111
+      <pane id="AndroidView" />
112 112
     </panes>
113 113
   </component>
114 114
   <component name="PropertiesComponent">
@@ -186,14 +186,16 @@
186 186
       <option name="presentableId" value="Default" />
187 187
       <updated>1528392361016</updated>
188 188
       <workItem from="1528392362380" duration="10476000" />
189
+      <workItem from="1528750637434" duration="2893000" />
189 190
     </task>
190 191
     <servers />
191 192
   </component>
192 193
   <component name="TimeTrackingManager">
193
-    <option name="totallyTimeSpent" value="10476000" />
194
+    <option name="totallyTimeSpent" value="13369000" />
194 195
   </component>
195 196
   <component name="ToolWindowManager">
196
-    <frame x="202" y="167" width="1401" height="721" extended-state="0" />
197
+    <frame x="39" y="107" width="1401" height="721" extended-state="0" />
198
+    <editor active="true" />
197 199
     <layout>
198 200
       <window_info anchor="right" id="Palette" order="5" />
199 201
       <window_info anchor="bottom" id="TODO" order="11" />
@@ -207,9 +209,8 @@
207 209
       <window_info anchor="bottom" id="Version Control" order="4" />
208 210
       <window_info anchor="bottom" id="Terminal" order="2" />
209 211
       <window_info id="Capture Tool" order="2" />
210
-      <window_info anchor="bottom" id="TypeScript" order="0" />
211 212
       <window_info id="Designer" order="0" />
212
-      <window_info active="true" content_ui="combo" id="Project" order="5" visible="true" weight="0.24944812" />
213
+      <window_info active="true" content_ui="combo" id="Project" order="5" visible="true" weight="0.2531273" />
213 214
       <window_info anchor="right" id="Database" order="3" />
214 215
       <window_info id="Structure" order="6" side_tool="true" weight="0.25" />
215 216
       <window_info anchor="right" id="Ant Build" order="7" weight="0.25" />
@@ -217,12 +218,13 @@
217 218
       <window_info anchor="right" id="Theme Preview" order="2" />
218 219
       <window_info anchor="bottom" id="Debug" order="8" weight="0.4" />
219 220
       <window_info id="Favorites" order="3" side_tool="true" />
220
-      <window_info anchor="bottom" id="Find" order="6" />
221
-      <window_info anchor="right" id="Commander" internal_type="SLIDING" order="6" type="SLIDING" weight="0.4" />
222 221
       <window_info anchor="right" content_ui="combo" id="Hierarchy" order="8" weight="0.25" />
223 222
       <window_info anchor="bottom" id="Inspection" order="10" weight="0.4" />
223
+      <window_info anchor="right" id="Commander" internal_type="SLIDING" order="6" type="SLIDING" weight="0.4" />
224
+      <window_info anchor="bottom" id="TypeScript" order="0" />
224 225
       <window_info anchor="bottom" id="Message" order="5" />
225 226
       <window_info anchor="bottom" id="Cvs" order="9" weight="0.25" />
227
+      <window_info anchor="bottom" id="Find" order="6" />
226 228
     </layout>
227 229
   </component>
228 230
   <component name="TypeScriptGeneratedFilesManager">
@@ -232,17 +234,28 @@
232 234
     <option name="myLimit" value="2678400000" />
233 235
   </component>
234 236
   <component name="editorHistoryManager">
235
-    <entry file="file://$PROJECT_DIR$/pets">
237
+    <entry file="file://$PROJECT_DIR$/PhoneBook.java">
236 238
       <provider selected="true" editor-type-id="text-editor">
237
-        <state relative-caret-position="15">
238
-          <caret line="1" lean-forward="true" selection-start-line="1" selection-end-line="1" />
239
+        <state relative-caret-position="135">
240
+          <caret line="9" selection-start-line="9" selection-end-line="9" />
239 241
         </state>
240 242
       </provider>
241 243
     </entry>
242
-    <entry file="file://$PROJECT_DIR$/PhoneBook.java">
244
+    <entry file="file://$PROJECT_DIR$/PhoneBookTest.java">
243 245
       <provider selected="true" editor-type-id="text-editor">
244
-        <state relative-caret-position="135">
245
-          <caret line="9" selection-start-line="9" selection-end-line="9" />
246
+        <state relative-caret-position="300">
247
+          <caret line="25" selection-start-line="25" selection-end-line="25" />
248
+          <folding>
249
+            <element signature="e#581#582#0" expanded="true" />
250
+            <element signature="e#634#635#0" expanded="true" />
251
+          </folding>
252
+        </state>
253
+      </provider>
254
+    </entry>
255
+    <entry file="file://$PROJECT_DIR$/pets">
256
+      <provider selected="true" editor-type-id="text-editor">
257
+        <state relative-caret-position="15">
258
+          <caret line="1" lean-forward="true" selection-start-line="1" selection-end-line="1" />
246 259
         </state>
247 260
       </provider>
248 261
     </entry>
@@ -250,17 +263,13 @@
250 263
       <provider selected="true" editor-type-id="text-editor">
251 264
         <state relative-caret-position="30">
252 265
           <caret line="2" column="9" selection-start-line="2" selection-start-column="9" selection-end-line="2" selection-end-column="9" />
253
-          <folding>
254
-            <element signature="e#59#60#0" expanded="true" />
255
-            <element signature="e#71#72#0" expanded="true" />
256
-          </folding>
257 266
         </state>
258 267
       </provider>
259 268
     </entry>
260 269
     <entry file="file://$PROJECT_DIR$/PhoneBookTest.java">
261 270
       <provider selected="true" editor-type-id="text-editor">
262
-        <state relative-caret-position="300">
263
-          <caret line="25" selection-start-line="25" selection-end-line="25" />
271
+        <state relative-caret-position="93">
272
+          <caret line="18" column="38" selection-start-line="18" selection-start-column="38" selection-end-line="18" selection-end-column="38" />
264 273
           <folding>
265 274
             <element signature="e#581#582#0" expanded="true" />
266 275
             <element signature="e#634#635#0" expanded="true" />
@@ -268,5 +277,12 @@
268 277
         </state>
269 278
       </provider>
270 279
     </entry>
280
+    <entry file="file://$PROJECT_DIR$/PhoneBook.java">
281
+      <provider selected="true" editor-type-id="text-editor">
282
+        <state relative-caret-position="213">
283
+          <caret line="18" lean-forward="true" selection-start-line="18" selection-end-line="18" />
284
+        </state>
285
+      </provider>
286
+    </entry>
271 287
   </component>
272 288
 </project>

+ 66
- 61
PhoneBook.java View File

@@ -4,84 +4,89 @@ import java.util.*;
4 4
  * Created by leon on 1/23/18.
5 5
  */
6 6
 public class PhoneBook {
7
-  String name;
8
-  String phoneNumber;
9
-  TreeMap<String, ArrayList<String>> phoneBook = new TreeMap<>();
7
+    String name;
8
+    String phoneNumber;
9
+    TreeMap<String, ArrayList<String>> phoneBook = new TreeMap<>();
10 10
 
11
-  public PhoneBook(TreeMap<String, ArrayList<String>> phoneBook) {
12
-    this.phoneBook = phoneBook;
13
-  }
11
+    public PhoneBook(TreeMap<String, ArrayList<String>> phoneBook) {
12
+        this.phoneBook = phoneBook;
13
+    }
14 14
 
15
-  public PhoneBook(){}
15
+    public PhoneBook(){}
16 16
 
17
-  public void add(String name, String phoneNumber){
18
-    ArrayList<String> list;
17
+    public void add(String name, String phoneNumber){
18
+        if (!phoneBook.containsKey(name)) {
19
+            ArrayList<String> list = new ArrayList<>();
20
+            list.add(phoneNumber);
21
+            phoneBook.put(name, list);          
22
+        } else {
19 23
 
20
-    if (!phoneBook.containsKey(name)) {
21
-      list = new ArrayList<>();
22
-      phoneBook.put(name, list);
23
-    }
24
-    else
25
-    {
26
-      list = phoneBook.get(name);
27
-      list.add(phoneNumber);
28
-      phoneBook.replace(name, list);
24
+            ArrayList<String> temp = phoneBook.get(name);
25
+            temp.add(phoneNumber);
26
+            phoneBook.replace(name, phoneBook.get(name), temp);//key,old,new
27
+
28
+        }   
29 29
     }
30
-  }
31 30
 
32
-  public void remove(String name){
33
-    phoneBook.remove(name);
34
-  }
31
+    public void remove(String name, String phoneNumber){
32
+        ArrayList<String> list = phoneBook.get(name);
35 33
 
36
-  public String lookup(String name){
37
-    ArrayList<String> numbers = phoneBook.get(name);
38
-    StringBuilder sb = new StringBuilder();
34
+        if(list.size() == 1) {
35
+            phoneBook.remove(name); 
36
+        } else {
37
+            ArrayList<String> temp = phoneBook.get(name);
38
+            temp.remove(phoneNumber);
39
+            phoneBook.replace(name,list,temp);
40
+        }
41
+    }
39 42
 
40
-    for (int i=0; i<numbers.size(); i++) {
41
-      sb.append(numbers.get(i));
42
-      if (i == numbers.size()-1) {
43
-        break;
44
-      } else {
45
-        sb.append(", ");
46
-      }
43
+    public void removeRecord(String name) {
44
+        phoneBook.remove(name);
47 45
     }
48
-    sb.append("");
49
-    return sb.toString();
50
-  }
51 46
 
52
-  public String reverseLookup(String phoneNumber) {
53
-    for (Map.Entry<String, ArrayList<String>> e : phoneBook.entrySet()) {
54
-      String name = e.getKey();
55
-      ArrayList<String> numbers = e.getValue();
47
+    public ArrayList<String> lookup(String name){
48
+        return phoneBook.get(name);
49
+    }
56 50
 
57
-      for (String s : numbers) {
58
-        if (s == phoneNumber) {
59
-          return name;
51
+    public String reverseLookup(String phoneNumber) {
52
+        // for each entry in phonebook
53
+        for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()) {
54
+            // gets name
55
+            String name = entry.getKey();
56
+            // gets list of numbers
57
+            ArrayList<String> list = entry.getValue();
58
+            // if number equals the number we are looking for, return name
59
+            for (String number: list) {
60
+                if (number.equals(phoneNumber)) {
61
+                    return name;
62
+                } 
63
+            }           
60 64
         }
61
-      }
65
+        return "Not found";
62 66
     }
63
-    return "Name not found";
64
-  }
65 67
 
66
-  public String display() {
67
-    StringBuilder sb = new StringBuilder();
68
+    public String display() {
69
+        // build a string to display
70
+        StringBuilder sb = new StringBuilder();
71
+        
72
+        for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()){
73
+            
74
+            String name = entry.getKey();
75
+            ArrayList<String> numbers = entry.getValue();
68 76
 
69
-    for (Map.Entry<String, ArrayList<String>> e : phoneBook.entrySet()){
70
-      String name = e.getKey();
71
-      ArrayList<String> numbers = e.getValue();
77
+            sb.append(name).append(" ");
72 78
 
73
-      sb.append(name).append(" ");
79
+            // for each number in element, append to name
80
+            for (int i = 0; i < numbers.size(); i++) {
81
+                sb.append(numbers.get(i));
74 82
 
75
-      for (int i=0; i<numbers.size(); i++) {
76
-        sb.append(numbers.get(i));
77
-        if (i == numbers.size()-1) {
78
-          break;
79
-        } else {
80
-          sb.append(", ");
83
+                // append a space if i is not the last number in list
84
+                if (i != numbers.size()-1) {
85
+                    sb.append(" ");
86
+                }
87
+            }
88
+            sb.append("\n");
81 89
         }
82
-      }
83
-      sb.append("");
90
+        return sb.toString();
84 91
     }
85
-    return sb.toString();
86
-  }
87 92
 }

+ 52
- 55
PhoneBookTest.java View File

@@ -1,5 +1,4 @@
1 1
 
2
-
3 2
 import org.junit.Assert;
4 3
 import org.junit.After;
5 4
 import org.junit.Before;
@@ -32,7 +31,7 @@ public class PhoneBookTest
32 31
     @Before
33 32
     public void setUp()
34 33
     {
35
-      PhoneBook pb = new PhoneBook(phoneBook);
34
+        PhoneBook pb = new PhoneBook();
36 35
     }
37 36
 
38 37
     /**
@@ -48,74 +47,72 @@ public class PhoneBookTest
48 47
 
49 48
     // add entries
50 49
     @Test
51
-    public void testOne() { 
52
-      String expected = "Dog 222-444-5555\nZebra 111-222-3333\n";
50
+    public void testAdd() { 
51
+        String expected = "Zebra 111-222-3333 222-444-5555\n";
53 52
 
54
-      PhoneBook pb = new PhoneBook(phoneBook);
55
-      pb.add("Zebra", "111-222-3333");
56
-      pb.add("Dog", "222-444-5555");
53
+        PhoneBook pb = new PhoneBook();
54
+        pb.add("Zebra", "111-222-3333");
55
+        pb.add("Zebra", "222-444-5555");
57 56
 
58
-      String actual = pb.display();
57
+        String actual = pb.display();
59 58
 
60
-      Assert.assertEquals(expected, actual);
61
-    }
62
-    @Test
63
-    public void testTwo() {
64
-      String expected = "Dog 222-444-5555\n";
65
-      PhoneBook pb = new PhoneBook(phoneBook);
66
-        
67
-      pb.add("Zebra", "111-222-3333");
68
-      pb.add("Dog", "222-444-5555");
69
-      pb.remove("Zebra");
70
-        
71
-      String actual = pb.display();
72
-
73
-      Assert.assertEquals(expected, actual);  
74
-    }
75
-    @Test
76
-    public void TestThree() {
77
-      String expected = "222-444-5555";
78
-      PhoneBook pb = new PhoneBook(phoneBook);
79
-        
80
-      pb.add("Zebra", "111-222-3333");
81
-      pb.add("Dog", "222-444-5555");
82
-      
83
-      String actual = pb.lookup("Dog");
84
-        
85
-      Assert.assertEquals(expected, actual);
59
+        Assert.assertEquals(expected, actual);
86 60
     }
61
+
87 62
     @Test
88
-    public void TestFour() {
89
-      String expected = "Dog";
90
-      PhoneBook pb = new PhoneBook(phoneBook);
91
-        
92
-      pb.add("Zebra", "111-222-3333");
93
-      pb.add("Dog", "222-444-5555");
94
-      
95
-      String actual = pb.reverseLookup("222-444-5555");
96
-      Assert.assertEquals(expected, actual);
63
+    public void testRemove() {
64
+        String expected = "Zebra 222-444-5555\n";
65
+        PhoneBook pb = new PhoneBook(phoneBook);
66
+
67
+        pb.add("Zebra", "111-222-3333");
68
+        pb.add("Zebra", "222-444-5555");
69
+        pb.remove("Zebra", "111-222-3333");
70
+
71
+        String actual = pb.display();
72
+
73
+        Assert.assertEquals(expected, actual);  
97 74
     }
98 75
 
99
-    // Testing list of phonenumbers for an entry
100 76
     @Test
101
-    public void TestFive() {
102
-        String expected = "Dog 111-222-3333, 444-555-6666\n";
77
+    public void testRemoveRecord() {
78
+        String expected = "Zebra 222-444-5555\n";
103 79
         PhoneBook pb = new PhoneBook(phoneBook);
104
-        
80
+
105 81
         pb.add("Dog", "111-222-3333");
106
-        pb.add("Dog", "444-555-6666");
107
-        
82
+        pb.add("Dog", "333-444-5555");
83
+        pb.add("Zebra", "222-444-5555");
84
+        pb.removeRecord("Dog");
85
+
108 86
         String actual = pb.display();
109
-        Assert.assertEquals(expected, actual);
87
+
88
+        Assert.assertEquals(expected, actual);  
110 89
     }
111
-    
112 90
 
113 91
     @Test
114
-    public void Test2()
115
-    {
92
+    public void testLookup() {
93
+
94
+        ArrayList<String> expected = new ArrayList<>();
95
+        expected.add("111-222-3333");
96
+        expected.add("222-444-5555");
97
+        PhoneBook pb = new PhoneBook(phoneBook);
98
+
99
+        pb.add("Zebra", "111-222-3333");
100
+        pb.add("Zebra", "222-444-5555");
101
+
102
+        ArrayList<String> actual = pb.lookup("Zebra");
103
+        Assert.assertEquals(expected, actual);  
116 104
     }
117 105
 
118
-    
119
-}
106
+    @Test
107
+    public void testReverseLookup() {
108
+
109
+        String expected = "Zebra";    
110
+        PhoneBook pb = new PhoneBook(phoneBook);
120 111
 
112
+        pb.add("Zebra", "111-222-3333");
113
+        pb.add("Zebra", "222-444-5555");
121 114
 
115
+        String actual = pb.reverseLookup("222-444-5555");
116
+        Assert.assertEquals(expected, actual);  
117
+    }
118
+}

+ 7
- 7
package.bluej View File

@@ -2,18 +2,18 @@
2 2
 dependency1.from=PhoneBookTest
3 3
 dependency1.to=PhoneBook
4 4
 dependency1.type=UsesDependency
5
-editor.fx.0.height=722
5
+editor.fx.0.height=800
6 6
 editor.fx.0.width=800
7
-editor.fx.0.x=590
8
-editor.fx.0.y=125
7
+editor.fx.0.x=486
8
+editor.fx.0.y=23
9 9
 objectbench.height=101
10 10
 objectbench.width=461
11 11
 package.divider.horizontal=0.6
12 12
 package.divider.vertical=0.8007380073800738
13 13
 package.editor.height=427
14 14
 package.editor.width=674
15
-package.editor.x=35
16
-package.editor.y=158
15
+package.editor.x=499
16
+package.editor.y=60
17 17
 package.frame.height=600
18 18
 package.frame.width=800
19 19
 package.numDependencies=1
@@ -38,5 +38,5 @@ target2.name=PhoneBook
38 38
 target2.showInterface=false
39 39
 target2.type=ClassTarget
40 40
 target2.width=100
41
-target2.x=70
42
-target2.y=70
41
+target2.x=80
42
+target2.y=100