|
@@ -1,93 +1,35 @@
|
1
|
1
|
# Store Lab
|
2
|
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
|
3
|
|
4
|
|
-* **Objective** - to create two type of maps
|
|
4
|
+* **Objective** - A map/associative array is a data type that allow users to search for a value. The objective of this lab is to understand how to create map and see how we store data affects how its performance.
|
|
5
|
+
|
5
|
6
|
|
6
|
7
|
## 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
|
+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. Your task is to add the `equals` and `hashCode` to the `User` and `Cart` class.
|
8
|
9
|
|
9
|
10
|
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
|
+ 1. Add an `equals` method to the `User` class. The equal method should return true only if the `id` and `name` are the same. It return false otherwise.
|
11
|
12
|
2. Add a `hashCode` method by calling `Objects.hash()` and gives it the `id` and `name` (e.g. `Objects.hash(id, name)`)
|
12
|
13
|
2. Cart
|
13
|
14
|
1. Add an `equals` and `hashCode` methods with IntelliJ shortcut
|
14
|
15
|
1. Click `Command + N` and select `equals and hashCode`. Follow the wizard to generate the code.
|
15
|
16
|
|
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
|
|
17
|
+## Map
|
|
18
|
+Details instructions on what each method should do and return is in `MyMap` class.
|
64
|
19
|
|
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`
|
|
20
|
+### Part 2 - ListMap
|
|
21
|
+A list map stores every entry in a list. Every time the user calls `put`, create a new `Entry` and add it to the list. Your task is to make all the test pass.
|
|
22
|
+ 1. Uncomment line 178 - 196. Notice how long the test takes to run.
|
68
|
23
|
|
69
|
|
- ```java
|
70
|
|
- ArrayList<Product> products = new ArrayList();
|
71
|
|
- products.add(new Product("Shirt", 15.01));
|
72
|
|
- ```
|
|
24
|
+**BONUS**: Uncomment the remove method and add the code to pass the tests.
|
73
|
25
|
|
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);
|
|
26
|
+### Part 3 - MyHashMap
|
|
27
|
+If you still have no idea what a HashMap is, watch this [video](https://www.youtube.com/watch?v=shs0KM3wKv8). Notice I add the `bucketIndex` to help you determine where to store the entry. The `bucketIndex` uses the `hashCode` to try to evenly distribute the entries.
|
|
28
|
+ 1. `put` - find which bucket it belongs to, then add it to the list of that bucket
|
|
29
|
+ 2. `get` - find which bucket it belongs to, then loop through the list to find the entry corresponding to the key
|
|
30
|
+ 3. Uncomment line 178 - 196. Notice how fast it takes to run.
|
81
|
31
|
|
82
|
|
- // When
|
83
|
|
- Product shirt = new Product("Shirt", 1.99);
|
84
|
|
- cart.addProduct(shirt);
|
|
32
|
+**BONUS**: Uncomment the remove method and add the code to pass the tests.
|
85
|
33
|
|
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.
|
|
34
|
+## Performance
|
|
35
|
+Notice how long the ListMap takes to store and find 70,000 items. Notice how much faster a HashMap is. In the `MyHashMap` class, change the `BUCKET_SIZE` to `100`. Run the `MyHashMapTest` again. Notice how much faster it is. That's the power of designing and using a good data structure. It can speed up your program significantly.
|