Nhu Nguyen 6 years ago
commit
f42009eafb

+ 55
- 0
.gitignore View File

@@ -0,0 +1,55 @@
1
+# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
2
+# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
3
+
4
+# User-specific stuff:
5
+.idea/**
6
+.idea/**/tasks.xml
7
+.idea/dictionaries
8
+
9
+# Sensitive or high-churn files:
10
+.idea/**/dataSources/
11
+.idea/**/dataSources.ids
12
+.idea/**/dataSources.xml
13
+.idea/**/dataSources.local.xml
14
+.idea/**/sqlDataSources.xml
15
+.idea/**/dynamic.xml
16
+.idea/**/uiDesigner.xml
17
+
18
+# Gradle:
19
+.idea/**/gradle.xml
20
+.idea/**/libraries
21
+
22
+*.iml
23
+# CMake
24
+cmake-build-debug/
25
+
26
+# Mongo Explorer plugin:
27
+.idea/**/mongoSettings.xml
28
+
29
+## File-based project format:
30
+*.iws
31
+
32
+## Plugin-specific files:
33
+
34
+# IntelliJ
35
+/out/
36
+
37
+# mpeltonen/sbt-idea plugin
38
+.idea_modules/
39
+
40
+# JIRA plugin
41
+atlassian-ide-plugin.xml
42
+
43
+# Cursive Clojure plugin
44
+.idea/replstate.xml
45
+
46
+# Crashlytics plugin (for Android Studio and IntelliJ)
47
+com_crashlytics_export_strings.xml
48
+crashlytics.properties
49
+crashlytics-build.properties
50
+fabric.properties
51
+
52
+.project
53
+.classpath
54
+.settings
55
+target

+ 93
- 0
README.md View File

