#20 WIP bonus lab - maps

Atvērta
jonathan-hinds vēlas sapludināt 1 revīzijas no jonathan-hinds/Lab-ZipcoderStore-HashMap:master uz master

+ 13
- 0
src/main/java/org/zipcoder/store/Cart.java Parādīt failu

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

+ 47
- 7
src/main/java/org/zipcoder/store/ListMap.java Parādīt failu

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

+ 75
- 6
src/main/java/org/zipcoder/store/MyHashMap.java Parādīt failu

@@ -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;
@@ -13,43 +13,112 @@ public class MyHashMap implements MyMap{
13 13
         entries = new List[BUCKET_SIZE];
14 14
 
15 15
         for(int i = 0; i < BUCKET_SIZE; i++) {
16
-            entries[i] = new ArrayList<>();
16
+            entries[i] = new ArrayList<Entry>();
17 17
         }
18 18
     }
19 19
 
20 20
     @Override
21 21
     public int size() {
22
-        return -1;
22
+        int size = 0;
23
+
24
+        for(List list : entries){
25
+            size += list.size();
26
+        }
27
+
28
+        return size;
23 29
     }
24 30
 
25 31
     @Override
26 32
     public boolean isEmptry() {
33
+
34
+        if(size() == 0){
35
+            return true;
36
+        }
37
+
27 38
         return false;
28 39
     }
29 40
 
30 41
     @Override
31 42
     public Cart get(User key) {
43
+
44
+        for(int i = 0; i < entries.length; i ++){
45
+            for(int m = 0; m < entries[i].size(); m ++){
46
+                Entry entry = entries[i].get(m);
47
+                if(entry.getKey().equals(key)){
48
+                    return entry.getValue();
49
+                }
50
+            }
51
+        }
52
+
32 53
         return null;
33 54
     }
34 55
 
35 56
     @Override
36 57
     public void put(User key, Cart value) {
58
+        int index = bucketIndex(key);
59
+        boolean added = false;
37 60
 
61
+        List list = entries[index];
62
+
63
+        if(list.size() != 0) {
64
+            for (int i = 0; i < list.size(); i++) {
65
+                if (((Entry) list.get(i)).getKey().equals(key)) {
66
+                    Entry entry = (Entry) list.get(i);
67
+                    list.remove(entry);
68
+                    list.add(new Entry(key, value));
69
+                    added = true;
70
+                }
71
+            }
72
+        }
73
+        if(!added) {
74
+            entries[index].add(new Entry(key, value));
75
+        }
38 76
     }
39 77
 
40 78
     @Override
41 79
     public List<User> getKeys() {
42
-        return null;
80
+        List<User> users = new ArrayList<>();
81
+
82
+        for(int i = 0; i < entries.length; i ++){
83
+            List list = entries[i];
84
+            for(int m = 0; m < list.size(); m ++){
85
+                Entry entry = (Entry) list.get(m);
86
+                users.add(entry.getKey());
87
+            }
88
+        }
89
+        return users;
43 90
     }
44 91
 
45 92
     @Override
46 93
     public List<Cart> getValues() {
47
-        return null;
94
+        List<Cart> carts = new ArrayList<>();
95
+
96
+        for(int i = 0; i < entries.length; i ++){
97
+            List list = entries[i];
98
+            for(int m = 0; m < list.size(); m ++){
99
+                Entry entry = (Entry) list.get(m);
100
+                carts.add(entry.getValue());
101
+            }
102
+        }
103
+        return carts;
48 104
     }
49 105
 
50 106
     @Override
51 107
     public Cart remove(User key) {
52
-        return null;
108
+
109
+        Cart removeCart = null;
110
+
111
+        for(int i = 0; i < entries.length; i ++){
112
+            List list = entries[i];
113
+            for(int m = 0; m < list.size(); m ++){
114
+                Entry entry = (Entry) (list.get(m));
115
+                if(entry.getKey().equals(key)){
116
+                    removeCart = entry.getValue();
117
+                    list.remove(entry);
118
+                }
119
+            }
120
+        }
121
+        return removeCart;
53 122
     }
