Shivam Patel 6 vuotta sitten
vanhempi
commit
07999b0647

+ 14
- 0
src/main/java/org/zipcoder/store/Cart.java Näytä tiedosto

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
 }

+ 35
- 6
src/main/java/org/zipcoder/store/ListMap.java Näytä tiedosto

1
 package org.zipcoder.store;
1
 package org.zipcoder.store;
2
 
2
 
3
-import java.util.ArrayList;
4
-import java.util.List;
3
+import java.util.*;
5
 
4
 
6
 public class ListMap implements MyMap {
5
 public class ListMap implements MyMap {
7
     // instance field that is intialize to an ArrayList
6
     // instance field that is intialize to an ArrayList
9
 
8
 
10
     @Override
9
     @Override
11
     public int size() {
10
     public int size() {
12
-        return -1;
11
+        return entries.size();
13
     }
12
     }
14
 
13
 
15
     @Override
14
     @Override
16
     public boolean isEmptry() {
15
     public boolean isEmptry() {
17
-        return false;
16
+        if(size() == 0){
17
+            return true;}
18
+        else{
19
+            return false;}
18
     }
20
     }
19
 
21
 
20
     @Override
22
     @Override
21
     public Cart get(User key) {
23
     public Cart get(User key) {
22
         // find the cart associate with the key
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
         return null;
32
         return null;
24
     }
33
     }
25
 
34
 
26
     @Override
35
     @Override
27
     public void put(User key, Cart value) {
36
     public void put(User key, Cart value) {
28
         // add the key, value entry to the entries list
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
     @Override
51
     @Override
33
     public List<User> getKeys() {
52
     public List<User> getKeys() {
34
         // get all the keys in the map
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
     @Override
61
     @Override
39
     public List<Cart> getValues() {
62
     public List<Cart> getValues() {
40
         // get all the values in the map
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
     @Override
72
     @Override
45
     public Cart remove(User key) {
73
     public Cart remove(User key) {
74
+
46
         return null;
75
         return null;
47
     }
76
     }
48
 }
77
 }

+ 38
- 6
src/main/java/org/zipcoder/store/MyHashMap.java Näytä tiedosto

19
 
19
 
20
     @Override
20
     @Override
21
     public int size() {
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
     @Override
30
     @Override
26
     public boolean isEmptry() {
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
     @Override
40
     @Override
31
     public Cart get(User key) {
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
     @Override
53
     @Override
36
     public void put(User key, Cart value) {
54
     public void put(User key, Cart value) {
37
-
55
+        entries[bucketIndex(key)].add(new Entry(key,value));
38
     }
56
     }
39
 
57
 
40
     @Override
58
     @Override
41
     public List<User> getKeys() {
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
     @Override
71
     @Override
46
     public List<Cart> getValues() {
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
     @Override
82
     @Override

+ 20
- 0
src/main/java/org/zipcoder/store/User.java Näytä tiedosto

37
     public void setName(String name) {
37
     public void setName(String name) {
38
         this.name = name;
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 Näytä tiedosto

174
         Assert.assertTrue(values.contains(cart2));
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
 //    BONUS
198
 //    BONUS
199
 //    @Test
199
 //    @Test

+ 19
- 19
src/test/java/org/zipcoder/store/MyHashMapTest.java Näytä tiedosto

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
 //    @Test
198
 //    @Test
199
 //    public void testRemove(){
199
 //    public void testRemove(){