Shivam Patel 6 년 전
부모
커밋
07999b0647

+ 14
- 0
src/main/java/org/zipcoder/store/Cart.java 파일 보기

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

+ 35
- 6
src/main/java/org/zipcoder/store/ListMap.java 파일 보기

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

+ 38
- 6
src/main/java/org/zipcoder/store/MyHashMap.java 파일 보기

@@ -19,32 +19,64 @@ public class MyHashMap implements MyMap{
19 19
 
20 20
     @Override
21 21
     public int size() {
22
-        return -1;
22
+        int size = 0;
23
+        for(List<Entry> lst: entries){
24
+            size+= lst.size();
25
+
26
+        }
27
+        return size;
23 28
     }
24 29
 
25 30
     @Override
26 31
     public boolean isEmptry() {
27
-        return false;
32
+        if(size() == 0){
33
+            return true;
34
+        }
35
+        else{
36
+            return false;
37
+        }
28 38
     }
29 39
 
30 40
     @Override
31 41
     public Cart get(User key) {
32
-        return null;
42
+        Cart c = null;
43
+        List<Entry> entryList = entries[bucketIndex(key)];
44
+        for(Entry e: entryList){
45
+            if(e.getKey().equals(key)){
46
+               c = e.getValue();
47
+            }
48
+
49
+        }
50
+        return c;
33 51
     }
34 52
 
35 53
     @Override
36 54
     public void put(User key, Cart value) {
37
-
55
+        entries[bucketIndex(key)].add(new Entry(key,value));
38 56
     }
39 57
 
40 58
     @Override
41 59
     public List<User> getKeys() {
42
-        return null;
60
+        List<User> u = new ArrayList<>();
61
+
62
+        for(List<Entry> entryList : entries){
63
+            for(Entry e : entryList){
64
+                u.add(e.getKey());
65
+            }
66
+
67
+        }
68
+        return u;
43 69
     }
44 70
 
45 71
     @Override
46 72
     public List<Cart> getValues() {
47
-        return null;
73
+        List<Cart> c = new ArrayList<>();
74
+        for(List<Entry> entryList : entries){
75
+            for(Entry e : entryList){
76
+                c.add(e.getValue());
77
+            }
78
+        }
79
+        return c;
48 80
     }
49 81
 
50 82
     @Override

+ 20
- 0
src/main/java/org/zipcoder/store/User.java 파일 보기

@@ -37,4 +37,24 @@ 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 equals = false;
44
+        User u = (User) obj;
45
+        if((this.getId() == u.getId()) && (this.getName() == u.getName())){
46
+            equals = true;
47
+
48
+        }
49
+        return equals;
50
+    }
51
+
52
+    public int hashCode(){
53
+        if(getName()== null){
54
+            return (int) id;
55
+        }
56
+        else{
57
+            return (int) id + getName().hashCode();
58
+        }
59
+    }
40 60
 }

+ 20
- 20
src/test/java/org/zipcoder/store/ListMapTest.java 파일 보기

@@ -174,26 +174,26 @@ 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
-//    }
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 197
 
198 198
 //    BONUS
199 199
 //    @Test

+ 19
- 19
src/test/java/org/zipcoder/store/MyHashMapTest.java 파일 보기

@@ -175,25 +175,25 @@ 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
-//    }
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
 //    @Test
199 199
 //    public void testRemove(){