Rachelle 6 anos atrás
pai
commit
1d79ffcdc3

BIN
.DS_Store Ver arquivo


+ 14
- 0
src/main/java/org/zipcoder/store/Cart.java Ver arquivo

@@ -12,4 +12,18 @@ public class Cart {
12 12
     public long getId(){
13 13
         return this.id;
14 14
     }
15
+
16
+    @Override
17
+    public boolean equals(Object o) {
18
+        if (this == o) return true;
19
+        if (o == null || getClass() != o.getClass()) return false;
20
+        Cart cart = (Cart) o;
21
+        return id == cart.id;
22
+    }
23
+
24
+    @Override
25
+    public int hashCode() {
26
+
27
+        return Objects.hash(id);
28
+    }
15 29
 }

+ 29
- 6
src/main/java/org/zipcoder/store/ListMap.java Ver arquivo

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

+ 38
- 7
src/main/java/org/zipcoder/store/MyHashMap.java Ver arquivo

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

+ 5
- 0
src/main/java/org/zipcoder/store/User.java Ver arquivo

@@ -37,4 +37,9 @@ public class User {
37 37
     public void setName(String name) {
38 38
         this.name = name;
39 39
     }
40
+
41
+    public boolean equals(User user){return(Objects.equals(name, user.name)&&id == user.id); }
42
+
43
+    @Override
44
+    public int hashCode(){return Objects.hash(id, name);}
40 45
 }

+ 51
- 51
src/test/java/org/zipcoder/store/ListMapTest.java Ver arquivo

@@ -175,57 +175,57 @@ public class ListMapTest {
175 175
     }
176 176
 
177 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
-//    }
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 197
 
198 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
-//    }
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
 }

+ 53
- 53
src/test/java/org/zipcoder/store/MyHashMapTest.java Ver arquivo

@@ -174,57 +174,57 @@ 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
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
-//    @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
-//    }
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
+    @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
 }