#19 laurenstewartgreen

Открыто
laurengreen хочет смерджить 5 коммит(ов) из laurengreen/Lab-ZipcoderStore-HashMap:master в master

+ 13
- 0
src/main/java/org/zipcoder/store/Cart.java Просмотреть файл

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
+        return Objects.hash(id);
27
+    }
15
 }
28
 }

+ 50
- 8
src/main/java/org/zipcoder/store/ListMap.java Просмотреть файл

7
     // instance field that is intialize to an ArrayList
7
     // instance field that is intialize to an ArrayList
8
     private List<Entry> entries = new ArrayList<>();
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
     @Override
18
     @Override
11
     public int size() {
19
     public int size() {
12
-        return -1;
20
+        return entries.size();
13
     }
21
     }
14
 
22
 
15
     @Override
23
     @Override
16
-    public boolean isEmptry() {
24
+    public boolean isEmpty() {
25
+
26
+        if(size() == 0) {
27
+            return true;
28
+        }
29
+
17
         return false;
30
         return false;
18
     }
31
     }
19
 
32
 
20
     @Override
33
     @Override
21
     public Cart get(User key) {
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
         return null;
42
         return null;
24
     }
43
     }
25
 
44
 
26
     @Override
45
     @Override
27
     public void put(User key, Cart value) {
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
     @Override
60
     @Override
33
     public List<User> getKeys() {
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
     @Override
69
     @Override
39
     public List<Cart> getValues() {
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
     @Override
78
     @Override
45
     public Cart remove(User key) {
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
         return null;
88
         return null;
47
     }
89
     }
48
 }
90
 }

+ 58
- 6
src/main/java/org/zipcoder/store/MyHashMap.java Просмотреть файл

17
         }
17
         }
18
     }
18
     }
19
 
19
 
20
+    public List<Entry>[] getEntries() {
21
+        return entries;
22
+    }
23
+
20
     @Override
24
     @Override
21
     public int size() {
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
     @Override
35
     @Override
26
-    public boolean isEmptry() {
36
+    public boolean isEmpty() {
37
+
38
+        if(size() == 0) {
39
+            return true;
40
+        }
41
+
27
         return false;
42
         return false;
28
     }
43
     }
29
 
44
 
30
     @Override
45
     @Override
31
     public Cart get(User key) {
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
         return null;
53
         return null;
33
     }
54
     }
34
 
55
 
35
     @Override
56
     @Override
36
     public void put(User key, Cart value) {
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
     @Override
72
     @Override
41
     public List<User> getKeys() {
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
     @Override
83
     @Override
46
     public List<Cart> getValues() {
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
     @Override
94
     @Override
51
     public Cart remove(User key) {
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
     private int bucketIndex(User key){
107
     private int bucketIndex(User key){
56
         return Math.abs(key.hashCode()) % entries.length;
108
         return Math.abs(key.hashCode()) % entries.length;
57
     }
109
     }

+ 3
- 1
src/main/java/org/zipcoder/store/MyMap.java Просмотреть файл

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
      */
14
      */
15
-    public boolean isEmptry();
15
+    public boolean isEmpty();
16
 
16
 
17
     /**
17
     /**
18
      * Return the cart associated with the user
18
      * Return the cart associated with the user
44
      * @return The cart associated with the user
44
      * @return The cart associated with the user
45
      */
45
      */
46
     public Cart remove(User key);
46
     public Cart remove(User key);
47
+
48
+
47
 }
49
 }

+ 33
- 0
src/main/java/org/zipcoder/store/User.java Просмотреть файл

37
     public void setName(String name) {
37
     public void setName(String name) {
38
         this.name = name;
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
 
116
 
117
     @Test
117
     @Test
118
     public void testIsEmpty_WithoutEntry(){
118
     public void testIsEmpty_WithoutEntry(){
119
-        Assert.assertTrue(myMap.isEmptry());
119
+        Assert.assertTrue(myMap.isEmpty());
120
     }
120
     }
121
 
121
 
122
     @Test
122
     @Test
129
         myMap.put(user1, cart1);
129
         myMap.put(user1, cart1);
130
 
130
 
131
         // Then
131
         // Then
132
-        Assert.assertFalse(myMap.isEmptry());
132
+        Assert.assertFalse(myMap.isEmpty());
133
     }
133
     }
134
 
134
 
135
     @Test
135
     @Test
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
 }

+ 54
- 54
src/test/java/org/zipcoder/store/MyHashMapTest.java Просмотреть файл

116
 
116
 
117
     @Test
117
     @Test
118
     public void testIsEmpty_WithoutEntry(){
118
     public void testIsEmpty_WithoutEntry(){
119
-        Assert.assertTrue(myMap.isEmptry());
119
+        Assert.assertTrue(myMap.isEmpty());
120
     }
120
     }
121
 
121
 
122
     @Test
122
     @Test
129
         myMap.put(user1, cart1);
129
         myMap.put(user1, cart1);
130
 
130
 
131
         // Then
131
         // Then
132
-        Assert.assertFalse(myMap.isEmptry());
132
+        Assert.assertFalse(myMap.isEmpty());
133
     }
133
     }
134
 
134
 
135
     @Test
135
     @Test
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
 }

+ 9
- 0
src/test/java/org/zipcoder/store/UserTest.java Просмотреть файл

65
     }
65
     }
66
 
66
 
67
     @Test
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
     public void testEqual_withSameNameDifferentId(){
77
     public void testEqual_withSameNameDifferentId(){
69
         String name = "Asher";
78
         String name = "Asher";
70
         User user1 = new User(92, name);
79
         User user1 = new User(92, name);