Ben Blinebury il y a 6 ans
Parent
révision
025dbfe126

+ 17
- 0
src/main/java/org/zipcoder/store/Cart.java Voir le fichier

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

+ 36
- 5
src/main/java/org/zipcoder/store/ListMap.java Voir le fichier

@@ -9,40 +9,71 @@ public class ListMap implements MyMap {
9 9
 
10 10
     @Override
11 11
     public int size() {
12
-        return -1;
12
+
13
+        return entries.size();
13 14
     }
14 15
 
15 16
     @Override
16 17
     public boolean isEmptry() {
17
-        return false;
18
+
19
+        return entries.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
+        for(Entry e: entries){
26
+            if(e.getKey().equals(key)){
27
+                return e.getValue();
28
+            }
29
+        }
23 30
         return null;
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
+        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 47
 
32 48
     @Override
33 49
     public List<User> getKeys() {
34 50
         // get all the keys in the map
35
-        return null;
51
+        List<User> users = new ArrayList<User>();
52
+        for(Entry e: entries) {
53
+            users.add(e.getKey());
54
+        }
55
+        return users;
36 56
     }
37 57
 
38 58
     @Override
39 59
     public List<Cart> getValues() {
40 60
         // get all the values in the map
41
-        return null;
61
+        List<Cart> carts = new ArrayList<Cart>();
62
+        for(Entry e: entries){
63
+            carts.add(e.getValue());
64
+        }
65
+        return carts;
42 66
     }
43 67
 
44 68
     @Override
45 69
     public Cart remove(User key) {
46
-        return null;
70
+        Cart gone = null;
71
+        for(Entry e: entries){
72
+            if(e.getKey().equals(key)){
73
+                gone = e.getValue();
74
+                entries.remove(e);
75
+            }
76
+        }
77
+        return gone;
47 78
     }
48 79
 }

+ 57
- 7
src/main/java/org/zipcoder/store/MyHashMap.java Voir le fichier

@@ -19,40 +19,90 @@ public class MyHashMap implements MyMap{
19 19
 
20 20
     @Override
21 21
     public int size() {
22
-        return -1;
22
+        int total = 0;
23
+        for(List<Entry> e: entries){
24
+            total += e.size();
25
+        }
26
+        return total;
23 27
     }
24 28
 
25 29
     @Override
26 30
     public boolean isEmptry() {
27
-        return false;
31
+        if (size() == 0) {
32
+            return true;
33
+        } else {
34
+            return false;
35
+        }
28 36
     }
29 37
 
30 38
     @Override
31 39
     public Cart get(User key) {
32
-        return null;
40
+        Cart cart = null;
41
+        List<Entry> entryList = entries[bucketIndex(key)];
42
+        for (Entry e : entryList) {
43
+            if (e.getKey().equals(key)) {
44
+                cart = e.getValue();
45
+                }
46
+        }
47
+        return cart;
48
+
33 49
     }
34 50
 
35 51
     @Override
36 52
     public void put(User key, Cart value) {
37
-
53
+        entries[bucketIndex(key)].add(new Entry(key,value));
38 54
     }
39 55
 
40 56
     @Override
41 57
     public List<User> getKeys() {
42
-        return null;
58
+        List<User> users = new ArrayList<User>();
59
+        for(List<Entry> entryList: entries){
60
+            for(Entry entry: entryList){
61
+                users.add(entry.getKey());
62
+            }
63
+        }
64
+        return users;
43 65
     }
44 66
 
45 67
     @Override
46 68
     public List<Cart> getValues() {
47
-        return null;
69
+        List<Cart> carts = new ArrayList<Cart>();
70
+        for (List<Entry> entryList : entries) {
71
+            for (Entry entry : entryList) {
72
+                carts.add(entry.getValue());
73
+            }
74
+        }
75
+        return carts;
48 76
     }
49 77
 
50 78
     @Override
51 79
     public Cart remove(User key) {
52
-        return null;
80
+        List<Entry> bin = entries[bucketIndex(key)];
81
+        Iterator<Entry> iterator = bin.iterator();
82
+
83
+        Cart removedCart = null;
84
+
85
+        while(iterator.hasNext()) {
86
+            Entry entry = iterator.next();
87
+            if (entry.getKey().equals(key)) {
88
+                removedCart = entry.getValue();
89
+                iterator.remove();
90
+            }
91
+        }
92
+        return removedCart;
53 93
     }
54 94
 
55 95
     private int bucketIndex(User key){
56 96
         return Math.abs(key.hashCode()) % entries.length;
57 97
     }
98
+
99
+        private int keyIndex(List<Entry> bin, User key) {
100
+        int index = -1;
101
+        for (int i = 0; i < bin.size(); i++) {
102
+            if (bin.get(i).getKey().equals(key)) {
103
+                index = i;
104
+            }
105
+        }
106
+        return index;
107
+    }
58 108
 }

+ 15
- 0
src/main/java/org/zipcoder/store/User.java Voir le fichier

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

+ 51
- 51
src/test/java/org/zipcoder/store/ListMapTest.java Voir le fichier

@@ -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 Voir le fichier

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