Bladeren bron

got it, neat

NedRedmond 6 jaren geleden
bovenliggende
commit
fd2c076fec

+ 13
- 0
src/main/java/org/zipcoder/store/Cart.java Bestand weergeven

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

+ 34
- 5
src/main/java/org/zipcoder/store/ListMap.java Bestand weergeven

@@ -1,7 +1,9 @@
1 1
 package org.zipcoder.store;
2 2
 
3 3
 import java.util.ArrayList;
4
+import java.util.Iterator;
4 5
 import java.util.List;
6
+import java.util.Map;
5 7
 
6 8
 public class ListMap implements MyMap {
7 9
     // instance field that is intialize to an ArrayList
@@ -9,40 +11,67 @@ public class ListMap implements MyMap {
9 11
 
10 12
     @Override
11 13
     public int size() {
12
-        return -1;
14
+        return entries.size();
13 15
     }
14 16
 
15 17
     @Override
16 18
     public boolean isEmptry() {
17
-        return false;
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 entry : entries)
26
+        { if (entry.getKey().equals(key)) return entry.getValue(); }
23 27
         return null;
24 28
     }
25 29
 
26 30
     @Override
27 31
     public void put(User key, Cart value) {
28 32
         // add the key, value entry to the entries list
33
+        for (Entry entry : entries) {
34
+            if (entry.getKey().equals(key)) {
35
+                entry.setValue(value);
36
+                return;
37
+            }
38
+        }
39
+        entries.add(new Entry(key, value));
29 40
     }
30 41
 
31 42
 
32 43
     @Override
33 44
     public List<User> getKeys() {
34 45
         // get all the keys in the map
35
-        return null;
46
+        List<User> users = new ArrayList<>();
47
+        for (Entry entry : entries) {
48
+            users.add(entry.getKey());
49
+        }
50
+        return users;
36 51
     }
37 52
 
38 53
     @Override
39 54
     public List<Cart> getValues() {
40 55
         // get all the values in the map
41
-        return null;
56
+        List<Cart> carts = new ArrayList<>();
57
+        for (Entry entry : entries) {
58
+            carts.add(entry.getValue());
59
+        }
60
+        return carts;
42 61
     }
43 62
 
44 63
     @Override
45 64
     public Cart remove(User key) {
46
-        return null;
65
+        Cart cart = null;
66
+
67
+        for (Iterator<Entry> i = entries.iterator(); i.hasNext();) {
68
+            Entry entry = i.next();
69
+            if (entry.getKey().equals(key)) {
70
+                cart = entry.getValue();
71
+                i.remove();
72
+            }
73
+        }
74
+
75
+        return cart;
47 76
     }
48 77
 }

+ 50
- 7
src/main/java/org/zipcoder/store/MyHashMap.java Bestand weergeven

@@ -4,7 +4,7 @@ import java.util.*;
4 4
 
5 5
 public class MyHashMap implements MyMap{
6 6
     // final field that doesn't change for any object
7
-    private static final int BUCKET_SIZE = 15;
7
+    private static final int BUCKET_SIZE = 100;
8 8
 
9 9
     // instance field that can only be set once. It is different for every Object
10 10
     private final List<Entry>[] entries;
@@ -17,39 +17,82 @@ public class MyHashMap implements MyMap{
17 17
         }
18 18
     }
19 19
 
20
+    int sizeCounter = 0;
21
+
20 22
     @Override
21 23
     public int size() {
22
-        return -1;
24
+        for (int i = 0; i < BUCKET_SIZE; i++) {
25
+            if (entries[i].size() > 0) {
26
+                for (Entry e : entries[i]) {
27
+                    sizeCounter++;
28
+                }
29
+            }
30
+        }
31
+        return sizeCounter;
23 32
     }
24 33
 
25 34
     @Override
26 35
     public boolean isEmptry() {
27
-        return false;
36
+        return size() == 0;
28 37
     }
29 38
 
30 39
     @Override
31 40
     public Cart get(User key) {
41
+        for (Entry e : entries[bucketIndex(key)]) {
42
+            if (e.getKey().equals(key)) {
43
+                return e.getValue();
44
+            }
45
+        }
32 46
         return null;
33 47
     }
34 48
 
35 49
     @Override
36 50
     public void put(User key, Cart value) {
37
-
51
+        for (Entry e : entries[bucketIndex(key)]) {
52
+            if (e.getKey().equals(key)) {
53
+                e.setValue(value);
54
+                return;
55
+            }
56
+        }
57
+        entries[bucketIndex(key)].add(new Entry(key,value));
38 58
     }
39 59
 
40 60
     @Override
41 61
     public List<User> getKeys() {
42
-        return null;
62
+        List<User> users = new ArrayList<>();
63
+        for (int i = 0; i < BUCKET_SIZE; i++) {
64
+            if (entries[i].size() > 0) {
65
+                for (Entry e : entries[i]) {
66
+                    users.add(e.getKey());
67
+                }
68
+            }
69
+        }
70
+        return users;
43 71
     }
44 72
 
45 73
     @Override
46 74
     public List<Cart> getValues() {
47
-        return null;
75
+        List<Cart> values = new ArrayList<>();
76
+        for (int i = 0; i < BUCKET_SIZE; i++) {
77
+            if (entries[i].size() > 0) {
78
+                for (Entry e : entries[i]) {
79
+                    values.add(e.getValue());
80
+                }
81
+            }
82
+        }
83
+        return values;
48 84
     }
49 85
 
50 86
     @Override
51 87
     public Cart remove(User key) {
52
-        return null;
88
+        Cart cart = null;
89
+        for (Entry e : entries[bucketIndex(key)]) {
90
+            if (e.getKey().equals(key)) {
91
+                cart = e.getValue();
92
+                remove(key);
93
+            }
94
+        }
95
+        return cart;
53 96
     }
54 97
 
55 98
     private int bucketIndex(User key){

+ 14
- 2
src/main/java/org/zipcoder/store/User.java Bestand weergeven

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

+ 51
- 51
src/test/java/org/zipcoder/store/ListMapTest.java Bestand weergeven

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