Jared Norris преди 6 години
родител
ревизия
5736879aeb

+ 9
- 0
src/main/java/org/zipcoder/store/Cart.java Целия файл

@@ -12,4 +12,13 @@ public class Cart {
12 12
     public long getId(){
13 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 Целия файл

@@ -4,45 +4,67 @@ import java.util.ArrayList;
4 4
 import java.util.List;
5 5
 
6 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 8
     private List<Entry> entries = new ArrayList<>();
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
-    public boolean isEmptry() {
17
-        return false;
16
+    public boolean isEmpty() {
17
+        return size() == 0;
18 18
     }
19 19
 
20 20
     @Override
21 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 25
         return null;
24 26
     }
25 27
 
26 28
     @Override
27 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 41
     @Override
33 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 50
     @Override
39 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 59
     @Override
45 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 Целия файл

@@ -19,32 +19,59 @@ public class MyHashMap implements MyMap{
19 19
 
20 20
     @Override
21 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 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 40
     @Override
31 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 49
     @Override
36 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 55
     @Override
41 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 66
     @Override
46 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 77
     @Override

+ 1
- 1
src/main/java/org/zipcoder/store/MyMap.java Целия файл

@@ -12,7 +12,7 @@ public interface MyMap {
12 12
     /**
13 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 18
      * Return the cart associated with the user

+ 9
- 0
src/main/java/org/zipcoder/store/User.java Целия файл

@@ -37,4 +37,13 @@ public class User {
37 37
     public void setName(String name) {
38 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 Целия файл

@@ -116,7 +116,7 @@ public class ListMapTest {
116 116
 
117 117
     @Test
118 118
     public void testIsEmpty_WithoutEntry(){
119
-        Assert.assertTrue(myMap.isEmptry());
119
+        Assert.assertTrue(myMap.isEmpty());
120 120
     }
121 121
 
122 122
     @Test
@@ -124,12 +124,11 @@ public class ListMapTest {
124 124
         // Given
125 125
         User user1 = new User("Monica");
126 126
         Cart cart1 = new Cart(45);
127
-
128 127
         // When
129 128
         myMap.put(user1, cart1);
130 129
 
131 130
         // Then
132
-        Assert.assertFalse(myMap.isEmptry());
131
+        Assert.assertFalse(myMap.isEmpty());
133 132
     }
134 133
 
135 134
     @Test
@@ -194,8 +193,8 @@ public class ListMapTest {
194 193
 //            Assert.assertEquals(actualCart, expectedCart);
195 194
 //        }
196 195
 //    }
197
-
198
-//    BONUS
196
+//
197
+//    //BONUS
199 198
 //    @Test
200 199
 //    public void testRemove(){
201 200
 //        // Given

+ 3
- 3
src/test/java/org/zipcoder/store/MyHashMapTest.java Целия файл

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