Jason Gibbs пре 6 година
родитељ
комит
d62b04ae83

+ 16
- 0
src/main/java/org/zipcoder/store/Cart.java Прегледај датотеку

@@ -12,4 +12,20 @@ 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 instanceof Cart)) return false;
20
+        Cart cart = (Cart) o;
21
+        return getId() == cart.getId();
22
+    }
23
+
24
+    @Override
25
+    public int hashCode() {
26
+
27
+        return Objects.hash(getId());
28
+    }
29
+
30
+
15 31
 }

+ 28
- 8
src/main/java/org/zipcoder/store/ListMap.java Прегледај датотеку

@@ -9,40 +9,60 @@ public class ListMap implements MyMap {
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;
18
-    }
16
+    public boolean isEmptry() { return entries.isEmpty(); }
19 17
 
20 18
     @Override
21 19
     public Cart get(User key) {
22 20
         // find the cart associate with the key
23
-        return null;
21
+        Cart rightCart = null;
22
+        for (Entry entry : entries) {
23
+            if (entry.getKey() == key) {
24
+                rightCart = entry.getValue();
25
+            }
26
+        }
27
+        return rightCart;
24 28
     }
25 29
 
26 30
     @Override
27 31
     public void put(User key, Cart value) {
28 32
         // add the key, value entry to the entries list
33
+        Entry entry = new Entry(key, value);
34
+        entries.add(entry);
29 35
     }
30 36
 
31 37
 
32 38
     @Override
33 39
     public List<User> getKeys() {
34 40
         // get all the keys in the map
35
-        return null;
41
+        List<User> keys = new ArrayList<>();
42
+        entries.forEach(entry -> keys.add(entry.getKey()));
43
+//        for (Entry entry : entries) {
44
+//            keys.add(entry.getKey());
45
+//        }
46
+        return keys;
36 47
     }
37 48
 
38 49
     @Override
39 50
     public List<Cart> getValues() {
40 51
         // get all the values in the map
41
-        return null;
52
+        List<Cart> values = new ArrayList<>();
53
+        entries.forEach(entry -> values.add(entry.getValue()));
54
+        return values;
42 55
     }
43 56
 
44 57
     @Override
45 58
     public Cart remove(User key) {
46
-        return null;
59
+        Cart rightValue = null;
60
+        for (Entry entry : entries) {
61
+            if (entry.getKey() == key) {
62
+                rightValue = entry.getValue();
63
+                entries.remove(entry);
64
+            }
65
+        }
66
+        return rightValue;
47 67
     }
48 68
 }

+ 39
- 8
src/main/java/org/zipcoder/store/MyHashMap.java Прегледај датотеку

@@ -8,10 +8,12 @@ public class MyHashMap implements MyMap{
8 8
 
9 9
     // instance field that can only be set once. It is different for every Object
10 10
     private final List<Entry>[] entries;
11
+    private int size;
11 12
 
12 13
     public MyHashMap(){
14
+    // creates an array of length 15
13 15
         entries = new List[BUCKET_SIZE];
14
-
16
+    // populates array with 15 ArrayLists
15 17
         for(int i = 0; i < BUCKET_SIZE; i++) {
16 18
             entries[i] = new ArrayList<>();
17 19
         }
@@ -19,37 +21,66 @@ public class MyHashMap implements MyMap{
19 21
 
20 22
     @Override
21 23
     public int size() {
22
-        return -1;
24
+        for (int i = 0; i < entries.length; i++) {
25
+            size += entries[i].size();
26
+        }
27
+        return size;
23 28
     }
24 29
 
25 30
     @Override
26 31
     public boolean isEmptry() {
27
-        return false;
32
+        return size == 0;
28 33
     }
29 34
 
30 35
     @Override
31 36
     public Cart get(User key) {
32
-        return null;
37
+        Cart rightCart = null;
38
+        for (List<Entry> e : entries) {
39
+            for (Entry entry : e) {
40
+                if(entry.getKey().equals(key) || entry.getKey().getName().equals(key.getName())) {
41
+                    rightCart = entry.getValue();
42
+                }
43
+            }
44
+        }
45
+        return rightCart;
33 46
     }
34 47
 
35 48
     @Override
36 49
     public void put(User key, Cart value) {
37
-
50
+        Entry entry = new Entry(key, value);
51
+        entries[this.size++].add(entry);
52
+        //this.size++;
38 53
     }
39 54
 
40 55
     @Override
41 56
     public List<User> getKeys() {
42
-        return null;
57
+        List<User> keyList = new ArrayList<>();
58
+        List<List<Entry>> lambdaList = new ArrayList<>(Arrays.asList(entries));
59
+
60
+        lambdaList.forEach(entries1 -> entries1.forEach(entry -> keyList.add(entry.getKey())));
61
+        return keyList;
43 62
     }
44 63
 
45 64
     @Override
46 65
     public List<Cart> getValues() {
47
-        return null;
66
+        List<Cart> valueList = new ArrayList<>();
67
+        List<List<Entry>> lambdaList = new ArrayList<>(Arrays.asList(entries));
68
+
69
+        lambdaList.forEach(entries1 -> entries1.forEach(entry -> valueList.add(entry.getValue())));
70
+        return valueList;
48 71
     }
49 72
 
50 73
     @Override
51 74
     public Cart remove(User key) {
52
-        return null;
75
+        Cart rightCart = null;
76
+        for (List<Entry> list : entries) {
77
+            for (Entry entry : list) {
78
+                if (entry.getKey() == key) {
79
+                    rightCart = entry.getValue();
80
+                }
81
+            }
82
+        }
83
+        return rightCart;
53 84
     }
54 85
 
55 86
     private int bucketIndex(User key){

+ 17
- 0
src/main/java/org/zipcoder/store/User.java Прегледај датотеку

@@ -37,4 +37,21 @@ public class User {
37 37
     public void setName(String name) {
38 38
         this.name = name;
39 39
     }
40
+
41
+    @Override
42
+    public boolean equals(Object obj) {
43
+        boolean isEqual = false;
44
+        if (this.getClass() == obj.getClass()) {
45
+            //obj = new User();
46
+            if (this.getId() == ((User) obj).getId() && this.getName() == ((User) obj).getName()) {
47
+                isEqual = true;
48
+            }
49
+        }
50
+        return isEqual;
51
+    }
52
+
53
+    @Override
54
+    public int hashCode() {
55
+        return Objects.hash(this.getId(), this.getName());
56
+    }
40 57
 }