#18 hallinanc Chad completed lab

Aberto
hallinanc quer mesclar 2 commits de hallinanc/Lab-ZipcoderStore-HashMap:master em master

+ 14
- 0
src/main/java/org/zipcoder/store/Cart.java Ver arquivo

@@ -12,4 +12,18 @@ 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
+
27
+        return Objects.hash(id);
28
+    }
15 29
 }

+ 33
- 6
src/main/java/org/zipcoder/store/ListMap.java Ver arquivo

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

+ 46
- 6
src/main/java/org/zipcoder/store/MyHashMap.java Ver arquivo

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

+ 19
- 0
src/main/java/org/zipcoder/store/User.java Ver arquivo

@@ -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 boolean equals(Object obj) {
43
+        boolean same = false;
44
+        User user = (User) obj;
45
+        if(user.getName()== null){
46
+            if(user.getId() == this.id){
47
+                same = true;
48
+            }
49
+        }else if((user.getId() == this.id) && (user.getName().equalsIgnoreCase(this.name))){
50
+            same = true;
51
+        }
52
+        return same;
53
+    }
54
+
55
+    @Override
56
+    public int hashCode() {
57
+        return Objects.hash(id, name);
58
+    }
40 59
 }

+ 34
- 34
src/test/java/org/zipcoder/store/ListMapTest.java Ver arquivo

@@ -174,7 +174,7 @@ 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
177
+    //I commented this out because it takes a long time to run. Uncomment it when implement your put method, uncomment this test
178 178
 //    @Test
179 179
 //    public void testPutAndGet_WithManyItems(){
180 180
 //        // Given
@@ -195,37 +195,37 @@ public class ListMapTest {
195 195
 //        }
196 196
 //    }
197 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
-//    }
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
 }

+ 33
- 33
src/test/java/org/zipcoder/store/MyHashMapTest.java Ver arquivo

@@ -174,7 +174,7 @@ 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
177
+//    //I commented this out because it takes a long time to run. Uncomment it when implement your put method, uncomment this test
178 178
 //    @Test
179 179
 //    public void testPutAndGet_WithManyItems(){
180 180
 //        // Given
@@ -195,36 +195,36 @@ public class MyHashMapTest {
195 195
 //        }
196 196
 //    }
197 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
-//    }
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
 }