Yesoda Sanka преди 6 години
родител
ревизия
c52be53626
променени са 2 файла, в които са добавени 141 реда и са изтрити 17 реда
  1. 48
    15
      src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java
  2. 93
    2
      src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java

+ 48
- 15
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Целия файл

@@ -10,15 +10,15 @@ import java.util.*;
10 10
 
11 11
 public class PhoneBook {
12 12
 
13
-    private TreeMap<String, String> treemp;
13
+    private TreeMap<String, ArrayList<String>> treemp;
14 14
 
15 15
     public PhoneBook() {
16
-        treemp = new TreeMap<String,String>() ;
17
-        addInitialList();
16
+        treemp = new TreeMap<String, ArrayList<String>>() ;
17
+        //addInitialList();
18 18
     }
19 19
 
20
-    public void setTreemp(String name, String phoneNumber) {
21
-         this.treemp.put(name, phoneNumber);
20
+    public void setTreemp(String name, ArrayList phoneNumbers) {
21
+         this.treemp.put(name, phoneNumbers);
22 22
 
23 23
     }
24 24
 
@@ -31,26 +31,55 @@ public class PhoneBook {
31 31
         return false;
32 32
     }
33 33
 
34
-    public String add(String name, String phoneNumber)
34
+    public void add(String name, String phoneNumber)
35 35
     {
36
-        String str = this.treemp.put(name, phoneNumber);
37
-        return str;
36
+        boolean keyExistsAlready = hashEntry (name);
37
+        //Str str = "";
38
+        //If key already exists then
39
+        ArrayList<String> alOneTwoMany;
40
+        if (keyExistsAlready) {
41
+            // Add new Phonenumber to ArrayList of this Key
42
+             alOneTwoMany = treemp.get(name);
43
+            alOneTwoMany.add(phoneNumber);
44
+            // Add the Arraylist back to the Key
45
+            treemp.put(name, alOneTwoMany);
46
+        } else {
47
+            alOneTwoMany = new ArrayList<String>();
48
+            alOneTwoMany.add(phoneNumber);
49
+            treemp.put(name, alOneTwoMany);
50
+        }
51
+    }
52
+
53
+    public int get (String key) {
54
+
55
+        ArrayList al = treemp.get(key);
56
+        return al.size();
57
+
38 58
     }
39 59
 
40 60
 
41
-    public String lmeookup(String name) {
61
+    /*public String lmeookup(String name) {
42 62
 
43 63
         String temp = treemp.get(name);
44 64
         return temp;
45
-    }
65
+    }*/
66
+
67
+    public void remove(String key, String number) {
46 68
 
47
-    public String hasRemove(String name) {
69
+        ArrayList alForRemove = treemp.get(key);
48 70
 
49
-        return name;
71
+        /* For Loop for iterating ArrayList */
72
+        for (int counter = 0; counter < alForRemove.size(); counter++) {
73
+            if (alForRemove.get(counter).toString().equalsIgnoreCase(number)){
74
+                alForRemove.remove(counter);
75
+            }
76
+        }
77
+        // add the updated Arraylist back to Treemap
78
+        treemp.put(key, alForRemove);
50 79
     }
51 80
 
52 81
 
53
-    public void addInitialList () {
82
+    /* public void addInitialList () {
54 83
 
55 84
         this.setTreemp("Dog", "222-333-444");
56 85
         this.setTreemp("Yashoda","111-222-333");
@@ -83,9 +112,13 @@ public class PhoneBook {
83 112
         return sb.toString();
84 113
 
85 114
     }
115
+*/
116
+    public void removeRecord (String key) {
117
+        treemp.remove(key);
118
+    }
86 119
 
87
-    public void clearSortedMap () {
88
-        treemp.clear();
120
+    public int getNoOfRecords (){
121
+        return treemp.size();
89 122
     }
90 123
 
91 124
 

+ 93
- 2
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Целия файл

@@ -4,14 +4,16 @@ package com.zipcodewilmington.phonebook;
4 4
 import org.junit.Assert;
5 5
 import org.junit.Test;
6 6
 
7
-    /**
7
+import java.util.ArrayList;
8
+
9
+/**
8 10
      * Created by leon on 1/23/18.
9 11
      */
10 12
     public class PhoneBookTest {
11 13
 
12 14
         private static final int Expected = 0;
13 15
 
14
-        @Test
16
+       /*** @Test
15 17
         public void testHasEntry(){
16 18
             //Given
17 19
             PhoneBook book = new PhoneBook();
@@ -100,6 +102,95 @@ import org.junit.Test;
100 102
             String expected = "Dog 222-444-4444\nZebra 111-222-333\n";
101 103
             String actualOutput= book.listNamesAndNumbers();
102 104
             Assert.assertEquals(expected, actualOutput);
105
+        } **/
106
+
107
+       @Test
108
+       //    public void add(String name, String phoneNumber)
109
+        public void testadd () {
110
+
111
+           PhoneBook pb = new PhoneBook();
112
+           ArrayList <String> al1 = new ArrayList<String>();
113
+           al1.add("111-111-111");
114
+           al1.add("222-222-222");
115
+           al1.add("333-333-333");
116
+           pb.setTreemp("John", al1);
117
+           ArrayList <String> al2 = new ArrayList<String>();
118
+           al2.add("444-444-444");
119
+           al2.add("222-222-222");
120
+           al2.add("333-333-333");
121
+           pb.setTreemp("Mike", al2);
122
+           ArrayList <String> al3 = new ArrayList<String>();
123
+           al3.add("111-111-111");
124
+           al3.add("555-555-555");
125
+           al3.add("333-333-333");
126
+           pb.setTreemp("Keenan", al3);
127
+
128
+           pb.add("Mike", "666-666-6666");
129
+
130
+           int expectedPhoneNumbersCount = 4;
131
+
132
+           int actual = pb.get("Mike");
133
+
134
+           Assert.assertEquals(expectedPhoneNumbersCount, actual);
135
+
136
+       }
137
+        @Test
138
+       public void testRemoveRecord () {
139
+           PhoneBook pb = new PhoneBook();
140
+           ArrayList <String> al1 = new ArrayList<String>();
141
+           al1.add("111-111-111");
142
+           al1.add("222-222-222");
143
+           al1.add("333-333-333");
144
+           pb.setTreemp("John", al1);
145
+           ArrayList <String> al2 = new ArrayList<String>();
146
+           al2.add("444-444-444");
147
+           al2.add("222-222-222");
148
+           al2.add("333-333-333");
149
+           pb.setTreemp("Mike", al2);
150
+           ArrayList <String> al3 = new ArrayList<String>();
151
+           al3.add("111-111-111");
152
+           al3.add("555-555-555");
153
+           al3.add("333-333-333");
154
+           pb.setTreemp("Keenan", al3);
155
+
156
+           int before = pb.getNoOfRecords();
157
+           pb.removeRecord("Mike");
158
+           int actual = pb.getNoOfRecords();
159
+           int expectedPhoneNumbersCount = 2;
160
+           Assert.assertEquals(expectedPhoneNumbersCount, actual);
161
+
162
+
163
+       }
164
+
165
+       @Test
166
+        public void testRemovePhoneNumber ( ) {
167
+
168
+            PhoneBook pb = new PhoneBook();
169
+            ArrayList <String> al1 = new ArrayList<String>();
170
+            al1.add("111-111-111");
171
+            al1.add("222-222-222");
172
+            al1.add("333-333-333");
173
+            pb.setTreemp("John", al1);
174
+            ArrayList <String> al2 = new ArrayList<String>();
175
+            al2.add("444-444-444");
176
+            al2.add("222-222-222");
177
+            al2.add("333-333-333");
178
+            pb.setTreemp("Mike", al2);
179
+            ArrayList <String> al3 = new ArrayList<String>();
180
+            al3.add("111-111-111");
181
+            al3.add("555-555-555");
182
+            al3.add("333-333-333");
183
+            pb.setTreemp("Keenan", al3);
184
+
185
+            pb.remove("Mike", "222-222-222");
186
+
187
+            int expectedPhoneNumbersCount = 2;
188
+
189
+            int actual = pb.get("Mike");
190
+
191
+            Assert.assertEquals(expectedPhoneNumbersCount, actual);
192
+
193
+
103 194
         }
104 195
 
105 196
     }