瀏覽代碼

listmap including bonus

Allison Ziegler 6 年之前
父節點
當前提交
06e8105b58

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

+ 39
- 5
src/main/java/org/zipcoder/store/ListMap.java 查看文件

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

+ 16
- 1
src/main/java/org/zipcoder/store/User.java 查看文件

@@ -7,13 +7,18 @@ public class User {
7 7
     private long id;
8 8
     private String name;
9 9
 
10
-    public User(){}
10
+    public User(){
11
+        id = 0;
12
+        name = "";
13
+    }
11 14
 
12 15
     public User(long id) {
13 16
         this.id = id;
17
+        name = "";
14 18
     }
15 19
 
16 20
     public User(String name) {
21
+        id = 0;
17 22
         this.name = name;
18 23
     }
19 24
 
@@ -37,4 +42,14 @@ public class User {
37 42
     public void setName(String name) {
38 43
         this.name = name;
39 44
     }
45
+
46
+    public boolean equals(User anotherUser) {
47
+        if (anotherUser.getId() == id && anotherUser.getName().equals(name)) {
48
+            return true;
49
+        }
50
+        else return false;
51
+    }
52
+    public int hashCode() {
53
+        return Objects.hash(id, name);
54
+    }
40 55
 }

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