#19 laurenstewartgreen

オープン
laurengreen が 5 個のコミットを laurengreen/Lab-ZipcoderStore-HashMap:master から master へマージしようとしています

+ 13
- 0
src/main/java/org/zipcoder/store/Cart.java ファイルの表示

@@ -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
- 8
src/main/java/org/zipcoder/store/ListMap.java ファイルの表示

@@ -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().equals(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
 }

+ 58
- 6
src/main/java/org/zipcoder/store/MyHashMap.java ファイルの表示

@@ -17,41 +17,93 @@ public class MyHashMap implements MyMap{
17 17
         }
18 18
     }
19 19
 
20
+    public List<Entry>[] getEntries() {
21
+        return entries;
22
+    }
23
+
20 24
     @Override
21 25
     public int size() {
22
-        return -1;
26
+        int count = 0;
27
+        for(int i = 0; i < BUCKET_SIZE; i++) {
28
+            if(!getEntries()[i].isEmpty()) {
29
+                count++;
30
+            }
31
+        }
32
+        return count;
23 33
     }
24 34
 
25 35
     @Override
26
-    public boolean isEmptry() {
36
+    public boolean isEmpty() {
37
+
38
+        if(size() == 0) {
39
+            return true;
40
+        }
41
+
27 42
         return false;
28 43
     }
29 44
 
30 45
     @Override
31 46
     public Cart get(User key) {
47
+        for(Entry e: getEntries()[bucketIndex(key)]) {
48
+            if(e.getKey().equals(key)){
49
+                return e.getValue();
50
+            }
51
+        }
52
+
32 53
         return null;
33 54
     }
34 55
 
35 56
     @Override
36 57
     public void put(User key, Cart value) {
58
+        if(get(key) != null) {
59
+            for (Entry e : getEntries()[bucketIndex(key)]) {
60
+                if (e.getKey().equals(key)) {
61
+                    e.setValue(value);
62
+                }
63
+            }
64
+        } else {
65
+
66
+            Entry entry = new Entry(key, value);
67
+            getEntries()[bucketIndex(key)].add(entry);
37 68
 
69
+        }
38 70
     }
39 71
 
40 72
     @Override
41 73
     public List<User> getKeys() {
42
-        return null;
74
+        List<User> keys = new ArrayList<>();
75
+        for (int i = 0; i < BUCKET_SIZE; i++) {
76
+            for (Entry e : getEntries()[i]) {
77
+                keys.add(e.getKey());
78
+            }
79
+        }
80
+        return keys;
43 81
     }
44 82
 
45 83
     @Override
46 84
     public List<Cart> getValues() {
47
-        return null;
85
+        List<Cart> values = new ArrayList<>();
86
+        for (int i = 0; i < BUCKET_SIZE; i++) {
87
+            for (Entry e : getEntries()[i]) {
88
+                values.add(e.getValue());
89
+            }
90
+        }
91
+        return values;
48 92
     }
49 93
 
50 94
     @Override
51 95
     public Cart remove(User key) {
52
-        return null;
53
-    }
96
+        for (int i = 0; i < BUCKET_SIZE; i++) {
97
+            for (Entry e : getEntries()[i]) {
98
+                if (e.getKey().equals(key)) {
99
+                    getEntries()[i].remove(e);
100
+                    return e.getValue();
101
+                }
102
+            }
103
+        }
104
+            return null;
54 105
 
106
+    }
55 107
     private int bucketIndex(User key){
56 108
         return Math.abs(key.hashCode()) % entries.length;
57 109
     }

+ 3
- 1
src/main/java/org/zipcoder/store/MyMap.java ファイルの表示

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

+ 33
- 0
src/main/java/org/zipcoder/store/User.java ファイルの表示

@@ -37,4 +37,37 @@ public class User {
37 37
     public void setName(String name) {
38 38
         this.name = name;
39 39
     }
40
+
41
+    @Override
42
+    public boolean equals(Object o) {
43
+        if (this == o) return true;
44
+        if (o == null || getClass() != o.getClass()) return false;
45
+        User user = (User) o;
46
+        return id == user.id &&
47
+                Objects.equals(name, user.name);
48
+    }
49
+
50
+    //    @Override
51
+//    public boolean equals(Object otherUser) {
52
+//
53
+//        try {
54
+//        if (this.getName().equalsIgnoreCase(otherUser.getName()) && this.getId() == otherUser.getId()) {
55
+//            return true;
56
+//        }
57
+//        } catch (NullPointerException e) {
58
+//
59
+//        if (this.getName() == null && otherUser.getName() == null) {
60
+//            return true;
61
+//        } else {
62
+//            return false;
63
+//        }
64
+//        }
65
+//        return false;
66
+//        }
67
+
68
+    @Override
69
+    public int hashCode() {
70
+        return Objects.hash(id, name);
71
+
72
+    }
40 73
 }

+ 53
- 53
src/test/java/org/zipcoder/store/ListMapTest.java ファイルの表示

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

+ 54
- 54
src/test/java/org/zipcoder/store/MyHashMapTest.java ファイルの表示

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

+ 9
- 0
src/test/java/org/zipcoder/store/UserTest.java ファイルの表示

@@ -65,6 +65,15 @@ public class UserTest {
65 65
     }
66 66
 
67 67
     @Test
68
+    public void testEqual_withOneNamed(){
69
+        long id = 613;
70
+        User user1 = new User(id);
71
+        User user2 = new User(id, "Fiona");
72
+
73
+        Assert.assertFalse(user1.equals(user2));
74
+    }
75
+
76
+    @Test
68 77
     public void testEqual_withSameNameDifferentId(){
69 78
         String name = "Asher";
70 79
         User user1 = new User(92, name);