Browse Source

all test pass still might need to look over before I submit a PRclear

Nicholas Maidanos 6 years ago
parent
commit
ffe0d4aa25
2 changed files with 92 additions and 16 deletions
  1. 33
    16
      PhoneBook.java
  2. 59
    0
      PhoneBookTest.java

+ 33
- 16
PhoneBook.java View File

@@ -7,13 +7,13 @@ public class PhoneBook {
7 7
     
8 8
     Map<String, ArrayList<String> > entries = new HashMap<String, ArrayList<String>>();
9 9
     
10
-    public void add (String name, String phoneNumber){
10
+    public void addEntry(String name, String phoneNumber){
11 11
         ArrayList<String> pn = new ArrayList<String>();
12 12
         pn.add(phoneNumber);
13 13
         this.entries.put(name, pn);
14 14
     }
15 15
     
16
-    public void addNumber(String name, String phoneNumber){
16
+    public void addNumberTo(String name, String phoneNumber){
17 17
         for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
18 18
             if(name.equals(entry.getKey())){
19 19
                 entry.getValue().add(phoneNumber);
@@ -22,46 +22,63 @@ public class PhoneBook {
22 22
         }    
23 23
     }
24 24
     
25
-    public void remove(String name, String phoneNumber){
25
+    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
+        }        
26 35
     }
27 36
     
28 37
     public void removeRecord(String name){
29 38
         this.entries.remove(name);
30 39
     }
31 40
     
32
-    
33
-    
34
-    public void lookUp(String name){
35
-        System.out.print(this.entries.get(name));
41
+    public String lookUp(String name){
42
+        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
+                }   
48
+            }
49
+        }
50
+        return sb.toString();        
51
+        
36 52
     }
37 53
     
38 54
     public String reverseLoopup(String phoneNumber){
39
-        String name = "No Records...";
55
+        String name = null;
40 56
         for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
41
-            
42 57
             for(String pn : entry.getValue()){
43 58
                 if(pn.equals(phoneNumber)){
44 59
                     name = entry.getKey();
45 60
                 }
46 61
             }
47
-            
48 62
         }
49 63
         return name;
50
-        
51 64
     }
52 65
     
53 66
     
54
-    public void display() {
67
+    public String display() {
55 68
         StringBuilder sb = new StringBuilder();
56 69
         
57 70
         for(Map.Entry<String, ArrayList<String>> entry :this.entries.entrySet()){
58 71
             sb.append(entry.getKey());
59
-            sb.append(" - ");
60
-            sb.append(entry.getValue());
61
-            sb.append("\n");
72
+            sb.append(": ");
73
+            
74
+            for(String pn : entry.getValue()){
75
+                sb.append(pn);
76
+                //sb.append(" ");
77
+            }
78
+
62 79
         }
63 80
         
64
-        System.out.print(sb.toString());
81
+        return sb.toString();
65 82
     }
66 83
    
67 84
     

+ 59
- 0
PhoneBookTest.java View File

@@ -39,4 +39,63 @@ public class PhoneBookTest
39 39
     public void tearDown()
40 40
     {
41 41
     }
42
+
43
+    @Test
44
+    public void addEntry()
45
+    {
46
+        PhoneBook phoneBoo1 = new PhoneBook();
47
+        phoneBoo1.addEntry("nicholas", "3027387978");
48
+        assertEquals("nicholas: 3027387978", phoneBoo1.display());
49
+    }
50
+
51
+    @Test
52
+    public void addTwoNumberForOnePersonTest()
53
+    {
54
+        PhoneBook phoneBoo1 = new PhoneBook();
55
+        phoneBoo1.addEntry("nick", "302");
56
+        phoneBoo1.addNumberTo("nick", "103");
57
+        assertEquals("nick: 302103", phoneBoo1.display());
58
+    }
59
+
60
+    @Test
61
+    public void reverseLoopupTest()
62
+    {
63
+        PhoneBook phoneBoo1 = new PhoneBook();
64
+        phoneBoo1.addEntry("nick", "302");
65
+        assertEquals("nick", phoneBoo1.reverseLoopup("302"));
66
+    }
67
+
68
+    @Test
69
+    public void lookupTest()
70
+    {
71
+        PhoneBook phoneBoo1 = new PhoneBook();
72
+        phoneBoo1.addEntry("nick", "302");
73
+        assertEquals("302", phoneBoo1.lookUp("nick"));
74
+    }
75
+
76
+    @Test
77
+    public void RemoveRecordTest()
78
+    {
79
+        PhoneBook phoneBoo2 = new PhoneBook();
80
+        phoneBoo2.addEntry("m", "302");
81
+        phoneBoo2.removeRecord("m");
82
+        assertEquals("", phoneBoo2.lookUp("m"));
83
+    }
84
+
85
+    @Test
86
+    public void removePhoneNumberTest()
87
+    {
88
+        PhoneBook phoneBoo1 = new PhoneBook();
89
+        phoneBoo1.addEntry("m", "123");
90
+        phoneBoo1.addNumberTo("m", "456");
91
+        phoneBoo1.removePhoneNumber("m", "456");
92
+        assertEquals("123", phoneBoo1.lookUp("m"));
93
+    }
42 94
 }
95
+
96
+
97
+
98
+
99
+
100
+
101
+