Browse Source

Part 2 complete

Lauren Green 6 years ago
parent
commit
3db436d0b1

+ 50
- 8
src/main/java/org/zipcoder/store/ListMap.java View File

7
     // instance field that is intialize to an ArrayList
7
     // instance field that is intialize to an ArrayList
8
     private List<Entry> entries = new ArrayList<>();
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
     @Override
18
     @Override
11
     public int size() {
19
     public int size() {
12
-        return -1;
20
+        return entries.size();
13
     }
21
     }
14
 
22
 
15
     @Override
23
     @Override
16
-    public boolean isEmptry() {
24
+    public boolean isEmpty() {
25
+
26
+        if(size() == 0) {
27
+            return true;
28
+        }
29
+
17
         return false;
30
         return false;
18
     }
31
     }
19
 
32
 
20
     @Override
33
     @Override
21
     public Cart get(User key) {
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
         return null;
42
         return null;
24
     }
43
     }
25
 
44
 
26
     @Override
45
     @Override
27
     public void put(User key, Cart value) {
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
     @Override
60
     @Override
33
     public List<User> getKeys() {
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
     @Override
69
     @Override
39
     public List<Cart> getValues() {
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
     @Override
78
     @Override
45
     public Cart remove(User key) {
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
         return null;
88
         return null;
47
     }
89
     }
48
 }
90
 }

+ 1
- 1
src/main/java/org/zipcoder/store/MyHashMap.java View File

23
     }
23
     }
24
 
24
 
25
     @Override
25
     @Override
26
-    public boolean isEmptry() {
26
+    public boolean isEmpty() {
27
         return false;
27
         return false;
28
     }
28
     }
29
 
29
 

+ 3
- 1
src/main/java/org/zipcoder/store/MyMap.java View File

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
-    public boolean isEmptry();
15
+    public boolean isEmpty();
16
 
16
 
17
     /**
17
     /**
18
      * Return the cart associated with the user
18
      * Return the cart associated with the user
44
      * @return The cart associated with the user
44
      * @return The cart associated with the user
45
      */
45
      */
46
     public Cart remove(User key);
46
     public Cart remove(User key);
47
+
48
+
47
 }
49
 }

+ 53
- 53
src/test/java/org/zipcoder/store/ListMapTest.java View File

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

+ 2
- 2
src/test/java/org/zipcoder/store/MyHashMapTest.java View File

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