Browse Source

lab finished

yauhenip 6 years ago
parent
commit
4554cba1fc

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

12
     public long getId(){
12
     public long getId(){
13
         return this.id;
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
+
27
+        return Objects.hash(id);
28
+    }
15
 }
29
 }

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

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

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

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

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

11
 
11
 
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
+     * Test is built vice versa - true when list is empty, false otherwise
14
      */
15
      */
15
     public boolean isEmptry();
16
     public boolean isEmptry();
16
 
17
 

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

37
     public void setName(String name) {
37
     public void setName(String name) {
38
         this.name = name;
38
         this.name = name;
39
     }
39
     }
40
+
41
+    public boolean equals(User user) {
42
+        return Objects.equals(user.getId(), this.id) && Objects.equals(user.getName(), this.name);
43
+    }
44
+
45
+    public int hashCode() {
46
+        return Objects.hash(this.id, this.name);
47
+    }
40
 }
48
 }

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

+ 52
- 52
src/test/java/org/zipcoder/store/MyHashMapTest.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
-//    }
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
 }