@@ -0,0 +1,93 @@
1
+# Store Lab
2
+* **Purpose** - to understand [HashMap](https://www.youtube.com/watch?v=shs0KM3wKv8), [objects](https://docs.oracle.com/javase/tutorial/java/concepts/object.html) and [data encapsulation](https://en.wikipedia.org/wiki/Data_encapsulation).
3
+
4
+* **Objective** - to create two type of maps
5
+
6
+## Part 1 - Equals & HashCode
7
+By default, every [Java object](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html) has an [equals](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals) and a [hashCode](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode) method. Java uses the object's address to calculate the [hashCode](https://en.wikipedia.org/wiki/Hash_function). By default, two objects are equal if they have the same address. We don't want this. Two objects are equal if they are the same class and have the same values.
8
+
9
+  1. User
10
+    1. Add an `equals` method to the `User` class. The equal method should return true only if the `id` and `name` of the user are the same. It return false otherwise.
11
+    2. Add a `hashCode` method by calling `Objects.hash()` and gives it the `id` and `name` (e.g. `Objects.hash(id, name)`)
12
+  2. Cart
13
+    1. Add an `equals` and `hashCode` methods with IntelliJ shortcut
14
+      1. Click `Command + N` and select `equals and hashCode`. Follow the wizard to generate the code.
15
+
16
+## Part 2 - ListMap
17
+
18
+
19
+
20
+
21
+This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language
22
+
23
+
24
+## Part 1 - ListMap
25
+In the `test` folder, there is a `ProductTest` with most of the tested commented out.
26
+  1. The first test `testConstruction_WithNoParams` checks to see if the `Product` class has an empty constructor. To test it, I created a new object with no parameter.
27
+  2. The second test `testGetSetName` checks to see if you can set and get the name of the product. In order to make this pass, I added a `String` field called `name` (line 5). Then I added a getter method `getName` (line 18-20) and a setter method `setName(String name)` (line 23-25).
28
+  3. The third test `testConstruction_WithName` checks if the `Product` class has a constructor that takes a String. Notice we can have more than one constructor. When I create a new Product with a string `new Product("T-shirt")`, I set that string to the field `name` so when I called `getName`, I get the string that I passed into the constructor.
29
+  4. Continue to uncomment the test and write the code to make it pass
30
+  5. For the product description, it should be in this format `title (Color: color, Size: size)`
31
+    - ex: `Hat (Color: Yellow, Size: LG)`
32
+
33
+## Part 2 - Cart
34
+A cart has an array of Product.
35
+  1. Open the `CartTest` and uncomment line 9-13. To make the test pass:
36
+    - Create a new class named `Cart`
37
+    - Create a new constructor that takes an array of Product
38
+    - Run the test, it should pass
39
+  2. Create a getter for Product to make `testGetProducts` test pass
40
+  3. To make `testGetSetDiscount` test pass:
41
+    - Create a new decimal field called `discount`
42
+    - Create a getter and setter for the field
43
+    - Run the test
44
+  4. To get `getProductTotal` tests to pass, you need add all the product prices together and return the result.
45
+  5. To get the total, you need to add the total of all the products minus the discount. For example, if a product costs $100, and the discount is 20% (0.20), then the total = 100 - (100 * 0.20) = 80
46
+
47
+## Part 3 - CreditCard
48
+The goal of this exercise is to know how to create and test a class.
49
+  1. In the test folder, create a new test class called `CreditCardTest`
50
+  2. Create a new class called `CreditCard`
51
+  2. Create a test case to get and set the `cardHolderName` of the type `String`
52
+      - Create getter and setter methods in the `CreditCard` class
53
+      - Add a field called cardHolderName
54
+  2. Do the same for the following fields:
55
+    - `String number`
56
+    - `int expiredMonth`
57
+    - `int expiredYear`
58
+  3. Create a getter and setter for each field
59
+  4. Create a test for `getDescription`
60
+    - `getDescription` should return `[cardHolderName] [last 4 digit of the number] [expiredMonth]/[expiredYear]`
61
+      - ex: `Tia Mowry 4551 10/2019`
62
+  5. Add a `CreditCard` field to the `Cart` class
63
+    - Add a getter and setter for the `creditCard` field
64
+
65
+## Part 4 - Product as an ArrayList - BONUS
66
+The goal of this exercise is to change the `products` array in the `Cart` class to an ArrayList.
67
+  1. In the `CartTest`, change the `products` array to an `ArrayList`
68
+
69
+    ```java
70
+      ArrayList<Product> products = new ArrayList();
71
+      products.add(new Product("Shirt", 15.01));
72
+    ```
73
+
74
+  2. Add this test to the `CartTest` class
75
+  ```java
76
+    @Test
77
+    public void testAddToCard(){
78
+        // Given
79
+        ArrayList<Product> products = new ArrayList();
80
+        Cart cart = new Cart(products);
81
+
82
+        // When
83
+        Product shirt = new Product("Shirt", 1.99);
84
+        cart.addProduct(shirt);
85
+
86
+        // Then
87
+        double expectedTotal = 1.99;
88
+        double actualTotal = cart.getProductTotal();
89
+        Assert.assertEquals(expectedTotal, actualTotal, DELTA);
90
+    }
91
+  ```
92
+    - Add a `addProduct` method to the `Cart` class
93
+    - Run the test. It should pass.

+ 32
- 0
pom.xml View File

@@ -0,0 +1,32 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <modelVersion>4.0.0</modelVersion>
6
+
7
+    <groupId>org.zipcoder</groupId>
8
+    <artifactId>Store-Hash</artifactId>
9
+    <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.8</source>
17
+                    <target>1.8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
22
+    <dependencies>
23
+        <dependency>
24
+            <groupId>junit</groupId>
25
+            <artifactId>junit</artifactId>
26
+            <version>4.12</version>
27
+            <scope>test</scope>
28
+        </dependency>
29
+    </dependencies>
30
+
31
+
32
+</project>

+ 49
- 0
src/test/java/org/zipcoder/store/CartTest.java View File

@@ -0,0 +1,49 @@
1
+package org.zipcoder.store;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CartTest {
7
+
8
+    @Test
9
+    public void testGetSetId(){
10
+        // Given
11
+        long expectedId = 931;
12
+        Cart cart1 = new Cart(expectedId);
13
+
14
+        // When
15
+        long actualId = cart1.getId();
16
+
17
+        // Then
18
+        Assert.assertEquals(expectedId, actualId);
19
+    }
20
+
21
+    @Test
22
+    public void testEquals_CartWithTheSameIdAreEquals(){
23
+        // Given
24
+        long id = 111;
25
+        Cart cart1 = new Cart(id);
26
+        Cart copyCart1 = new Cart(id);
27
+
28
+        // When
29
+        boolean equality = cart1.equals(copyCart1);
30
+
31
+        // Then
32
+        Assert.assertTrue(equality);
33
+    }
34
+
35
+    @Test
36
+    public void testHashCode_CartWithTheSameIdAreHaveTheSameHash(){
37
+        // Given
38
+        long id = 111;
39
+        Cart cart1 = new Cart(id);
40
+        Cart copyCart1 = new Cart(id);
41
+
42
+        // When
43
+        int cart1Hash = cart1.hashCode();
44
+        int copyCart1Hash = copyCart1.hashCode();
45
+
46
+        // Then
47
+        Assert.assertEquals(cart1Hash, copyCart1Hash);
48
+    }
49
+}

+ 227
- 0
src/test/java/org/zipcoder/store/ListMapTest.java View File

@@ -0,0 +1,227 @@
1
+package org.zipcoder.store;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import java.util.List;
8
+
9
+public class ListMapTest {
10
+    private MyMap myMap;
11
+
12
+    @Before
13
+    public void setup(){
14
+        myMap = new ListMap();
15
+    }
16
+
17
+    @Test
18
+    public void testPutAndGet_WithOneItem(){
19
+        User user = new User("Tiana");
20
+        Cart cart = new Cart(1);
21
+
22
+        // When
23
+        myMap.put(user, cart);
24
+
25
+        // Then
26
+        Cart actualCart = myMap.get(user);
27
+        Assert.assertEquals(cart, actualCart);
28
+    }
29
+
30
+    @Test
31
+    public void testPutAndGet_WithTwoItems(){
32
+        User tiana = new User("Tiana");
33
+        Cart tianasCart = new Cart(1134);
34
+
35
+        User john = new User("John");
36
+        Cart johnsCart = new Cart(42);
37
+
38
+        // When
39
+        myMap.put(john, johnsCart);
40
+        myMap.put(tiana, tianasCart);
41
+
42
+        // Then
43
+        Cart actualJohnsCart = myMap.get(john);
44
+        Assert.assertEquals(johnsCart, actualJohnsCart);
45
+
46
+        Cart actualTianasCart = myMap.get(tiana);
47
+        Assert.assertEquals(tianasCart, actualTianasCart);
48
+    }
49
+
50
+    @Test
51
+    public void testPutAndGet_UpdateCart(){
52
+        User tiana = new User("Tiana");
53
+        Cart tianasCart = new Cart(11);
54
+        myMap.put(tiana, tianasCart);
55
+
56
+        // When
57
+        Cart updatedCart = new Cart(99);
58
+        myMap.put(tiana, updatedCart);
59
+
60
+        // Then
61
+        Cart actualCart = myMap.get(tiana);
62
+        Assert.assertEquals(updatedCart, actualCart);
63
+    }
64
+
65
+    @Test
66
+    public void testGetWithoutPut(){
67
+        // Given
68
+        User user = new User("Tiana");
69
+
70
+        // When
71
+        Cart actualCart = myMap.get(user);
72
+
73
+        // Then
74
+        Assert.assertNull(actualCart);
75
+    }
76
+
77
+    @Test
78
+    public void testPutAndGet_WithDifferentKeyInstance(){
79
+        // Given
80
+        String name = "Tiana";
81
+        User tiana = new User(name);
82
+        Cart cart = new Cart(76);
83
+
84
+        // When
85
+        myMap.put(tiana, cart);
86
+        User cloneTiana = new User(name);
87
+        Cart actualCart = myMap.get(cloneTiana);
88
+
89
+        // Then
90
+        Assert.assertEquals(cart, actualCart);
91
+    }
92
+
93
+    @Test
94
+    public void testSize_WithNoEntry(){
95
+        int expectedSize = 0;
96
+        Assert.assertEquals(expectedSize, myMap.size());
97
+    }
98
+
99
+    @Test
100
+    public void testSize_WithEntries(){
101
+        // Given
102
+        User user1 = new User("Monica");
103
+        Cart cart1 = new Cart(45);
104
+
105
+        User user2 = new User("Quincy");
106
+        Cart cart2 = new Cart(78);
107
+
108
+        // When
109
+        myMap.put(user1, cart1);
110
+        myMap.put(user2, cart2);
111
+
112
+        // Then
113
+        int expectedSize = 2;
114
+        Assert.assertEquals(expectedSize, myMap.size());
115
+    }
116
+
117
+    @Test
118
+    public void testIsEmpty_WithoutEntry(){
119
+        Assert.assertTrue(myMap.isEmptry());
120
+    }
121
+
122
+    @Test
123
+    public void testisEmpty_WithEntries(){
124
+        // Given
125
+        User user1 = new User("Monica");
126
+        Cart cart1 = new Cart(45);
127
+
128
+        // When
129
+        myMap.put(user1, cart1);
130
+
131
+        // Then
132
+        Assert.assertFalse(myMap.isEmptry());
133
+    }
134
+
135
+    @Test
136
+    public void testGetKeys(){
137
+        // Given
138
+        User user1 = new User("Zeke");
139
+        Cart cart1 = new Cart(7);
140
+
141
+        User user2 = new User("Nona");
142
+        Cart cart2 = new Cart(29);
143
+
144
+        // When
145
+        myMap.put(user1, cart1);
146
+        myMap.put(user2, cart2);
147
+
148
+        // Then
149
+        List<User> keys = myMap.getKeys();
150
+        int expectedKeySize = 2;
151
+        Assert.assertEquals(expectedKeySize, keys.size());
152
+        Assert.assertTrue(keys.contains(user1));
153
+        Assert.assertTrue(keys.contains(user2));
154
+    }
155
+
156
+    @Test
157
+    public void testGetValues(){
158
+        // Given
159
+        User user1 = new User("Camille");
160
+        Cart cart1 = new Cart(433);
161
+
162
+        User user2 = new User("Nathan");
163
+        Cart cart2 = new Cart(71);
164
+
165
+        // When
166
+        myMap.put(user1, cart1);
167
+        myMap.put(user2, cart2);
168
+
169
+        // Then
170
+        List<Cart> values = myMap.getValues();
171
+        int expectedKeySize = 2;
172
+        Assert.assertEquals(expectedKeySize, values.size());
173
+        Assert.assertTrue(values.contains(cart1));
174
+        Assert.assertTrue(values.contains(cart2));
175
+    }
176
+
177
+    @Test
178
+    public void testPutAndGet_WithManyItems(){
179
+        // Given
180
+        for (int i = 0; i < 70000; i++) {
181
+            myMap.put(new User(i), new Cart(i));
182
+        }
183
+
184
+
185
+        for (int i = 0; i < 70000; i++) {
186
+            // When
187
+            Cart actualCart = myMap.get(new User(i));
188
+            Cart expectedCart = new Cart(i);
189
+
190
+            // Then
191
+            Assert.assertEquals(actualCart, expectedCart);
192
+        }
193
+    }
194
+
195
+    @Test
196
+    public void testRemove(){
197
+        // Given
198
+        User user1 = new User("Lena");
199
+        Cart cart1 = new Cart(11);
200
+
201
+        User user2 = new User("Jamal");
202
+        Cart cart2 = new Cart(21);
203
+
204
+        User user3 = new User("Kelvin");
205
+        Cart cart3 = new Cart(31);
206
+
207
+        // When
208
+        myMap.put(user1, cart1);
209
+        myMap.put(user2, cart2);
210
+        myMap.put(user3, cart3);
211
+
212
+        // Then
213
+        Cart actualCart = myMap.remove(user2);
214
+
215
+        Assert.assertEquals(cart2, actualCart);
216
+        Assert.assertNull(myMap.get(user2));
217
+    }
218
+
219
+    @Test
220
+    public void testRemove_CartNotInTheMap(){
221
+        // Given
222
+        User user1 = new User("Lena");
223
+
224
+        // When & Then
225
+        Assert.assertNull(myMap.remove(user1));
226
+    }
227
+}

+ 227
- 0
src/test/java/org/zipcoder/store/MyHashMapTest.java View File

@@ -0,0 +1,227 @@
1
+package org.zipcoder.store;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import java.util.List;
8
+
9
+public class MyHashMapTest {
10
+    private MyMap myMap;
11
+
12
+    @Before
13
+    public void setup(){
14
+        myMap = new MyHashMap();
15
+    }
16
+
17
+    @Test
18
+    public void testPutAndGet_WithOneItem(){
19
+        User user = new User("Tiana");
20
+        Cart cart = new Cart(1);
21
+
22
+        // When
23
+        myMap.put(user, cart);
24
+
25
+        // Then
26
+        Cart actualCart = myMap.get(user);
27
+        Assert.assertEquals(cart, actualCart);
28
+    }
29
+
30
+    @Test
31
+    public void testPutAndGet_WithTwoItems(){
32
+        User tiana = new User("Tiana");
33
+        Cart tianasCart = new Cart(1134);
34
+
35
+        User john = new User("John");
36
+        Cart johnsCart = new Cart(42);
37
+
38
+        // When
39
+        myMap.put(john, johnsCart);
40
+        myMap.put(tiana, tianasCart);
41
+
42
+        // Then
43
+        Cart actualJohnsCart = myMap.get(john);
44
+        Assert.assertEquals(johnsCart, actualJohnsCart);
45
+
46
+        Cart actualTianasCart = myMap.get(tiana);
47
+        Assert.assertEquals(tianasCart, actualTianasCart);
48
+    }
49
+
50
+    @Test
51
+    public void testPutAndGet_UpdateCart(){
52
+        User tiana = new User("Tiana");
53
+        Cart tianasCart = new Cart(11);
54
+        myMap.put(tiana, tianasCart);
55
+
56
+        // When
57
+        Cart updatedCart = new Cart(99);
58
+        myMap.put(tiana, updatedCart);
59
+
60
+        // Then
61
+        Cart actualCart = myMap.get(tiana);
62
+        Assert.assertEquals(updatedCart, actualCart);
63
+    }
64
+
65
+    @Test
66
+    public void testGetWithoutPut(){
67
+        // Given
68
+        User user = new User("Tiana");
69
+
70
+        // When
71
+        Cart actualCart = myMap.get(user);
72
+
73
+        // Then
74
+        Assert.assertNull(actualCart);
75
+    }
76
+
77
+    @Test
78
+    public void testPutAndGet_WithDifferentKeyInstance(){
79
+        // Given
80
+        String name = "Tiana";
81
+        User tiana = new User(name);
82
+        Cart cart = new Cart(76);
83
+
84
+        // When
85
+        myMap.put(tiana, cart);
86
+        User cloneTiana = new User(name);
87
+        Cart actualCart = myMap.get(cloneTiana);
88
+
89
+        // Then
90
+        Assert.assertEquals(cart, actualCart);
91
+    }
92
+
93
+    @Test
94
+    public void testSize_WithNoEntry(){
95
+        int expectedSize = 0;
96
+        Assert.assertEquals(expectedSize, myMap.size());
97
+    }
98
+
99
+    @Test
100
+    public void testSize_WithEntries(){
101
+        // Given
102
+        User user1 = new User("Monica");
103
+        Cart cart1 = new Cart(45);
104
+
105
+        User user2 = new User("Quincy");
106
+        Cart cart2 = new Cart(78);
107
+
108
+        // When
109
+        myMap.put(user1, cart1);
110
+        myMap.put(user2, cart2);
111
+
112
+        // Then
113
+        int expectedSize = 2;
114
+        Assert.assertEquals(expectedSize, myMap.size());
115
+    }
116
+
117
+    @Test
118
+    public void testIsEmpty_WithoutEntry(){
119
+        Assert.assertTrue(myMap.isEmptry());
120
+    }
121
+
122
+    @Test
123
+    public void testisEmpty_WithEntries(){
124
+        // Given
125
+        User user1 = new User("Monica");
126
+        Cart cart1 = new Cart(45);
127
+
128
+        // When
129
+        myMap.put(user1, cart1);
130
+
131
+        // Then
132
+        Assert.assertFalse(myMap.isEmptry());
133
+    }
134
+
135
+    @Test
136
+    public void testGetKeys(){
137
+        // Given
138
+        User user1 = new User("Zeke");
139
+        Cart cart1 = new Cart(7);
140
+
141
+        User user2 = new User("Nona");
142
+        Cart cart2 = new Cart(29);
143
+
144
+        // When
145
+        myMap.put(user1, cart1);
146
+        myMap.put(user2, cart2);
147
+
148
+        // Then
149
+        List<User> keys = myMap.getKeys();
150
+        int expectedKeySize = 2;
151
+        Assert.assertEquals(expectedKeySize, keys.size());
152
+        Assert.assertTrue(keys.contains(user1));
153
+        Assert.assertTrue(keys.contains(user2));
154
+    }
155
+
156
+    @Test
157
+    public void testGetValues(){
158
+        // Given
159
+        User user1 = new User("Camille");
160
+        Cart cart1 = new Cart(433);
161
+
162
+        User user2 = new User("Nathan");
163
+        Cart cart2 = new Cart(71);
164
+
165
+        // When
166
+        myMap.put(user1, cart1);
167
+        myMap.put(user2, cart2);
168
+
169
+        // Then
170
+        List<Cart> values = myMap.getValues();
171
+        int expectedKeySize = 2;
172
+        Assert.assertEquals(expectedKeySize, values.size());
173
+        Assert.assertTrue(values.contains(cart1));
174
+        Assert.assertTrue(values.contains(cart2));
175
+    }
176
+
177
+    @Test
178
+    public void testPutAndGet_WithManyItems(){
179
+        // Given
180
+        for (int i = 0; i < 70000; i++) {
181
+            myMap.put(new User(i), new Cart(i));
182
+        }
183
+
184
+
185
+        for (int i = 0; i < 70000; i++) {
186
+            // When
187
+            Cart actualCart = myMap.get(new User(i));
188
+            Cart expectedCart = new Cart(i);
189
+
190
+            // Then
191
+            Assert.assertEquals(actualCart, expectedCart);
192
+        }
193
+    }
194
+
195
+    @Test
196
+    public void testRemove(){
197
+        // Given
198
+        User user1 = new User("Lena");
199
+        Cart cart1 = new Cart(11);
200
+
201
+        User user2 = new User("Jamal");
202
+        Cart cart2 = new Cart(21);
203
+
204
+        User user3 = new User("Kelvin");
205
+        Cart cart3 = new Cart(31);
206
+
207
+        // When
208
+        myMap.put(user1, cart1);
209
+        myMap.put(user2, cart2);
210
+        myMap.put(user3, cart3);
211
+
212
+        // Then
213
+        Cart actualCart = myMap.remove(user2);
214
+
215
+        Assert.assertEquals(cart2, actualCart);
216
+        Assert.assertNull(myMap.get(user2));
217
+    }
218
+
219
+    @Test
220
+    public void testRemove_CartNotInTheMap(){
221
+        // Given
222
+        User user1 = new User("Lena");
223
+
224
+        // When & Then
225
+        Assert.assertNull(myMap.remove(user1));
226
+    }
227
+}

+ 130
- 0
src/test/java/org/zipcoder/store/UserTest.java View File

@@ -0,0 +1,130 @@
1
+package org.zipcoder.store;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class UserTest {
8
+    private User user;
9
+
10
+    @Before
11
+    public void setup(){
12
+        user = new User();
13
+    }
14
+
15
+    @Test
16
+    public void testGetSetName(){
17
+        // Given
18
+        String expectedName = "Tiana";
19
+        user.setName(expectedName);
20
+
21
+        // When
22
+        String actualName = user.getName();
23
+
24
+        // Then
25
+        Assert.assertEquals(expectedName, actualName);
26
+    }
27
+
28
+    @Test
29
+    public void testGetSetId(){
30
+        // Given
31
+        long expectedId = 1133;
32
+        user.setId(expectedId);
33
+
34
+        // When
35
+        long actualId = user.getId();
36
+
37
+        // Then
38
+        Assert.assertEquals(expectedId, actualId);
39
+    }
40
+
41
+    @Test
42
+    public void testEqual_withDefaultFields(){
43
+        User user1 = new User();
44
+        User user2 = new User();
45
+
46
+        Assert.assertTrue(user1.equals(user2));
47
+    }
48
+
49
+    @Test
50
+    public void testEqual_withSameIdAndNoName(){
51
+        long id = 73;
52
+        User user1 = new User(id);
53
+        User user2 = new User(id);
54
+
55
+        Assert.assertTrue(user1.equals(user2));
56
+    }
57
+
58
+    @Test
59
+    public void testEqual_withSameIdAndDifferentName(){
60
+        long id = 613;
61
+        User user1 = new User(id, "Jonas");
62
+        User user2 = new User(id, "Fiona");
63
+
64
+        Assert.assertFalse(user1.equals(user2));
65
+    }
66
+
67
+    @Test
68
+    public void testEqual_withSameNameDifferentId(){
69
+        String name = "Asher";
70
+        User user1 = new User(92, name);
71
+        User user2 = new User(87, name);
72
+
73
+        Assert.assertFalse(user1.equals(user2));
74
+    }
75
+
76
+    @Test
77
+    public void testEqual_withSameIdAndSameName(){
78
+        long id = 613;
79
+        String name = "Asher";
80
+        User user1 = new User(id, name);
81
+        User user2 = new User(id, name);
82
+
83
+        Assert.assertTrue(user1.equals(user2));
84
+    }
85
+
86
+    @Test
87
+    public void testHashCode_withDefaultFields(){
88
+        User user1 = new User();
89
+        User user2 = new User();
90
+
91
+        Assert.assertEquals(user1.hashCode(), user2.hashCode());
92
+    }
93
+
94
+    @Test
95
+    public void testHashCode_withSameIdAndNoName(){
96
+        long id = 124;
97
+        User user1 = new User(id);
98
+        User user2 = new User(id);
99
+
100
+        Assert.assertEquals(user1.hashCode(), user2.hashCode());
101
+    }
102
+
103
+    @Test
104
+    public void testHashCode_withSameIdAndDifferentName(){
105
+        long id = 57;
106
+        User user1 = new User(id, "Jonas");
107
+        User user2 = new User(id, "Fiona");
108
+
109
+        Assert.assertNotEquals(user1.hashCode(), user2.hashCode());
110
+    }
111
+
112
+    @Test
113
+    public void testHashCode_withSameNameDifferentId(){
114
+        String name = "Asher";
115
+        User user1 = new User(11, name);
116
+        User user2 = new User(22, name);
117
+
118
+        Assert.assertNotEquals(user1.hashCode(), user2.hashCode());
119
+    }
120
+
121
+    @Test
122
+    public void testHashCode_withSameIdAndSameName(){
123
+        long id = 33;
124
+        String name = "Asher";
125
+        User user1 = new User(id, name);
126
+        User user2 = new User(id, name);
127
+
128
+        Assert.assertEquals(user1.hashCode(), user2.hashCode());
129
+    }
130
+}