Tommy Rogers vor 6 Jahren
Ursprung
Commit
6d1917d232

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

@@ -12,4 +12,19 @@ public class Cart {
12 12
     public long getId(){
13 13
         return this.id;
14 14
     }
15
+
16
+    public int hashCode(){
17
+        return (int) id;
18
+    }
19
+
20
+    @Override
21
+    public boolean equals(Object o){
22
+        boolean answer =false;
23
+        Cart cart = (Cart) o;
24
+        if (this.getId() == cart.getId()) {
25
+            answer = true;
26
+        }
27
+        return answer;
28
+    }
15 29
 }
30
+

+ 1
- 1
src/main/java/org/zipcoder/store/Entry.java Datei anzeigen

@@ -20,4 +20,4 @@ public class Entry {
20 20
     public void setValue(Cart value) {
21 21
         this.value = value;
22 22
     }
23
-}
23
+}

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

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

+ 36
- 9
src/main/java/org/zipcoder/store/MyHashMap.java Datei anzeigen

@@ -4,7 +4,7 @@ import java.util.*;
4 4
 
5 5
 public class MyHashMap implements MyMap{
6 6
     // final field that doesn't change for any object
7
-    private static final int BUCKET_SIZE = 15;
7
+    private static final int BUCKET_SIZE = 100;
8 8
 
9 9
     // instance field that can only be set once. It is different for every Object
10 10
     private final List<Entry>[] entries;
@@ -19,40 +19,67 @@ public class MyHashMap implements MyMap{
19 19
 
20 20
     @Override
21 21
     public int size() {
22
-        return -1;
22
+        int count = 0;
23
+        for(List<Entry> e : entries)
24
+            count += e.size();
25
+        return count;
23 26
     }
24 27
 
25 28
     @Override
26
-    public boolean isEmptry() {
27
-        return false;
29
+    public boolean isEmpty() {
30
+        return (size() ==0);
28 31
     }
29 32
 
30 33
     @Override
31 34
     public Cart get(User key) {
32
-        return null;
35
+        Cart cart = null;
36
+        List<Entry> entry = entries[bucketIndex(key)];
37
+        for(Entry e : entry) {
38
+            if (e.getKey().equals(key))
39
+                cart =  e.getValue();
40
+        }
41
+        return cart;
33 42
     }
34 43
 
35 44
     @Override
36 45
     public void put(User key, Cart value) {
37
-
46
+        entries[bucketIndex(key)].add(new Entry(key, value));
38 47
     }
39 48
 
40 49
     @Override
41 50
     public List<User> getKeys() {
42
-        return null;
51
+        List<User> users = new ArrayList<>();
52
+        for(List<Entry> entry : entries)
53
+            for (Entry e: entry)
54
+                users.add(e.getKey());
55
+        return users;
43 56
     }
44 57
 
45 58
     @Override
46 59
     public List<Cart> getValues() {
47
-        return null;
60
+        List<Cart> carts = new ArrayList<>();
61
+        for(List<Entry> entry : entries)
62
+            for (Entry e: entry)
63
+                carts.add(e.getValue());
64
+        return carts;
48 65
     }
49 66
 
50 67
     @Override
51 68
     public Cart remove(User key) {
52
-        return null;
69
+        Cart cart = null;
70
+        List<Entry> entry = entries[bucketIndex(key)];
71
+        for(Entry e : entry){
72
+            if(e.getKey().equals(key)){
73
+                cart = e.getValue();
74
+                entry.remove(e);
75
+                break;
76
+            }
77
+        }
78
+        return cart;
53 79
     }
54 80
 
55 81
     private int bucketIndex(User key){
56 82
         return Math.abs(key.hashCode()) % entries.length;
57 83
     }
58 84
 }
85
+

+ 1
- 1
src/main/java/org/zipcoder/store/MyMap.java Datei anzeigen

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

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

@@ -37,4 +37,23 @@ public class User {
37 37
     public void setName(String name) {
38 38
         this.name = name;
39 39
     }
40
+
41
+
42
+    public int hashCode(){
43
+        if (getName() == null)
44
+            return (int) id;
45
+        else
46
+            return (int) id + getName().hashCode();
47
+    }
48
+
49
+    @Override
50
+    public boolean equals(Object o){
51
+        boolean answer =false;
52
+        User user = (User) o;
53
+        if ((this.getId() == user.getId()) && (this.getName() == user.getName())) {
54
+            answer = true;
55
+        }
56
+        return answer;
57
+    }
40 58
 }
59
+

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

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

+ 55
- 55
src/test/java/org/zipcoder/store/MyHashMapTest.java Datei anzeigen

@@ -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
@@ -174,57 +174,57 @@ public class MyHashMapTest {
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
-//    @Test
199
-//    public void testRemove(){
200
-//        // Given
201
-//        User user1 = new User("Lena");
202
-//        Cart cart1 = new Cart(11);
203
-//
204
-//        User user2 = new User("Jamal");
205
-//        Cart cart2 = new Cart(21);
206
-//
207
-//        User user3 = new User("Kelvin");
208
-//        Cart cart3 = new Cart(31);
209
-//
210
-//        // When
211
-//        myMap.put(user1, cart1);
212
-//        myMap.put(user2, cart2);
213
-//        myMap.put(user3, cart3);
214
-//
215
-//        // Then
216
-//        Cart actualCart = myMap.remove(user2);
217
-//
218
-//        Assert.assertEquals(cart2, actualCart);
219
-//        Assert.assertNull(myMap.get(user2));
220
-//    }
221
-//
222
-//    @Test
223
-//    public void testRemove_CartNotInTheMap(){
224
-//        // Given
225
-//        User user1 = new User("Lena");
226
-//
227
-//        // When & Then
228
-//        Assert.assertNull(myMap.remove(user1));
229
-//    }
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
+    @Test
199
+    public void testRemove(){
200
+        // Given
201
+        User user1 = new User("Lena");
202
+        Cart cart1 = new Cart(11);
203
+
204
+        User user2 = new User("Jamal");
205
+        Cart cart2 = new Cart(21);
206
+
207
+        User user3 = new User("Kelvin");
208
+        Cart cart3 = new Cart(31);
209
+
210
+        // When
211
+        myMap.put(user1, cart1);
212
+        myMap.put(user2, cart2);
213
+        myMap.put(user3, cart3);
214
+
215
+        // Then
216
+        Cart actualCart = myMap.remove(user2);
217
+
218
+        Assert.assertEquals(cart2, actualCart);
219
+        Assert.assertNull(myMap.get(user2));
220
+    }
221
+
222
+    @Test
223
+    public void testRemove_CartNotInTheMap(){
224
+        // Given
225
+        User user1 = new User("Lena");
226
+
227
+        // When & Then
228
+        Assert.assertNull(myMap.remove(user1));
229
+    }
230 230
 }