Chad 6 yıl önce
ebeveyn
işleme
cbda45544f

+ 30
- 6
src/main/java/org/zipcoder/store/ListMap.java Dosyayı Görüntüle

14
 
14
 
15
     @Override
15
     @Override
16
     public boolean isEmptry() {
16
     public boolean isEmptry() {
17
-        return entries.size()==0;
17
+        return size()==0;
18
     }
18
     }
19
 
19
 
20
     @Override
20
     @Override
21
     public Cart get(User key) {
21
     public Cart get(User key) {
22
         // find the cart associate with the key
22
         // find the cart associate with the key
23
+        Cart cart = null;
23
         for(int i=0; i<entries.size(); i++){
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
     @Override
33
     @Override
30
     public void put(User key, Cart value) {
34
     public void put(User key, Cart value) {
31
         // add the key, value entry to the entries list
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
     @Override
42
     @Override
36
     public List<User> getKeys() {
43
     public List<User> getKeys() {
37
         // get all the keys in the map
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
     @Override
53
     @Override
42
     public List<Cart> getValues() {
54
     public List<Cart> getValues() {
43
         // get all the values in the map
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
     @Override
63
     @Override
48
     public Cart remove(User key) {
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 Dosyayı Görüntüle

19
 
19
 
20
     @Override
20
     @Override
21
     public int size() {
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
     @Override
32
     @Override
26
     public boolean isEmptry() {
33
     public boolean isEmptry() {
27
-        return false;
34
+        return size() == 0;
28
     }
35
     }
29
 
36
 
30
     @Override
37
     @Override
31
     public Cart get(User key) {
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
     @Override
51
     @Override
36
     public void put(User key, Cart value) {
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
     @Override
59
     @Override
41
     public List<User> getKeys() {
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
     @Override
70
     @Override
46
     public List<Cart> getValues() {
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
     @Override
81
     @Override
51
     public Cart remove(User key) {
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
     private int bucketIndex(User key){
95
     private int bucketIndex(User key){

+ 34
- 34
src/test/java/org/zipcoder/store/ListMapTest.java Dosyayı Görüntüle

174
         Assert.assertTrue(values.contains(cart2));
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
 //    @Test
178
 //    @Test
179
 //    public void testPutAndGet_WithManyItems(){
179
 //    public void testPutAndGet_WithManyItems(){
180
 //        // Given
180
 //        // Given
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 Dosyayı Görüntüle

174
         Assert.assertTrue(values.contains(cart2));
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
 //    @Test
178
 //    @Test
179
 //    public void testPutAndGet_WithManyItems(){
179
 //    public void testPutAndGet_WithManyItems(){
180
 //        // Given
180
 //        // Given
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
 }