Jared Norris 6 yıl önce
ebeveyn
işleme
5736879aeb

+ 9
- 0
src/main/java/org/zipcoder/store/Cart.java Dosyayı Görüntüle

12
     public long getId(){
12
     public long getId(){
13
         return this.id;
13
         return this.id;
14
     }
14
     }
15
+
16
+    public boolean equals(Cart c) {
17
+        return this.hashCode() == c.hashCode();
18
+    }
19
+
20
+    @Override
21
+    public int hashCode() {
22
+        return Objects.hash(this.id);
23
+    }
15
 }
24
 }

+ 33
- 11
src/main/java/org/zipcoder/store/ListMap.java Dosyayı Görüntüle

4
 import java.util.List;
4
 import java.util.List;
5
 
5
 
6
 public class ListMap implements MyMap {
6
 public class ListMap implements MyMap {
7
-    // instance field that is intialize to an ArrayList
7
+    // instance field that is initialize to an ArrayList
8
     private List<Entry> entries = new ArrayList<>();
8
     private List<Entry> entries = new ArrayList<>();
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() {
17
-        return false;
16
+    public boolean isEmpty() {
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
+        for (Entry e : entries) {
23
+            if (e.getKey().equals(key)) return e.getValue();
24
+        }
23
         return null;
25
         return null;
24
     }
26
     }
25
 
27
 
26
     @Override
28
     @Override
27
     public void put(User key, Cart value) {
29
     public void put(User key, Cart value) {
28
-        // add the key, value entry to the entries list
30
+        for (Entry e : entries) {
31
+            if (e.getKey() == key) {
32
+                e.setValue(value);
33
+                return;
34
+            }
35
+        }
36
+        Entry entry = new Entry(key, value);
37
+        entries.add(entry);
29
     }
38
     }
30
 
39
 
31
 
40
 
32
     @Override
41
     @Override
33
     public List<User> getKeys() {
42
     public List<User> getKeys() {
34
-        // get all the keys in the map
35
-        return null;
43
+        List<User> users = new ArrayList<>();
44
+        for(Entry e : entries){
45
+            users.add(e.getKey());
46
+        }
47
+        return users;
36
     }
48
     }
37
 
49
 
38
     @Override
50
     @Override
39
     public List<Cart> getValues() {
51
     public List<Cart> getValues() {
40
-        // get all the values in the map
41
-        return null;
52
+        List<Cart> carts = new ArrayList<>();
53
+        for (Entry e : entries) {
54
+            carts.add(e.getValue());
55
+        }
56
+        return carts;
42
     }
57
     }
43
 
58
 
44
     @Override
59
     @Override
45
     public Cart remove(User key) {
60
     public Cart remove(User key) {
46
-        return null;
61
+        Cart cart = null;
62
+        for (Entry e : entries) {
63
+            if (e.getKey() == key) {
64
+                cart = e.getValue();
65
+                e.setValue(null);
66
+            }
67
+        }
68
+        return cart;
47
     }
69
     }
48
 }
70
 }

+ 34
- 7
src/main/java/org/zipcoder/store/MyHashMap.java Dosyayı Görüntüle

19
 
19
 
20
     @Override
20
     @Override
21
     public int size() {
21
     public int size() {
22
-        return -1;
22
+        int count = 0;
23
+        if (!isEmpty()) {
24
+            for (int i = 0; i < entries.length; i++) {
25
+                if (entries[i].size() > 0) count++;
26
+            }
27
+            return count;
28
+        } else return 0;
23
     }
29
     }
24
 
30
 
25
     @Override
31
     @Override
26
-    public boolean isEmptry() {
27
-        return false;
32
+    public boolean isEmpty() {
33
+        int count = 0;
34
+        for (int i = 0; i < entries.length; i++) {
35
+            if (entries[i].size() != 0) count++;
36
+        }
37
+        return count == 0;
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 cart = null;
43
+        for (Entry e : entries[bucketIndex(key)]) {
44
+            cart = e.getValue();
45
+        }
46
+        return cart;
33
     }
47
     }
34
 
48
 
35
     @Override
49
     @Override
36
     public void put(User key, Cart value) {
50
     public void put(User key, Cart value) {
37
-
51
+        Entry e = new Entry(key, value);
52
+        entries[bucketIndex(key)].add(e);
38
     }
53
     }
39
 
54
 
40
     @Override
55
     @Override
41
     public List<User> getKeys() {
56
     public List<User> getKeys() {
42
-        return null;
57
+        List<User> keys = new ArrayList<>();
58
+        for (int i = 0; i < entries.length; i++) {
59
+            for (Entry e : entries[i]) {
60
+                keys.add(e.getKey());
61
+            }
62
+        }
63
+        return keys;
43
     }
64
     }
44
 
65
 
45
     @Override
66
     @Override
46
     public List<Cart> getValues() {
67
     public List<Cart> getValues() {
47
-        return null;
68
+        List<Cart> values = new ArrayList<>();
69
+        for (int i = 0; i < entries.length; i++) {
70
+            for (Entry e : entries[i]) {
71
+                values.add(e.getValue());
72
+            }
73
+        }
74
+        return values;
48
     }
75
     }
49
 
76
 
50
     @Override
77
     @Override

+ 1
- 1
src/main/java/org/zipcoder/store/MyMap.java Dosyayı Görüntüle

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

+ 9
- 0
src/main/java/org/zipcoder/store/User.java Dosyayı Görüntüle

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 u) {
42
+        return this.hashCode() == u.hashCode();
43
+    }
44
+
45
+    @Override
46
+    public int hashCode() {
47
+        return Objects.hash(this.id, this.name);
48
+    }
40
 }
49
 }

+ 4
- 5
src/test/java/org/zipcoder/store/ListMapTest.java Dosyayı Görüntüle

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
124
         // Given
124
         // Given
125
         User user1 = new User("Monica");
125
         User user1 = new User("Monica");
126
         Cart cart1 = new Cart(45);
126
         Cart cart1 = new Cart(45);
127
-
128
         // When
127
         // When
129
         myMap.put(user1, cart1);
128
         myMap.put(user1, cart1);
130
 
129
 
131
         // Then
130
         // Then
132
-        Assert.assertFalse(myMap.isEmptry());
131
+        Assert.assertFalse(myMap.isEmpty());
133
     }
132
     }
134
 
133
 
135
     @Test
134
     @Test
194
 //            Assert.assertEquals(actualCart, expectedCart);
193
 //            Assert.assertEquals(actualCart, expectedCart);
195
 //        }
194
 //        }
196
 //    }
195
 //    }
197
-
198
-//    BONUS
196
+//
197
+//    //BONUS
199
 //    @Test
198
 //    @Test
200
 //    public void testRemove(){
199
 //    public void testRemove(){
201
 //        // Given
200
 //        // Given

+ 3
- 3
src/test/java/org/zipcoder/store/MyHashMapTest.java Dosyayı Görüntüle

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
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
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
178
 //    @Test
179
 //    public void testPutAndGet_WithManyItems(){
179
 //    public void testPutAndGet_WithManyItems(){
180
 //        // Given
180
 //        // Given