Lauren Green před 6 roky
rodič
revize
3db436d0b1

+ 50
- 8
src/main/java/org/zipcoder/store/ListMap.java Zobrazit soubor

@@ -7,42 +7,84 @@ public class ListMap implements MyMap {
7 7
     // instance field that is intialize to an ArrayList
8 8
     private List<Entry> entries = new ArrayList<>();
9 9
 
10
+    public ListMap() {
11
+
12
+    }
13
+
14
+    public List<Entry> getEntries() {
15
+        return entries;
16
+    }
17
+
10 18
     @Override
11 19
     public int size() {
12
-        return -1;
20
+        return entries.size();
13 21
     }
14 22
 
15 23
     @Override
16
-    public boolean isEmptry() {
24
+    public boolean isEmpty() {
25
+
26
+        if(size() == 0) {
27
+            return true;
28
+        }
29
+
17 30
         return false;
18 31
     }
19 32
 
20 33
     @Override
21 34
     public Cart get(User key) {
22
-        // find the cart associate with the key
35
+
36
+        for(Entry e : getEntries()) {
37
+            if(e.getKey().equals(key)) {
38
+                return e.getValue();
39
+            }
40
+        }
41
+
23 42
         return null;
24 43
     }
25 44
 
26 45
     @Override
27 46
     public void put(User key, Cart value) {
28
-        // add the key, value entry to the entries list
47
+        if(get(key) != null) {
48
+            for(Entry e : getEntries()) {
49
+                if(e.getKey() == key) {
50
+                    e.setValue(value);
51
+                }
52
+            }
53
+        } else {
54
+            Entry entry = new Entry(key, value);
55
+            getEntries().add(entry);
56
+        }
29 57
     }
30 58
 
31 59
 
32 60
     @Override
33 61
     public List<User> getKeys() {
34
-        // get all the keys in the map
35
-        return null;
62
+        List<User> keys = new ArrayList<>();
63
+        for(Entry e : getEntries()) {
64
+            keys.add(e.getKey());
65
+        }
66
+        return keys;
36 67
     }
37 68
 
38 69
     @Override
39 70
     public List<Cart> getValues() {
40
-        // get all the values in the map
41
-        return null;
71
+        List<Cart> values = new ArrayList<>();
72
+        for(Entry e : getEntries()) {
73
+            values.add(e.getValue());
74
+        }
75
+        return values;
42 76
     }
43 77
 
44 78
     @Override
45 79
     public Cart remove(User key) {
80
+
81
+        for(Entry e : getEntries()) {
82
+            if(e.getKey() == key) {
83
+                getEntries().remove(e);
84
+                return e.getValue();
85
+            }
86
+        }
87
+
46 88
         return null;
47 89
     }
48 90
 }

+ 1
- 1
src/main/java/org/zipcoder/store/MyHashMap.java Zobrazit soubor

@@ -23,7 +23,7 @@ public class MyHashMap implements MyMap{
23 23
     }
24 24
 
25 25
     @Override
26
-    public boolean isEmptry() {
26
+    public boolean isEmpty() {
27 27
         return false;
28 28
     }
29 29
 

+ 3
- 1
src/main/java/org/zipcoder/store/MyMap.java Zobrazit soubor

@@ -12,7 +12,7 @@ public interface MyMap {
12 12
     /**
13 13
      * @return true if the map has one or more item in it. False otherwise.
14 14
      */
15
-    public boolean isEmptry();
15
+    public boolean isEmpty();
16 16
 
17 17
     /**
18 18
      * Return the cart associated with the user
@@ -44,4 +44,6 @@ public interface MyMap {
44 44
      * @return The cart associated with the user
45 45
      */
46 46
     public Cart remove(User key);
47
+
48
+
47 49
 }

+ 53
- 53
src/test/java/org/zipcoder/store/ListMapTest.java Zobrazit soubor

@@ -116,7 +116,7 @@ public class ListMapTest {
116 116
 
117 117
     @Test
118 118
     public void testIsEmpty_WithoutEntry(){
119
-        Assert.assertTrue(myMap.isEmptry());
119
+        Assert.assertTrue(myMap.isEmpty());
120 120
     }
121 121
 
122 122
     @Test
@@ -129,7 +129,7 @@ public class ListMapTest {
129 129
         myMap.put(user1, cart1);
130 130
 
131 131
         // Then
132
-        Assert.assertFalse(myMap.isEmptry());
132
+        Assert.assertFalse(myMap.isEmpty());
133 133
     }
134 134
 
135 135
     @Test
@@ -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
 }

+ 2
- 2
src/test/java/org/zipcoder/store/MyHashMapTest.java Zobrazit soubor

@@ -116,7 +116,7 @@ public class MyHashMapTest {
116 116
 
117 117
     @Test
118 118
     public void testIsEmpty_WithoutEntry(){
119
-        Assert.assertTrue(myMap.isEmptry());
119
+        Assert.assertTrue(myMap.isEmpty());
120 120
     }
121 121
 
122 122
     @Test
@@ -129,7 +129,7 @@ public class MyHashMapTest {
129 129
         myMap.put(user1, cart1);
130 130
 
131 131
         // Then
132
-        Assert.assertFalse(myMap.isEmptry());
132
+        Assert.assertFalse(myMap.isEmpty());
133 133
     }
134 134
 
135 135
     @Test