54 123
 
55 124
     private int bucketIndex(User key){

+ 17
- 0
src/main/java/org/zipcoder/store/User.java Parādīt failu

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

+ 51
- 54
src/test/java/org/zipcoder/store/ListMapTest.java Parādīt failu

@@ -174,58 +174,55 @@ 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
+    @Test
178
+    public void testPutAndGet_WithManyItems(){
179
+        // Given
180
+        int numberOfEntries = 70000;
181
+
182
+        for (int i = 0; i < numberOfEntries; i++) {
183
+            myMap.put(new User(i), new Cart(i));
184
+        }
185
+
186
+        for (int i = 0; i < numberOfEntries; i++) {
187
+            // When
188
+            Cart actualCart = myMap.get(new User(i));
189
+            Cart expectedCart = new Cart(i);
190
+
191
+            // Then
192
+            Assert.assertEquals(actualCart, expectedCart);
193
+        }
194
+    }
195
+
196
+    @Test
197
+    public void testRemove(){
198
+        // Given
199
+        User user1 = new User("Lena");
200
+        Cart cart1 = new Cart(11);
201
+
202
+        User user2 = new User("Jamal");
203
+        Cart cart2 = new Cart(21);
204
+
205
+        User user3 = new User("Kelvin");
206
+        Cart cart3 = new Cart(31);
207
+
208
+        // When
209
+        myMap.put(user1, cart1);
210
+        myMap.put(user2, cart2);
211
+        myMap.put(user3, cart3);
212
+
213
+        // Then
214
+        Cart actualCart = myMap.remove(user2);
215
+
216
+        Assert.assertEquals(cart2, actualCart);
217
+        Assert.assertNull(myMap.get(user2));
218
+    }
219
+
220
+    @Test
221
+    public void testRemove_CartNotInTheMap(){
222
+        // Given
223
+        User user1 = new User("Lena");
224
+
225
+        // When & Then
226
+        Assert.assertNull(myMap.remove(user1));
227
+    }
231 228
 }

+ 52
- 53
src/test/java/org/zipcoder/store/MyHashMapTest.java Parādīt failu

@@ -174,57 +174,56 @@ 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
+    @Test
178
+    public void testPutAndGet_WithManyItems(){
179
+        // Given
180
+        int numberOfEntries = 70000;
181
+
182
+        for (int i = 0; i < numberOfEntries; i++) {
183
+            myMap.put(new User(i), new Cart(i));
184
+        }
185
+
186
+
187
+        for (int i = 0; i < numberOfEntries; i++) {
188
+            // When
189
+            Cart actualCart = myMap.get(new User(i));
190
+            Cart expectedCart = new Cart(i);
191
+
192
+            // Then
193
+            Assert.assertEquals(actualCart, expectedCart);
194
+        }
195
+    }
196
+
197
+    @Test
198
+    public void testRemove(){
199
+        // Given
200
+        User user1 = new User("Lena");
201
+        Cart cart1 = new Cart(11);
202
+
203
+        User user2 = new User("Jamal");
204
+        Cart cart2 = new Cart(21);
205
+
206
+        User user3 = new User("Kelvin");
207
+        Cart cart3 = new Cart(31);
208
+
209
+        // When
210
+        myMap.put(user1, cart1);
211
+        myMap.put(user2, cart2);
212
+        myMap.put(user3, cart3);
213
+
214
+        // Then
215
+        Cart actualCart = myMap.remove(user2);
216
+
217
+        Assert.assertEquals(cart2, actualCart);
218
+        Assert.assertNull(myMap.get(user2));
219
+    }
220
+
221
+    @Test
222
+    public void testRemove_CartNotInTheMap(){
223
+        // Given
224
+        User user1 = new User("Lena");
225
+
226
+        // When & Then
227
+        Assert.assertNull(myMap.remove(user1));
228
+    }
230 229
 }