|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+# Store Lab
|
|
|
2
|
+* **Purpose** - to understand [classes](https://docs.oracle.com/javase/tutorial/java/concepts/class.html), [objects](https://docs.oracle.com/javase/tutorial/java/concepts/object.html) and [data encapsulation](https://en.wikipedia.org/wiki/Data_encapsulation).
|
|
|
3
|
+* **Objective** - to create a cart with products and payment
|
|
|
4
|
+
|
|
|
5
|
+## Part 1 - Product
|
|
|
6
|
+In the `test` folder, there is a `ProductTest` with most of the tested commented out.
|
|
|
7
|
+ 1. Notice the word `Product` is red. That's because we need to create a new Class named `Product`. The file should be in `src/main/java/org.zipcoder.store`.
|
|
|
8
|
+ - To make the test pass, create a new empty constructor with no parameter
|
|
|
9
|
+ 2. Uncomment the second test, line 14 - 26.
|
|
|
10
|
+ - Create a new constructor that takes a String for the product name
|
|
|
11
|
+ 3. Continue to uncomment the test and make it pass
|
|
|
12
|
+ 4. For the `get` and `set` test, you need to create a field and methods to get and set the field. Ex:
|
|
|
13
|
+
|
|
|
14
|
+ ```java
|
|
|
15
|
+ public class Book {
|
|
|
16
|
+ private String title;
|
|
|
17
|
+
|
|
|
18
|
+ public void setTitle(String title){
|
|
|
19
|
+ this.title = title;
|
|
|
20
|
+ }
|
|
|
21
|
+
|
|
|
22
|
+ public String getTitle() {
|
|
|
23
|
+ return this.title;
|
|
|
24
|
+ }
|
|
|
25
|
+ }
|
|
|
26
|
+ ```
|
|
|
27
|
+ 5. Continue to uncomment the test and add code to make the test pass
|
|
|
28
|
+ 6. For the product description, it should be in this format `title (Color: color, Size: size)`
|
|
|
29
|
+ - ex: `Hat (Color: Yellow, Size: LG)`
|
|
|
30
|
+
|
|
|
31
|
+## Part 2 - Cart
|
|
|
32
|
+A cart has an array of Product.
|
|
|
33
|
+ 1. Open the `CartTest` and uncomment line 9-13. To make the test pass:
|
|
|
34
|
+ - Create a new class named `Cart`
|
|
|
35
|
+ - Create a new constructor that takes an array of Product
|
|
|
36
|
+ - Run the test, it should pass
|
|
|
37
|
+ 2. Create a getter for Product to make `testGetProducts` test pass
|
|
|
38
|
+ 3. To make `testGetSetDiscount` test pass:
|
|
|
39
|
+ - Create a new decimal field called `discount`
|
|
|
40
|
+ - Create a getter and setter for the field
|
|
|
41
|
+ - Run the test
|
|
|
42
|
+ 4. To get `getProductTotal` tests to pass, you need add all the product prices together and return the result.
|
|
|
43
|
+ 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
|
|
|
44
|
+
|
|
|
45
|
+## Part 3 - CreditCard
|
|
|
46
|
+The goal of this exercise is to know how to create and test a class.
|
|
|
47
|
+ 1. Create a new class called `CreditCard`
|
|
|
48
|
+ 2. Create a test case to get and set the `cardHolderName` of the type `String`
|
|
|
49
|
+ - Create getter and setter methods in the `CreditCard` class
|
|
|
50
|
+ - Add a field called cardHolderName
|
|
|
51
|
+ 2. Do the same for the following fields:
|
|
|
52
|
+ - `String number`
|
|
|
53
|
+ - `int expiredMonth`
|
|
|
54
|
+ - `int expiredYear`
|
|
|
55
|
+ 3. Create a getter and setter for each field
|
|
|
56
|
+ 4. Create a test for `getDescription`
|
|
|
57
|
+ - `getDescription` should return `[cardHolderName] [last 4 digit of the number] [expiredMonth]/[expiredYear]`
|
|
|
58
|
+ - ex: `Tia Mowry 4551 10/2019`
|
|
|
59
|
+ 5. Add a `CreditCard` field to the `Cart` class
|
|
|
60
|
+ - Add a getter and setter for the `creditCard` field
|
|
|
61
|
+
|
|
|
62
|
+## Part 4 - Product as an ArrayList - BONUS
|
|
|
63
|
+The goal of this exercise is to change the `products` array in the `Cart` class to an ArrayList.
|
|
|
64
|
+ 1. In the `CartTest`, change the `products` array to an `ArrayList`
|
|
|
65
|
+ ```java
|
|
|
66
|
+ ArrayList<Product> products = new ArrayList();
|
|
|
67
|
+ products.add(new Product("Shirt", 15.01));
|
|
|
68
|
+ ```
|
|
|
69
|
+ 2. Add this test to the `CartTest` class
|
|
|
70
|
+ ```java
|
|
|
71
|
+ @Test
|
|
|
72
|
+ public void testAddToCard(){
|
|
|
73
|
+ // Given
|
|
|
74
|
+ ArrayList<Product> products = new ArrayList();
|
|
|
75
|
+ Cart cart = new Cart(products);
|
|
|
76
|
+
|
|
|
77
|
+ // When
|
|
|
78
|
+ Product shirt = new Product("Shirt", 1.99);
|
|
|
79
|
+ cart.addProduct(shirt);
|
|
|
80
|
+
|
|
|
81
|
+ // Then
|
|
|
82
|
+ double expectedTotal = 1.99;
|
|
|
83
|
+ double actualTotal = cart.getProductTotal();
|
|
|
84
|
+ Assert.assertEquals(expectedTotal, actualTotal, DELTA);
|
|
|
85
|
+ }
|
|
|
86
|
+ ```
|
|
|
87
|
+ - Add a `addProduct` method to the `Cart` class
|
|
|
88
|
+ - Run the test. It should pass.
|