Jason Gibbs 6 years ago
parent
commit
d62b04ae83

+ 16
- 0
src/main/java/org/zipcoder/store/Cart.java View File

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 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 View File

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;
18
-    }
16
+    public boolean isEmptry() { return entries.isEmpty(); }
19
 
17
 
20
     @Override
18
     @Override
21
     public Cart get(User key) {
19
     public Cart get(User key) {
22
         // find the cart associate with the key
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
     @Override
30
     @Override
27
     public void put(User key, Cart value) {
31
     public void put(User key, Cart value) {
28
         // add the key, value entry to the entries list
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
     @Override
38
     @Override
33
     public List<User> getKeys() {
39
     public List<User> getKeys() {
34
         // get all the keys in the map
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
     @Override
49
     @Override
39
     public List<Cart> getValues() {
50
     public List<Cart> getValues() {
40
         // get all the values in the map
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
     @Override
57
     @Override
45
     public Cart remove(User key) {
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 View File

8
 
8
 
9
     // instance field that can only be set once. It is different for every Object
9
     // instance field that can only be set once. It is different for every Object
10
     private final List<Entry>[] entries;
10
     private final List<Entry>[] entries;
11
+    private int size;
11
 
12
 
12
     public MyHashMap(){
13
     public MyHashMap(){
14
+    // creates an array of length 15
13
         entries = new List[BUCKET_SIZE];
15
         entries = new List[BUCKET_SIZE];
14
-
16
+    // populates array with 15 ArrayLists
15
         for(int i = 0; i < BUCKET_SIZE; i++) {
17
         for(int i = 0; i < BUCKET_SIZE; i++) {
16
             entries[i] = new ArrayList<>();
18
             entries[i] = new ArrayList<>();
17
         }
19
         }
19
 
21
 
20
     @Override
22
     @Override
21
     public int size() {
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
     @Override
30
     @Override
26
     public boolean isEmptry() {
31
     public boolean isEmptry() {
27
-        return false;
32
+        return size == 0;
28
     }
33
     }
29
 
34
 
30
     @Override
35
     @Override
31
     public Cart get(User key) {
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
     @Override
48
     @Override
36
     public void put(User key, Cart value) {
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
     @Override
55
     @Override
41
     public List<User> getKeys() {
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
     @Override
64
     @Override
46
     public List<Cart> getValues() {
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
     @Override
73
     @Override
51
     public Cart remove(User key) {
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
     private int bucketIndex(User key){
86
     private int bucketIndex(User key){

+ 17
- 0
src/main/java/org/zipcoder/store/User.java View File

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 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
 }