Chad před 6 roky
rodič
revize
cbda45544f

+ 30
- 6
src/main/java/org/zipcoder/store/ListMap.java Zobrazit soubor

@@ -14,38 +14,62 @@ public class ListMap implements MyMap {
14 14
 
15 15
     @Override
16 16
     public boolean isEmptry() {
17
-        return entries.size()==0;
17
+        return size()==0;
18 18
     }
19 19
 
20 20
     @Override
21 21
     public Cart get(User key) {
22 22
         // find the cart associate with the key
23
+        Cart cart = null;
23 24
         for(int i=0; i<entries.size(); i++){
24
-
25
+        if(entries.get(i).getKey().equals(key)){
26
+            cart = entries.get(i).getValue();
27
+        }
25 28
         }
26
-        return null;
29
+
30
+        return cart;
27 31
     }
28 32
 
29 33
     @Override
30 34
     public void put(User key, Cart value) {
31 35
         // add the key, value entry to the entries list
36
+        Entry entry= new Entry(key, value);
37
+        remove(key);
38
+        this.entries.add(entry);
32 39
     }
33 40
 
34 41
 
35 42
     @Override
36 43
     public List<User> getKeys() {
37 44
         // get all the keys in the map
38
-        return null;
45
+        List<User> keys = new ArrayList<>();
46
+        for (Entry entry : entries) {
47
+            keys.add(entry.getKey());
48
+        }
49
+
50
+        return keys;
39 51
     }
40 52
 
41 53
     @Override
42 54
     public List<Cart> getValues() {
43 55
         // get all the values in the map
44
-        return null;
56
+        List<Cart> values = new ArrayList<>();
57
+        for (Entry entry : this.entries) {
58
+            values.add(entry.getValue());
59
+        }
60
+        return values;
45 61
     }
46 62
 
47 63
     @Override
48 64
     public Cart remove(User key) {
49
-        return null;
65
+        Cart cart = get(key);
66
+
67
+        for(int i=0; i<entries.size(); i++){
68
+            if(entries.get(i).getKey().equals(key)){
69
+                entries.remove(i);
70
+            }
71
+        }
72
+
73
+        return cart;
50 74
     }
51 75
 }

+ 46
- 6
src/main/java/org/zipcoder/store/MyHashMap.java Zobrazit soubor

