Browse Source

first attempt

Eric Foster 6 years ago
parent
commit
b337df78c7

+ 13
- 0
src/main/java/org/zipcoder/store/Cart.java View File

5
 public class Cart {
5
 public class Cart {
6
     private final long id;
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
     public Cart(long id) {
21
     public Cart(long id) {
9
         this.id = id;
22
         this.id = id;
10
     }
23
     }

+ 33
- 10
src/main/java/org/zipcoder/store/ListMap.java View File

1
 package org.zipcoder.store;
1
 package org.zipcoder.store;
2
 
2
 
3
 import java.util.ArrayList;
3
 import java.util.ArrayList;
4
+import java.util.Iterator;
4
 import java.util.List;
5
 import java.util.List;
5
 
6
 
6
 public class ListMap implements MyMap {
7
 public class ListMap implements MyMap {
9
 
10
 
10
     @Override
11
     @Override
11
     public int size() {
12
     public int size() {
12
-        return -1;
13
+        return entries.size();
13
     }
14
     }
14
 
15
 
15
     @Override
16
     @Override
16
     public boolean isEmptry() {
17
     public boolean isEmptry() {
17
-        return false;
18
+        return (size()==0);
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
-        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
     @Override
35
     @Override
27
     public void put(User key, Cart value) {
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
     @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> entriesUser = new ArrayList<>();
45
+        for (Entry e : entries){
46
+            entriesUser.add(e.getKey());
47
+        }
48
+        return entriesUser;
36
     }
49
     }
37
 
50
 
38
     @Override
51
     @Override
39
     public List<Cart> getValues() {
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
     @Override
60
     @Override
45
     public Cart remove(User key) {
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 View File

11
 
11
 
12
     public MyHashMap(){
12
     public MyHashMap(){
13
         entries = new List[BUCKET_SIZE];
13
         entries = new List[BUCKET_SIZE];
14
-
15
         for(int i = 0; i < BUCKET_SIZE; i++) {
14
         for(int i = 0; i < BUCKET_SIZE; i++) {
16
             entries[i] = new ArrayList<>();
15
             entries[i] = new ArrayList<>();
17
         }
16
         }
19
 
18
 
20
     @Override
19
     @Override
21
     public int size() {
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
     @Override
28
     @Override
26
     public boolean isEmptry() {
29
     public boolean isEmptry() {
27
-        return false;
30
+        return (size() == 0);
28
     }
31
     }
29
 
32
 
30
     @Override
33
     @Override
31
     public Cart get(User key) {
34
     public Cart get(User key) {
35
+        for(int i = 0; i < BUCKET_SIZE; i++) {
36
+            entries[i] = new ArrayList<>();
37
+        }
32
         return null;
38
         return null;
33
     }
39
     }
34
 
40
 
35
     @Override
41
     @Override
36
     public void put(User key, Cart value) {
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
     @Override
47
     @Override

+ 15
- 0
src/main/java/org/zipcoder/store/User.java View File

22
         this.name = name;
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
     public long getId() {
40
     public long getId() {
26
         return id;
41
         return id;
27
     }
42
     }

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

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
 }