#24 trtong - finall fixed the nullpointerexception

Offen
trtong möchte 4 Commits von trtong/Lab-ZipcoderStore-HashMap:master nach master zusammenführen

+ 13
- 0
src/main/java/org/zipcoder/store/Cart.java Datei anzeigen

@@ -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 instanceof Cart)) return false;
20
+        Cart cart = (Cart) o;
21
+        return getId() == cart.getId();
22
+    }
23
+
24
+    @Override
25
+    public int hashCode() {
26
+        return Objects.hash(getId());
27
+    }
15 28
 }

+ 40
- 7
src/main/java/org/zipcoder/store/ListMap.java Datei anzeigen

@@ -1,7 +1,9 @@
1 1
 package org.zipcoder.store;
2 2
 
3 3
 import java.util.ArrayList;
4
+import java.util.HashSet;
4 5
 import java.util.List;
6
+import java.util.Set;
5 7
 
6 8
 public class ListMap implements MyMap {
7 9
     // instance field that is intialize to an ArrayList
@@ -9,40 +11,71 @@ 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 (this.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
+
26
+            for (Entry e: entries) {
27
+                if (e.getKey().equals(key)) {
28
+                    return e.getValue();
29
+                }
30
+            }
23 31
         return null;
24 32
     }
25 33
 
26 34
     @Override
27 35
     public void put(User key, Cart value) {
28
-        // 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
-
32 47
     @Override
33 48
     public List<User> getKeys() {
34 49
         // get all the keys in the map
35
-        return null;
50
+
51
+        List<User> idSet = new ArrayList<>();
52
+
53
+        for (Entry e:entries) {
54
+            idSet.add(e.getKey());
55
+        }
56
+        return idSet;
36 57
     }
37 58
 
38 59
     @Override
39 60
     public List<Cart> getValues() {
40
-        // get all the values in the map
41
-        return null;
61
+        List<Cart> valueSet = new ArrayList<>();
62
+        for (Entry e: entries) {
63
+            valueSet.add(e.getValue());
64
+        }
65
+        return valueSet;
42 66
     }
43 67
 
44 68
     @Override
45 69
     public Cart remove(User key) {
70
+        Entry eTemp = null;
71
+
72
+        for (Entry e: entries) {
73
+            if (e.getKey().equals(key)) {
74
+                eTemp = e;
75
+                entries.remove(e);
76
+                return eTemp.getValue();
77
+            }
78
+        }
46 79
         return null;
47 80
     }
48 81
 }

+ 61
- 5
src/main/java/org/zipcoder/store/MyHashMap.java Datei anzeigen

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

+ 14
- 0
src/main/java/org/zipcoder/store/User.java Datei anzeigen

@@ -22,6 +22,20 @@ public class User {
22 22
         this.name = name;
23 23
     }
24 24
 
25
+     @Override
26
+    public boolean equals(Object o) {
27
+                if (this == o) return true;
28
+                if (o == null || getClass() != o.getClass()) return false;
29
+                User user = (User) o;
30
+                return id == user.id &&
31
+                                Objects.equals(name, user.name);
32
+           }
33
+
34
+    public int hashCode() {
35
+        Objects.hash(getId(), getName());
36
+        return Objects.hash(getId(), getName());
37
+    }
38
+
25 39
     public long getId() {
26 40
         return id;
27 41
     }

+ 54
- 54
src/test/java/org/zipcoder/store/ListMapTest.java Datei anzeigen

@@ -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 Datei anzeigen

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