Eric Foster vor 6 Jahren
Ursprung
Commit
b337df78c7

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

@@ -5,6 +5,19 @@ import java.util.Objects;
5 5
 public class Cart {
6 6
     private final long id;
7 7
 
8
+    @Override
9
+    public boolean equals(Object o) {
10
+        if (this == o) return true;
11
+        if (o == null || getClass() != o.getClass()) return false;
12
+        Cart cart = (Cart) o;
13
+        return id == cart.id;
14
+    }
15
+
16
+    @Override
17
+    public int hashCode() {
18
+        return Objects.hash(id);
19
+    }
20
+
8 21
     public Cart(long id) {
9 22
         this.id = id;
10 23
     }

+ 33
- 10
src/main/java/org/zipcoder/store/ListMap.java Datei anzeigen

@@ -1,6 +1,7 @@
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;
5 6
 
6 7
 public class ListMap implements MyMap {
@@ -9,40 +10,62 @@ public class ListMap implements MyMap {
9 10
 
10 11
     @Override
11 12
     public int size() {
12
-        return -1;
13
+        return entries.size();
13 14
     }
14 15
 
15 16
     @Override
16 17
     public boolean isEmptry() {
17
-        return false;
18
+        return (size()==0);
18 19
     }
19 20
 
20 21
     @Override
21 22
     public Cart get(User key) {
22
-        // find the cart associate with the key
23
-        return null;
23
+        Iterator<Entry> entryIterator = entries.iterator();
24
+        Entry retrievedEntry;
25
+        Cart retrievedCart = null;
26
+        while (entryIterator.hasNext()){
27
+            retrievedEntry = entryIterator.next();
28
+            if (retrievedEntry.getKey().equals(key)){
29
+                retrievedCart = retrievedEntry.getValue();
30
+            }
31
+        }
32
+        return retrievedCart;
24 33
     }
25 34
 
26 35
     @Override
27 36
     public void put(User key, Cart value) {
28
-        // add the key, value entry to the entries list
37
+        Entry newEntry = new Entry(key, value);
38
+        entries.add(newEntry);
29 39
     }
30 40
 
31 41
 
32 42
     @Override
33 43
     public List<User> getKeys() {
34
-        // get all the keys in the map
35
-        return null;
44
+        List<User> entriesUser = new ArrayList<>();
45
+        for (Entry e : entries){
46
+            entriesUser.add(e.getKey());
47
+        }
48
+        return entriesUser;
36 49
     }
37 50
 
38 51
     @Override
39 52
     public List<Cart> getValues() {
40
-        // get all the values in the map
41
-        return null;
53
+        List<Cart> entriesCart = new ArrayList<>();
54
+        for (Entry e : entries){
55
+            entriesCart.add(e.getValue());
56
+        }
57
+        return entriesCart;
42 58
     }
43 59
 
44 60
     @Override
45 61
     public Cart remove(User key) {
46
-        return null;
62
+        Cart removedCart = null;
63
+        for (int i=0; i<entries.size(); i++){
64
+            if (entries.get(i).getKey() == key){
65
+                removedCart = entries.get(i).getValue();
66
+                entries.remove(entries.get(i));
67
+            }
68
+        }
69
+        return removedCart;
47 70
     }
48 71
 }

+ 11
- 4
src/main/java/org/zipcoder/store/MyHashMap.java Datei anzeigen

@@ -11,7 +11,6 @@ public class MyHashMap implements MyMap{
11 11
 
12 12
     public MyHashMap(){
13 13
         entries = new List[BUCKET_SIZE];
14
-
15 14
         for(int i = 0; i < BUCKET_SIZE; i++) {
16 15
             entries[i] = new ArrayList<>();
17 16
         }
@@ -19,22 +18,30 @@ public class MyHashMap implements MyMap{
19 18
 
20 19
     @Override
21 20
     public int size() {
22
-        return -1;
21
+        int totalEntries = 0;
22
+        for(int i = 0; i < BUCKET_SIZE; i++) {
23
+            totalEntries += entries[i].size();
24
+        }
25
+        return totalEntries;
23 26
     }
24 27
 
25 28
     @Override
26 29
     public boolean isEmptry() {
27
-        return false;
30
+        return (size() == 0);
28 31
     }
29 32
 
30 33
     @Override
31 34
     public Cart get(User key) {
35
+        for(int i = 0; i < BUCKET_SIZE; i++) {
36
+            entries[i] = new ArrayList<>();
37
+        }
32 38
         return null;
33 39
     }
34 40
 
35 41
     @Override
36 42
     public void put(User key, Cart value) {
37
-
43
+        Entry newEntry = new Entry(key, value);
44
+        entries[0].add(newEntry);
38 45
     }
39 46
 
40 47
     @Override

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

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

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

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