David Thornley 6 лет назад
Родитель
Сommit
ad1863a0e5

+ 14
- 0
src/main/java/org/zipcoder/store/Cart.java Просмотреть файл

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

+ 36
- 6
src/main/java/org/zipcoder/store/ListMap.java Просмотреть файл

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

+ 55
- 7
src/main/java/org/zipcoder/store/MyHashMap.java Просмотреть файл

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

+ 17
- 0
src/main/java/org/zipcoder/store/User.java Просмотреть файл

@@ -37,4 +37,21 @@ 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
 }
56
+
57
+

+ 54
- 54
src/test/java/org/zipcoder/store/ListMapTest.java Просмотреть файл

@@ -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
-//    }
197
-
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
-//    }
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
+    //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
+    }
231 231
 }

+ 52
- 52
src/test/java/org/zipcoder/store/MyHashMapTest.java Просмотреть файл

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