Demetrius Murray преди 6 години
родител
ревизия
c8e5add0bb

+ 13
- 0
src/main/java/org/zipcoder/store/Cart.java Целия файл

12
     public long getId(){
12
     public long getId(){
13
         return this.id;
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
 }

+ 27
- 8
src/main/java/org/zipcoder/store/ListMap.java Целия файл

9
 
9
 
10
     @Override
10
     @Override
11
     public int size() {
11
     public int size() {
12
-        return -1;
12
+        return entries.size();
13
     }
13
     }
14
 
14
 
15
     @Override
15
     @Override
16
     public boolean isEmptry() {
16
     public boolean isEmptry() {
17
-        return false;
17
+        if (size() > 0) return false;
18
+        return true;
18
     }
19
     }
19
 
20
 
20
     @Override
21
     @Override
21
     public Cart get(User key) {
22
     public Cart get(User key) {
22
-        // find the cart associate with the key
23
+        for (Entry e : entries) {
24
+            if (e.getKey().equals(key)) return e.getValue();
25
+        }
23
         return null;
26
         return null;
24
     }
27
     }
25
 
28
 
26
     @Override
29
     @Override
27
     public void put(User key, Cart value) {
30
     public void put(User key, Cart value) {
28
-        // add the key, value entry to the entries list
31
+        boolean contains = false;
32
+        for (Entry e : entries)
33
+            if (e.getKey().equals(key)) {
34
+                e.setValue(value);
35
+                contains = true;
36
+                break;
37
+            }
38
+        if (contains == false) entries.add(new Entry(key, value));
29
     }
39
     }
30
 
40
 
31
 
41
 
32
     @Override
42
     @Override
33
     public List<User> getKeys() {
43
     public List<User> getKeys() {
34
-        // get all the keys in the map
35
-        return null;
44
+        List<User> list = new ArrayList<>();
45
+        entries.forEach(e ->list.add(e.getKey()));
46
+        return list;
36
     }
47
     }
37
 
48
 
38
     @Override
49
     @Override
39
     public List<Cart> getValues() {
50
     public List<Cart> getValues() {
40
-        // get all the values in the map
41
-        return null;
51
+        List<Cart> list = new ArrayList<>();
52
+        entries.forEach(e ->list.add(e.getValue()));
53
+        return list;
42
     }
54
     }
43
 
55
 
44
     @Override
56
     @Override
45
     public Cart remove(User key) {
57
     public Cart remove(User key) {
58
+        //remove and return cart
59
+        for (Entry e : entries)
60
+            if (e.getKey().equals(key)){
61
+                Cart c = e.getValue();
62
+                e.setValue(null);
63
+                return c;
64
+            }
46
         return null;
65
         return null;
47
     }
66
     }
48
 }
67
 }

+ 33
- 4
src/main/java/org/zipcoder/store/MyHashMap.java Целия файл

19
 
19
 
20
     @Override
20
     @Override
21
     public int size() {
21
     public int size() {
22
-        return -1;
22
+        int size = 0;
23
+        for(List<Entry> l : entries){
24
+            size+=l.size();
25
+        }
26
+        return size;
23
     }
27
     }
24
 
28
 
25
     @Override
29
     @Override
26
     public boolean isEmptry() {
30
     public boolean isEmptry() {
27
-        return false;
31
+        if (size() > 0) return false;
32
+        return true;
28
     }
33
     }
29
 
34
 
30
     @Override
35
     @Override
31
     public Cart get(User key) {
36
     public Cart get(User key) {
37
+        for (Entry e : entries[bucketIndex(key)]){
38
+            if (e.getKey().equals(key)) return e.getValue();
39
+        }
32
         return null;
40
         return null;
33
     }
41
     }
34
 
42
 
35
     @Override
43
     @Override
36
     public void put(User key, Cart value) {
44
     public void put(User key, Cart value) {
45
+        boolean contains = false;
46
+        List<Entry> l = entries[bucketIndex(key)];
37
 
47
 
48
+        for (Entry e : l)
49
+            if (e.getKey().equals(key)) {
50
+                e.setValue(value);
51
+                contains = true;
52
+                break;
53
+            }
54
+        if (contains == false) l.add(new Entry(key, value));
38
     }
55
     }
39
 
56
 
40
     @Override
57
     @Override
41
     public List<User> getKeys() {
58
     public List<User> getKeys() {
42
-        return null;
59
+        List<User> list = new ArrayList<>();
60
+        for(List<Entry> l : entries)
61
+            l.forEach(e -> list.add(e.getKey()));
62
+        return list;
43
     }
63
     }
44
 
64
 
45
     @Override
65
     @Override
46
     public List<Cart> getValues() {
66
     public List<Cart> getValues() {
47
-        return null;
67
+        List<Cart> list = new ArrayList<>();
68
+        for(List<Entry> l : entries)
69
+            l.forEach(e -> list.add(e.getValue()));
70
+        return list;
48
     }
71
     }
49
 
72
 
50
     @Override
73
     @Override
51
     public Cart remove(User key) {
74
     public Cart remove(User key) {
75
+        for (Entry e : entries[bucketIndex(key)])
76
+            if (e.getKey().equals(key)){
77
+                Cart c = e.getValue();
78
+                e.setValue(null);
79
+                return c;
80
+            }
52
         return null;
81
         return null;
53
     }
82
     }
54
 
83
 

+ 1
- 0
src/main/java/org/zipcoder/store/MyMap.java Целия файл

12
     /**
12
     /**
13
      * @return true if the map has one or more item in it. False otherwise.
13
      * @return true if the map has one or more item in it. False otherwise.
14
      */
14
      */
15
+    //other way around?
15
     public boolean isEmptry();
16
     public boolean isEmptry();
16
 
17
 
17
     /**
18
     /**

+ 13
- 0
src/main/java/org/zipcoder/store/User.java Целия файл

37
     public void setName(String name) {
37
     public void setName(String name) {
38
         this.name = name;
38
         this.name = name;
39
     }
39
     }
40
+
41
+    public Boolean equals(User u){
42
+        if (this == u) return true;
43
+        if (u == null || getClass() != u.getClass()) return false;
44
+        if (u.name == null && this.name == null) {
45
+            if (u.id == this.id) return true;
46
+        } else if (u.id == this.id && u.name.equals(this.name)) return true;
47
+        return false;
48
+    }
49
+
50
+    public int hashCode(){
51
+        return Objects.hash(id,name);
52
+    }
40
 }
53
 }

+ 52
- 52
src/test/java/org/zipcoder/store/ListMapTest.java Целия файл

174
         Assert.assertTrue(values.contains(cart2));
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
-//    }
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
 
197
 
198
 //    BONUS
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 Целия файл

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