Demetrius Murray hace 6 años
padre
commit
c8e5add0bb

+ 13
- 0
src/main/java/org/zipcoder/store/Cart.java Ver fichero

@@ -12,4 +12,17 @@ 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
+        return Objects.hash(id);
27
+    }
15 28
 }

+ 27
- 8
src/main/java/org/zipcoder/store/ListMap.java Ver fichero

@@ -9,40 +9,59 @@ public class ListMap implements MyMap {
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
-        return false;
17
+        if (size() > 0) return false;
18
+        return true;
18 19
     }
19 20
 
20 21
     @Override
21 22
     public Cart get(User key) {
22
-        // find the cart associate with the key
23
+        for (Entry e : entries) {
24
+            if (e.getKey().equals(key)) return e.getValue();
25
+        }
23 26
         return null;
24 27
     }
25 28
 
26 29
     @Override
27 30
     public void put(User key, Cart value) {
28
-        // add the key, value entry to the entries list
31
+        boolean contains = false;
32
+        for (Entry e : entries)
33
+            if (e.getKey().equals(key)) {
34
+                e.setValue(value);
35
+                contains = true;
36
+                break;
37
+            }
38
+        if (contains == false) entries.add(new Entry(key, value));
29 39
     }
30 40
 
31 41
 
32 42
     @Override
33 43
     public List<User> getKeys() {
34
-        // get all the keys in the map
35
-        return null;
44
+        List<User> list = new ArrayList<>();
45
+        entries.forEach(e ->list.add(e.getKey()));
46
+        return list;
36 47
     }
37 48
 
38 49
     @Override
39 50
     public List<Cart> getValues() {
40
-        // get all the values in the map
41
-        return null;
51
+        List<Cart> list = new ArrayList<>();
52
+        entries.forEach(e ->list.add(e.getValue()));
53
+        return list;
42 54
     }
43 55
 
44 56
     @Override
45 57
     public Cart remove(User key) {
58
+        //remove and return cart
59
+        for (Entry e : entries)
60
+            if (e.getKey().equals(key)){
61
+                Cart c = e.getValue();
62
+                e.setValue(null);
63
+                return c;
64
+            }
46 65
         return null;
47 66
     }
48 67
 }

+ 33
- 4
src/main/java/org/zipcoder/store/MyHashMap.java Ver fichero

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

+ 1
- 0
src/main/java/org/zipcoder/store/MyMap.java Ver fichero

@@ -12,6 +12,7 @@ public interface MyMap {
12 12
     /**
13 13
      * @return true if the map has one or more item in it. False otherwise.
14 14
      */
15
+    //other way around?
15 16
     public boolean isEmptry();
16 17
 
17 18
     /**

+ 13
- 0
src/main/java/org/zipcoder/store/User.java Ver fichero

@@ -37,4 +37,17 @@ public class User {
37 37
     public void setName(String name) {
38 38
         this.name = name;
39 39
     }
40
+
41
+    public Boolean equals(User u){
42
+        if (this == u) return true;
43
+        if (u == null || getClass() != u.getClass()) return false;
44
+        if (u.name == null && this.name == null) {
45
+            if (u.id == this.id) return true;
46
+        } else if (u.id == this.id && u.name.equals(this.name)) return true;
47
+        return false;
48
+    }
49
+
50
+    public int hashCode(){
51
+        return Objects.hash(id,name);
52
+    }
40 53
 }

+ 52
- 52
src/test/java/org/zipcoder/store/ListMapTest.java Ver fichero

@@ -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
-//    }
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 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
 }

+ 52
- 52
src/test/java/org/zipcoder/store/MyHashMapTest.java Ver fichero

@@ -175,56 +175,56 @@ public class MyHashMapTest {
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
-//    }
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
-//    }
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
 }