Trinh Tong 6 gadus atpakaļ
vecāks
revīzija
c3f946382f

+ 36
- 10
src/main/java/org/zipcoder/store/MyHashMap.java Parādīt failu

@@ -17,6 +17,10 @@ public class MyHashMap implements MyMap{
17 17
         }
18 18
     }
19 19
 
20
+    public List<Entry>[] getEntries() {
21
+        return entries;
22
+    }
23
+
20 24
     @Override
21 25
     public int size() {
22 26
         int count = 0;
@@ -33,25 +37,47 @@ public class MyHashMap implements MyMap{
33 37
 
34 38
     @Override
35 39
     public Cart get(User key) {
36
-        int bucket = bucketIndex(key);
37
-        for (Entry e: entries[bucket]) {
38
-            if (e.getKey().equals(key)) {
39
-                return e.getValue();
40
+            int bucket = bucketIndex(key);
41
+
42
+            for (Entry e : getEntries()[bucket]) {
43
+                if (e.getKey().equals(key)) {
44
+                    return e.getValue();
45
+                }
40 46
             }
41
-        }
42 47
         return null;
43 48
     }
44 49
 
45 50
     @Override
46 51
     public void put(User key, Cart value) {
47
-        entries[bucketIndex(key)].add(new Entry(key, value));
52
+
53
+//        if(get(key) != null) {
54
+//            for (Entry e: getEntries()[bucketIndex(key)]) {
55
+//                if (e.getKey().equals(key)) {
56
+//                    e.setValue(value);
57
+//                }
58
+//            }
59
+//        } else {
60
+//            getEntries()[bucketIndex(key)].add(new Entry(key, value));
61
+//        }
62
+
63
+        if (!getKeys().contains(key)) {
64
+            entries[bucketIndex(key)].add(new Entry(key, value));
65
+        } else {
66
+            for (Entry e: entries[bucketIndex(key)]) {
67
+                if (e.getKey().equals(key)) {
68
+                    e.setValue(value);
69
+                }
70
+            }
71
+        }
72
+
73
+
48 74
     }
49 75
 
50 76
     @Override
51 77
     public List<User> getKeys() {
52 78
         List<User> keys = new ArrayList<>();
53
-        for (List<Entry> list:entries) {
54
-            for (Entry e: list )
79
+        for (List<Entry> list: getEntries()) {
80
+            for (Entry e: list)
55 81
                 keys.add(e.getKey());
56 82
         }
57 83
         return keys;
@@ -75,10 +101,10 @@ public class MyHashMap implements MyMap{
75 101
         while (iterator.hasNext()) {
76 102
             e = iterator.next();
77 103
             if (e.getKey().equals(key)) {
78
-                entries[bucket].remove(e);
104
+                getEntries()[bucket].remove(e);
105
+                break;
79 106
             }
80 107
         }
81
-
82 108
         return e.getValue();
83 109
     }
84 110
 

+ 8
- 6
src/main/java/org/zipcoder/store/User.java Parādīt failu

@@ -22,12 +22,14 @@ public class User {
22 22
         this.name = name;
23 23
     }
24 24
 
25
-    public boolean equals(User compare) {
26
-        if (this.id == compare.getId() && this.name.equals(compare.getName())) {
27
-            return true;
28
-        }
29
-        return false;
30
-    }
25
+     @Override
26
+    public boolean equals(Object o) {
27
+                if (this == o) return true;
28
+                if (o == null || getClass() != o.getClass()) return false;
29
+                User user = (User) o;
30
+                return id == user.id &&
31
+                                Objects.equals(name, user.name);
32
+           }
31 33
 
32 34
     public int hashCode() {
33 35
         Objects.hash(getId(), getName());