Browse Source

Refactoring complete

Nicholas Maidanos 6 years ago
parent
commit
befdcb09c5
3 changed files with 56 additions and 44 deletions
  1. 25
    29
      PhoneBook.java
  2. 8
    8
      PhoneBookTest.java
  3. 23
    7
      package.bluej

+ 25
- 29
PhoneBook.java View File

@@ -5,33 +5,28 @@ import java.util.*;
5 5
  */
6 6
 public class PhoneBook {
7 7
     
8
-    Map<String, ArrayList<String> > entries = new HashMap<String, ArrayList<String>>();
8
+    Map<String, ArrayList<String> > entries = new TreeMap<String, ArrayList<String>>();
9 9
     
10
-    public void addEntry(String name, String phoneNumber){
11
-        ArrayList<String> pn = new ArrayList<String>();
12
-        pn.add(phoneNumber);
13
-        this.entries.put(name, pn);
14
-    }
15
-    
16
-    public void addNumberTo(String name, String phoneNumber){
17
-        for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
18
-            if(name.equals(entry.getKey())){
19
-                entry.getValue().add(phoneNumber);
20
-                break;
21
-            }
22
-        }    
10
+    public void add(String name, String phoneNumber){
11
+        if(this.entries.get(name) != null){
12
+            this.entries.get(name).add(phoneNumber);
13
+        } else {
14
+            ArrayList<String> pn = new ArrayList<String>();
15
+            pn.add(phoneNumber);
16
+            this.entries.put(name, pn);
17
+        }
23 18
     }
24 19
     
25 20
     public void removePhoneNumber(String name, String phoneNumber){
26
-        for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
27
-            if(name.equals(entry.getKey())){
28
-                int indexOf = entry.getValue().indexOf(phoneNumber);
29
-                if(indexOf >= 0){
30
-                    entry.getValue().remove(indexOf);
31
-                    break;
32
-                }
33
-            }
34
-        }        
21
+        
22
+        ArrayList<String> numbers = this.entries.get(name);
23
+        
24
+        if(numbers != null){
25
+           int indexOf = numbers.indexOf(phoneNumber);
26
+           if(indexOf >= 0){
27
+               numbers.remove(indexOf);
28
+           }  
29
+        }
35 30
     }
36 31
     
37 32
     public void removeRecord(String name){
@@ -40,14 +35,15 @@ public class PhoneBook {
40 35
     
41 36
     public String lookUp(String name){
42 37
         StringBuilder sb = new StringBuilder();
43
-        for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
44
-            if(entry.getKey().equals(name)){
45
-                for(String pn : entry.getValue()){
46
-                    sb.append(pn);
47
-                }   
38
+        ArrayList<String> entry = this.entries.get(name);
39
+        
40
+        if(entry != null){
41
+            for(String phoneNumber : entry){
42
+                sb.append(phoneNumber);
48 43
             }
49 44
         }
50
-        return sb.toString();        
45
+        
46
+        return sb.toString();      
51 47
         
52 48
     }
53 49
     

+ 8
- 8
PhoneBookTest.java View File

@@ -44,7 +44,7 @@ public class PhoneBookTest
44 44
     public void addEntry()
45 45
     {
46 46
         PhoneBook phoneBoo1 = new PhoneBook();
47
-        phoneBoo1.addEntry("nicholas", "3027387978");
47
+        phoneBoo1.add("nicholas", "3027387978");
48 48
         assertEquals("nicholas: 3027387978", phoneBoo1.display());
49 49
     }
50 50
 
@@ -52,8 +52,8 @@ public class PhoneBookTest
52 52
     public void addTwoNumberForOnePersonTest()
53 53
     {
54 54
         PhoneBook phoneBoo1 = new PhoneBook();
55
-        phoneBoo1.addEntry("nick", "302");
56
-        phoneBoo1.addNumberTo("nick", "103");
55
+        phoneBoo1.add("nick", "302");
56
+        phoneBoo1.add("nick", "103");
57 57
         assertEquals("nick: 302103", phoneBoo1.display());
58 58
     }
59 59
 
@@ -61,7 +61,7 @@ public class PhoneBookTest
61 61
     public void reverseLoopupTest()
62 62
     {
63 63
         PhoneBook phoneBoo1 = new PhoneBook();
64
-        phoneBoo1.addEntry("nick", "302");
64
+        phoneBoo1.add("nick", "302");
65 65
         assertEquals("nick", phoneBoo1.reverseLoopup("302"));
66 66
     }
67 67
 
@@ -69,7 +69,7 @@ public class PhoneBookTest
69 69
     public void lookupTest()
70 70
     {
71 71
         PhoneBook phoneBoo1 = new PhoneBook();
72
-        phoneBoo1.addEntry("nick", "302");
72
+        phoneBoo1.add("nick", "302");
73 73
         assertEquals("302", phoneBoo1.lookUp("nick"));
74 74
     }
75 75
 
@@ -77,7 +77,7 @@ public class PhoneBookTest
77 77
     public void RemoveRecordTest()
78 78
     {
79 79
         PhoneBook phoneBoo2 = new PhoneBook();
80
-        phoneBoo2.addEntry("m", "302");
80
+        phoneBoo2.add("m", "302");
81 81
         phoneBoo2.removeRecord("m");
82 82
         assertEquals("", phoneBoo2.lookUp("m"));
83 83
     }
@@ -86,8 +86,8 @@ public class PhoneBookTest
86 86
     public void removePhoneNumberTest()
87 87
     {
88 88
         PhoneBook phoneBoo1 = new PhoneBook();
89
-        phoneBoo1.addEntry("m", "123");
90
-        phoneBoo1.addNumberTo("m", "456");
89
+        phoneBoo1.add("m", "123");
90
+        phoneBoo1.add("m", "456");
91 91
         phoneBoo1.removePhoneNumber("m", "456");
92 92
         assertEquals("123", phoneBoo1.lookUp("m"));
93 93
     }

+ 23
- 7
package.bluej View File

@@ -1,8 +1,11 @@
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
2
+dependency1.from=PhoneBookTest
3
+dependency1.to=PhoneBook
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=821
6
+editor.fx.0.width=1053
7
+editor.fx.0.x=560
8
+editor.fx.0.y=100
6 9
 objectbench.height=101
7 10
 objectbench.width=461
8 11
 package.divider.horizontal=0.6
@@ -13,8 +16,8 @@ package.editor.x=427
13 16
 package.editor.y=143
14 17
 package.frame.height=600
15 18
 package.frame.width=800
16
-package.numDependencies=0
17
-package.numTargets=1
19
+package.numDependencies=1
20
+package.numTargets=2
18 21
 package.showExtends=true
19 22
 package.showUses=true
20 23
 project.charset=UTF-8
@@ -23,4 +26,17 @@ 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=PhoneBookTest
31
+target1.showInterface=false
32
+target1.type=UnitTestTargetJunit4
33
+target1.width=120
34
+target1.x=100
35
+target1.y=10
36
+target2.height=50
37
+target2.name=PhoneBook
38
+target2.showInterface=false
39
+target2.type=ClassTarget
40
+target2.width=100
41
+target2.x=70
42
+target2.y=70