@@ -19,37 +19,77 @@ public class MyHashMap implements MyMap{
19 19
 
20 20
     @Override
21 21
     public int size() {
22
-        return -1;
22
+        int count= 0;
23
+        for(int i=0; i<BUCKET_SIZE;i++){
24
+            if(entries[i].size()>0){
25
+                count++;
26
+            }
27
+        }
28
+
29
+        return count;
23 30
     }
24 31
 
25 32
     @Override
26 33
     public boolean isEmptry() {
27
-        return false;
34
+        return size() == 0;
28 35
     }
29 36
 
30 37
     @Override
31 38
     public Cart get(User key) {
32
-        return null;
39
+        Cart cart = null;
40
+
41
+        for (int j = 0; j < entries[bucketIndex(key)].size(); j++) {
42
+            if (entries[bucketIndex(key)].get(j).getKey().equals(key)) {
43
+                    cart = entries[bucketIndex(key)].get(j).getValue();
44
+            }
45
+         }
46
+
47
+
48
+        return cart;
33 49
     }
34 50
 
35 51
     @Override
36 52
     public void put(User key, Cart value) {
53
+        Entry entry= new Entry(key, value);
54
+
55
+         entries[bucketIndex(key)].add(entry);
37 56
 
38 57
     }
39 58
 
40 59
     @Override
41 60
     public List<User> getKeys() {
42
-        return null;
61
+        List<User> keys = new ArrayList<>();
62
+         for(List<Entry> list: entries)
63
+            for (Entry entry : list) {
64
+                keys.add(entry.getKey());
65
+            }
66
+
67
+        return keys;
43 68
     }
44 69
 
45 70
     @Override
46 71
     public List<Cart> getValues() {
47
-        return null;
72
+        List<Cart> values = new ArrayList<>();
73
+        for(List<Entry> list: entries)
74
+            for (Entry entry : list) {
75
+                values.add(entry.getValue());
76
+            }
77
+
78
+        return values;
48 79
     }
49 80
 
50 81
     @Override
51 82
     public Cart remove(User key) {
52
-        return null;
83
+        Cart cart = get(key);
84
+
85
+        for (int j = 0; j < entries[bucketIndex(key)].size(); j++) {
86
+            if (entries[bucketIndex(key)].get(j).getKey().equals(key)) {
87
+                entries[bucketIndex(key)].remove(j);
88
+            }
89
+        }
90
+
91
+
92
+        return cart;
53 93
     }
54 94
 
55 95
     private int bucketIndex(User key){

+ 34
- 34
src/test/java/org/zipcoder/store/ListMapTest.java Zobrazit soubor

@@ -174,7 +174,7 @@ 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
177
+    //I commented this out because it takes a long time to run. Uncomment it when implement your put method, uncomment this test
178 178
 //    @Test
179 179
 //    public void testPutAndGet_WithManyItems(){
180 180
 //        // Given
@@ -195,37 +195,37 @@ public class ListMapTest {
195 195
 //        }
196 196
 //    }
197 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
-//    }
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
 }

+ 33
- 33
src/test/java/org/zipcoder/store/MyHashMapTest.java Zobrazit soubor

@@ -174,7 +174,7 @@ public class MyHashMapTest {
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
177
+//    //I commented this out because it takes a long time to run. Uncomment it when implement your put method, uncomment this test
178 178
 //    @Test
179 179
 //    public void testPutAndGet_WithManyItems(){
180 180
 //        // Given
@@ -195,36 +195,36 @@ public class MyHashMapTest {
195 195
 //        }
196 196
 //    }
197 197
 
198
-//    @Test
199
-//    public void testRemove(){
200
-//        // Given
201
-//        User user1 = new User("Lena");
202
-//        Cart cart1 = new Cart(11);
203
-//
204
-//        User user2 = new User("Jamal");
205
-//        Cart cart2 = new Cart(21);
206
-//
207
-//        User user3 = new User("Kelvin");
208
-//        Cart cart3 = new Cart(31);
209
-//
210
-//        // When
211
-//        myMap.put(user1, cart1);
212
-//        myMap.put(user2, cart2);
213
-//        myMap.put(user3, cart3);
214
-//
215
-//        // Then
216
-//        Cart actualCart = myMap.remove(user2);
217
-//
218
-//        Assert.assertEquals(cart2, actualCart);
219
-//        Assert.assertNull(myMap.get(user2));
220
-//    }
221
-//
222
-//    @Test
223
-//    public void testRemove_CartNotInTheMap(){
224
-//        // Given
225
-//        User user1 = new User("Lena");
226
-//
227
-//        // When & Then
228
-//        Assert.assertNull(myMap.remove(user1));
229
-//    }
198
+    @Test
199
+    public void testRemove(){
200
+        // Given
201
+        User user1 = new User("Lena");
202
+        Cart cart1 = new Cart(11);
203
+
204
+        User user2 = new User("Jamal");
205
+        Cart cart2 = new Cart(21);
206
+
207
+        User user3 = new User("Kelvin");
208
+        Cart cart3 = new Cart(31);
209
+
210
+        // When
211
+        myMap.put(user1, cart1);
212
+        myMap.put(user2, cart2);
213
+        myMap.put(user3, cart3);
214
+
215
+        // Then
216
+        Cart actualCart = myMap.remove(user2);
217
+
218
+        Assert.assertEquals(cart2, actualCart);
219
+        Assert.assertNull(myMap.get(user2));
220
+    }
221
+
222
+    @Test
223
+    public void testRemove_CartNotInTheMap(){
224
+        // Given
225
+        User user1 = new User("Lena");
226
+
227
+        // When & Then
228
+        Assert.assertNull(myMap.remove(user1));
229
+    }
230 230
 }