#21 JenniferChao4 - Store Lab (Finished)

Open
JenniferChao4 wants to merge 1 commits from JenniferChao4/Lab-ZipcoderStore-HashMap:master into master

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

@@ -12,4 +12,17 @@ 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 == null || getClass() != o.getClass()) return false;
20
+        Cart cart = (Cart) o;
21
+        return id == cart.id;
22
+    }
23
+
24
+    @Override
25
+    public int hashCode() {
26
+        return Objects.hash(id);
27
+    }
15 28
 }

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

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

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

@@ -2,57 +2,118 @@ package org.zipcoder.store;
2 2
 
3 3
 import java.util.*;
4 4
 
5
-public class MyHashMap implements MyMap{
5
+public class MyHashMap implements MyMap {
6 6
     // final field that doesn't change for any object
7 7
     private static final int BUCKET_SIZE = 15;
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;
11
+    private int entryListSize;
11 12
 
12
-    public MyHashMap(){
13
+    public MyHashMap() {
13 14
         entries = new List[BUCKET_SIZE];
14 15
 
15
-        for(int i = 0; i < BUCKET_SIZE; i++) {
16
+        for (int i = 0; i < BUCKET_SIZE; i++) {
16 17
             entries[i] = new ArrayList<>();
17 18
         }
19
+
20
+        this.entryListSize = 0;
18 21
     }
19 22
 
20 23
     @Override
21 24
     public int size() {
22
-        return -1;
25
+        return this.entryListSize;
23 26
     }
24 27
 
25 28
     @Override
26 29
     public boolean isEmptry() {
30
+        if (this.entryListSize == 0) {
31
+            return true;
32
+        }
27 33
         return false;
28 34
     }
29 35
 
30 36
     @Override
31 37
     public Cart get(User key) {
38
+        List<Entry> bucket = entries[bucketIndex(key)];
39
+        for (Entry entry : bucket) {
40
+            if (entry.getKey().equals(key)) {
41
+                return entry.getValue();
42
+            }
43
+        }
32 44
         return null;
33 45
     }
34 46
 
35 47
     @Override
36 48
     public void put(User key, Cart value) {
49
+        List<Entry> bucket = entries[bucketIndex(key)];
37 50
 
51
+
52
+        if (get(key) != null) {
53
+            for (Entry entry : bucket) {
54
+                if (entry.getKey().equals(key)) {
55
+                    entry.setValue(value);
56
+                }
57
+            }
58
+        } else {
59
+            Entry entry = new Entry(key, value);
60
+            bucket.add(entry);
61
+            setEntryListSize(getEntryListSize() + 1);
62
+        }
38 63
     }
39 64
 
40 65
     @Override
41 66
     public List<User> getKeys() {
42
-        return null;
67
+        List<User> userKeys = new ArrayList<>();
68
+
69
+        for (List<Entry> entryArray : entries) {
70
+            for (Entry entry : entryArray) {
71
+                userKeys.add(entry.getKey());
72
+            }
73
+        }
74
+
75
+        return userKeys;
43 76
     }
44 77
 
45 78
     @Override
46 79
     public List<Cart> getValues() {
47
-        return null;
80
+        List<Cart> userValues = new ArrayList<>();
81
+
82
+        for (List<Entry> entryArray : entries) {
83
+            for (Entry entry : entryArray) {
84
+                userValues.add(entry.getValue());
85
+            }
86
+        }
87
+
88
+        return userValues;
48 89
     }
49 90
 
50 91
     @Override
51 92
     public Cart remove(User key) {
93
+        for (List<Entry> entryArray : entries) {
94
+            for (Entry entry : entryArray) {
95
+                if (entry.getKey().equals(key)) {
96
+                    entryArray.remove(entry);
97
+                    return entry.getValue();
98
+                }
99
+            }
100
+        }
52 101
         return null;
53 102
     }
54 103
 
55
-    private int bucketIndex(User key){
104
+    private int bucketIndex(User key) {
56 105
         return Math.abs(key.hashCode()) % entries.length;
57 106
     }
107
+
108
+    public int getEntryListSize() {
109
+        return entryListSize;
110
+    }
111
+
112
+    public void setEntryListSize(int entryListSize) {
113
+        this.entryListSize = entryListSize;
114
+    }
115
+
116
+    public List<Entry>[] getEntries() {
117
+        return entries;
118
+    }
58 119
 }

+ 17
- 2
src/main/java/org/zipcoder/store/User.java View File

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

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

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

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

@@ -175,56 +175,56 @@ public class MyHashMapTest {
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
-//    }
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
-//    }
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
 }