Trinh Tong преди 6 години
родител
ревизия
fa3bb796ee
променени са 2 файла, в които са добавени 94 реда и са изтрити 61 реда
  1. 40
    7
      src/main/java/org/zipcoder/store/ListMap.java
  2. 54
    54
      src/test/java/org/zipcoder/store/ListMapTest.java

+ 40
- 7
src/main/java/org/zipcoder/store/ListMap.java Целия файл

@@ -1,7 +1,9 @@
1 1
 package org.zipcoder.store;
2 2
 
3 3
 import java.util.ArrayList;
4
+import java.util.HashSet;
4 5
 import java.util.List;
6
+import java.util.Set;
5 7
 
6 8
 public class ListMap implements MyMap {
7 9
     // instance field that is intialize to an ArrayList
@@ -9,40 +11,71 @@ public class ListMap implements MyMap {
9 11
 
10 12
     @Override
11 13
     public int size() {
12
-        return -1;
14
+        return entries.size();
13 15
     }
14 16
 
15 17
     @Override
16 18
     public boolean isEmptry() {
17
-        return false;
19
+        return (this.size() == 0);
18 20
     }
19 21
 
20 22
     @Override
21 23
     public Cart get(User key) {
22 24
         // find the cart associate with the key
25
+
26
+            for (Entry e: entries) {
27
+                if (e.getKey().equals(key)) {
28
+                    return e.getValue();
29
+                }
30
+            }
23 31
         return null;
24 32
     }
25 33
 
26 34
     @Override
27 35
     public void put(User key, Cart value) {
28
-        // add the key, value entry to the entries list
36
+        if (get(key) == null) {
37
+            entries.add(new Entry(key, value));
38
+        } else {
39
+            for (Entry e: entries) {
40
+                if (e.getKey().equals(key)) {
41
+                    e.setValue(value);
42
+                }
43
+            }
44
+        }
29 45
     }
30 46
 
31
-
32 47
     @Override
33 48
     public List<User> getKeys() {
34 49
         // get all the keys in the map
35
-        return null;
50
+
51
+        List<User> idSet = new ArrayList<>();
52
+
53
+        for (Entry e:entries) {
54
+            idSet.add(e.getKey());
55
+        }
56
+        return idSet;
36 57
     }
37 58
 
38 59
     @Override
39 60
     public List<Cart> getValues() {
40
-        // get all the values in the map
41
-        return null;
61
+        List<Cart> valueSet = new ArrayList<>();
62
+        for (Entry e: entries) {
63
+            valueSet.add(e.getValue());
64
+        }
65
+        return valueSet;
42 66
     }
43 67
 
44 68
     @Override
45 69
     public Cart remove(User key) {
70
+        Entry eTemp = null;
71
+
72
+        for (Entry e: entries) {
73
+            if (e.getKey().equals(key)) {
74
+                eTemp = e;
75
+                entries.remove(e);
76
+                return eTemp.getValue();
77
+            }
78
+        }
46 79
         return null;
47 80
     }
48 81
 }

+ 54
- 54
src/test/java/org/zipcoder/store/ListMapTest.java Целия файл

@@ -174,58 +174,58 @@ public class ListMapTest {
174 174
         Assert.assertTrue(values.contains(cart2));
175 175
     }
176 176
 
177
-//    I commented this out because it takes a long time to run. Uncomment it when implement your put method, uncomment this test
178
-//    @Test
179
-//    public void testPutAndGet_WithManyItems(){
180
-//        // Given
181
-//        int numberOfEntries = 70000;
182
-//
183
-//        for (int i = 0; i < numberOfEntries; i++) {
184
-//            myMap.put(new User(i), new Cart(i));
185
-//        }
186
-//
187
-//
188
-//        for (int i = 0; i < numberOfEntries; i++) {
189
-//            // When
190
-//            Cart actualCart = myMap.get(new User(i));
191
-//            Cart expectedCart = new Cart(i);
192
-//
193
-//            // Then
194
-//            Assert.assertEquals(actualCart, expectedCart);
195
-//        }
196
-//    }
197
-
198
-//    BONUS
199
-//    @Test
200
-//    public void testRemove(){
201
-//        // Given
202
-//        User user1 = new User("Lena");
203
-//        Cart cart1 = new Cart(11);
204
-//
205
-//        User user2 = new User("Jamal");
206
-//        Cart cart2 = new Cart(21);
207
-//
208
-//        User user3 = new User("Kelvin");
209
-//        Cart cart3 = new Cart(31);
210
-//
211
-//        // When
212
-//        myMap.put(user1, cart1);
213
-//        myMap.put(user2, cart2);
214
-//        myMap.put(user3, cart3);
215
-//
216
-//        // Then
217
-//        Cart actualCart = myMap.remove(user2);
218
-//
219
-//        Assert.assertEquals(cart2, actualCart);
220
-//        Assert.assertNull(myMap.get(user2));
221
-//    }
222
-//
223
-//    @Test
224
-//    public void testRemove_CartNotInTheMap(){
225
-//        // Given
226
-//        User user1 = new User("Lena");
227
-//
228
-//        // When & Then
229
-//        Assert.assertNull(myMap.remove(user1));
230
-//    }
177
+    //I commented this out because it takes a long time to run. Uncomment it when implement your put method, uncomment this test
178
+    @Test
179
+    public void testPutAndGet_WithManyItems(){
180
+        // Given
181
+        int numberOfEntries = 70000;
182
+
183
+        for (int i = 0; i < numberOfEntries; i++) {
184
+            myMap.put(new User(i), new Cart(i));
185
+        }
186
+
187
+
188
+        for (int i = 0; i < numberOfEntries; i++) {
189
+            // When
190
+            Cart actualCart = myMap.get(new User(i));
191
+            Cart expectedCart = new Cart(i);
192
+
193
+            // Then
194
+            Assert.assertEquals(actualCart, expectedCart);
195
+        }
196
+    }
197
+
198
+    //BONUS
199
+    @Test
200
+    public void testRemove(){
201
+        // Given
202
+        User user1 = new User("Lena");
203
+        Cart cart1 = new Cart(11);
204
+
205
+        User user2 = new User("Jamal");
206
+        Cart cart2 = new Cart(21);
207
+
208
+        User user3 = new User("Kelvin");
209
+        Cart cart3 = new Cart(31);
210
+
211
+        // When
212
+        myMap.put(user1, cart1);
213
+        myMap.put(user2, cart2);
214
+        myMap.put(user3, cart3);
215
+
216
+        // Then
217
+        Cart actualCart = myMap.remove(user2);
218
+
219
+        Assert.assertEquals(cart2, actualCart);
220
+        Assert.assertNull(myMap.get(user2));
221
+    }
222
+
223
+    @Test
224
+    public void testRemove_CartNotInTheMap(){
225
+        // Given
226
+        User user1 = new User("Lena");
227
+
228
+        // When & Then
229
+        Assert.assertNull(myMap.remove(user1));
230
+    }
231 231
 }