nafis nibir 6 years ago
parent
commit
a82e66c279

+ 30
- 5
src/main/java/org/zipcoder/store/ListMap.java View File

@@ -9,40 +9,65 @@ 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
+        if(this.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 result = null;
25
+        for (Entry e: entries){
26
+            if(e.getKey().equals(key)){
27
+                result = e.getValue();
28
+            }
29
+        }
30
+        return result;
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
+        Entry thing = new Entry(key, value);
37
+        entries.add(thing);
29 38
     }
30 39
 
31 40
 
32 41
     @Override
33 42
     public List<User> getKeys() {
34 43
         // get all the keys in the map
35
-        return null;
44
+        List<User> keys = new ArrayList<>();
45
+        for (Entry e: entries){
46
+            keys.add(e.getKey());
47
+        }
48
+        return keys;
36 49
     }
37 50
 
38 51
     @Override
39 52
     public List<Cart> getValues() {
40 53
         // get all the values in the map
41
-        return null;
54
+        List<Cart> values = new ArrayList<>();
55
+        for (Entry e: entries){
56
+            values.add(e.getValue());
57
+        }
58
+        return values;
42 59
     }
43 60
 
44 61
     @Override
45 62
     public Cart remove(User key) {
46
-        return null;
63
+        Cart removed = null;
64
+        for (Entry e: entries){
65
+            if (e.getKey().equals(key)){
66
+                entries.remove(e);
67
+                removed = e.getValue();
68
+                break;
69
+            }
70
+        }
71
+        return removed;
47 72
     }
48 73
 }

+ 42
- 6
src/main/java/org/zipcoder/store/MyHashMap.java View File

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

+ 30
- 15
src/main/java/org/zipcoder/store/User.java View File

@@ -38,20 +38,35 @@ public class User {
38 38
         this.name = name;
39 39
     }
40 40
 
41
-    public boolean equals(User user){
42
-        if(user.getName() == null){
43
-            if (user.getId() == this.getId()){
44
-                return true;
45
-            }
46
-            return false;
47
-        }
48
-        if (user.getId() == this.getId() && user.getName().equals(this.getName())){
49
-            return true;
50
-        }
51
-        return false;
52
-    }
53
-
54
-    public int hashCode(){
55
-        return Objects.hash(this.getId(), this.getName());
41
+    @Override
42
+    public int hashCode() {
43
+
44
+        return Objects.hash(id, name);
45
+    }
46
+
47
+    @Override
48
+    public boolean equals(Object o) {
49
+        if (this == o) return true;
50
+        if (o == null || getClass() != o.getClass()) return false;
51
+        User user = (User) o;
52
+        return id == user.id &&
53
+                Objects.equals(name, user.name);
56 54
     }
55
+
56
+//    public boolean equals(User user){
57
+//        if(user.getName() == null){
58
+//            if (user.getId() == this.getId()){
59
+//                return true;
60
+//            }
61
+//            return false;
62
+//        }
63
+//        if (user.getId() == this.getId() && user.getName().equals(this.getName())){
64
+//            return true;
65
+//        }
66
+//        return false;
67
+//    }
68
+
69
+//    public int hashCode(){
70
+//        return Objects.hash(this.getId(), this.getName());
71
+//    }
57 72
 }

+ 51
- 51
src/test/java/org/zipcoder/store/ListMapTest.java View File

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

+ 52
- 52
src/test/java/org/zipcoder/store/MyHashMapTest.java View File

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