Quellcode durchsuchen

Created Cart and Product tests

Nhu Nguyen vor 8 Jahren
Commit
5a19d83418
5 geänderte Dateien mit 389 neuen und 0 gelöschten Zeilen
  1. 55
    0
      .gitignore
  2. 88
    0
      README.md
  3. 20
    0
      pom.xml
  4. 110
    0
      src/test/java/org/zipcoder/store/CartTest.java
  5. 116
    0
      src/test/java/org/zipcoder/store/ProductTest.java

+ 55
- 0
.gitignore Datei anzeigen

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

+ 88
- 0
README.md Datei anzeigen

@@ -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.

+ 20
- 0
pom.xml Datei anzeigen

@@ -0,0 +1,20 @@
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>io.zipcoder</groupId>
8
+    <artifactId>ZipcodeStore</artifactId>
9
+    <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>4.12</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+    </dependencies>
18
+
19
+
20
+</project>

+ 110
- 0
src/test/java/org/zipcoder/store/CartTest.java Datei anzeigen

@@ -0,0 +1,110 @@
1
+package org.zipcoder.store;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CartTest {
7
+    private static final double DELTA = 0.01;
8
+
9
+    @Test
10
+    public void testConstruction(){
11
+        Product[] products = new Product[]{};
12
+        Cart cart = new Cart(products);
13
+    }
14
+
15
+    @Test
16
+    public void testGetProducts(){
17
+        // Given
18
+        Product[] expectedProducts = new Product[0];
19
+
20
+        Cart cart = new Cart(expectedProducts);
21
+
22
+        // When
23
+        Product[] actualProducts = cart.getProducts();
24
+
25
+        // Then
26
+        Assert.assertEquals(expectedProducts, actualProducts);
27
+    }
28
+
29
+    @Test
30
+    public void testGetSetDiscount(){
31
+        // Given
32
+        Product[] products = new Product[]{};
33
+        Cart cart = new Cart(products);
34
+        double expectedDiscount = 0.02; // 20%
35
+
36
+        // When
37
+        cart.setDiscount(expectedDiscount);
38
+
39
+        // Then
40
+        double actualDiscount = cart.getDiscount();
41
+        Assert.assertEquals(expectedDiscount, actualDiscount, DELTA);
42
+    }
43
+
44
+    @Test
45
+    public void testProductTotal_WithNoProduct(){
46
+        // Given
47
+        Product[] products = new Product[0];
48
+
49
+        Cart cart = new Cart(products);
50
+
51
+        // When
52
+        double actualTotal = cart.getProductTotal();
53
+
54
+        // Then
55
+        double expectedProductTotal = 0.0;
56
+        Assert.assertEquals(expectedProductTotal, actualTotal, DELTA);
57
+    }
58
+
59
+
60
+    @Test
61
+    public void testProductTotal_WithProduct(){
62
+        // Given
63
+        Product[] products = new Product[3];
64
+        products[0] = new Product("T-Shirt", 9.99);
65
+        products[1] = new Product("Pants", 10.00);
66
+        products[2] = new Product("Hat", 5.00);
67
+
68
+        Cart cart = new Cart(products);
69
+
70
+        // When
71
+        double actualTotal = cart.getProductTotal();
72
+
73
+        // Then
74
+        double expectedProductTotal = 24.99;
75
+        Assert.assertEquals(expectedProductTotal, actualTotal, DELTA);
76
+    }
77
+
78
+    @Test
79
+    public void testTotal_WithDiscountButNoProduct(){
80
+        // Given
81
+        Product[] products = new Product[]{};
82
+        Cart cart = new Cart(products);
83
+
84
+        // When
85
+        cart.setDiscount(0.50); // 50% off
86
+        double actualTotal = cart.getTotal();
87
+
88
+        // Then
89
+        double expectedTotal = 0.0;
90
+        Assert.assertEquals(expectedTotal, actualTotal, DELTA);
91
+    }
92
+
93
+    @Test
94
+    public void testTotal_WithDiscountAndProducts(){
95
+        // Given
96
+        Product[] products = new Product[2];
97
+        products[0] = new Product("T-Shirt", 20.10);
98
+        products[1] = new Product("Pants", 10.00);
99
+
100
+        Cart cart = new Cart(products);
101
+
102
+        // When
103
+        cart.setDiscount(0.50); // 50% off
104
+        double actualTotal = cart.getTotal();
105
+
106
+        // Then
107
+        double expectedTotal = 15.05;
108
+        Assert.assertEquals(expectedTotal, actualTotal, DELTA);
109
+    }
110
+}

+ 116
- 0
src/test/java/org/zipcoder/store/ProductTest.java Datei anzeigen

@@ -0,0 +1,116 @@
1
+package org.zipcoder.store;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class ProductTest {
7
+    private static final double DELTA = 0.01;
8
+
9
+    @Test
10
+    public void testConstruction_WithNoParams() {
11
+        Product product = new Product();
12
+    }
13
+
14
+//    @Test
15
+//    public void testConstruction_WithName(){
16
+//        //Given
17
+//        String expectedName = "T-Shirt";
18
+//
19
+//        //When
20
+//        Product product = new Product(expectedName);
21
+//
22
+//        //Then
23
+//        String actualName = product.getName();
24
+//        Assert.assertEquals(expectedName, actualName);
25
+//    }
26
+//
27
+//    @Test
28
+//    public void testConstruction_WithNameAndPrice(){
29
+//        //Given
30
+//        String expectedName = "T-Shirt";
31
+//        double expectedPrice = 19.99;
32
+//
33
+//        //When
34
+//        Product product = new Product(expectedName, expectedPrice);
35
+//
36
+//        //Then
37
+//        String actualName = product.getName();
38
+//        Assert.assertEquals(expectedName, actualName);
39
+//
40
+//        double actualPrice = product.getPrice();
41
+//        Assert.assertEquals(expectedPrice, actualPrice, DELTA);
42
+//    }
43
+//
44
+//    @Test
45
+//    public void testGetSetName(){
46
+//        //Given
47
+//        String expectedName = "Pants";
48
+//        Product product = new Product();
49
+//
50
+//        //When
51
+//        product.setName(expectedName);
52
+//
53
+//        //Then
54
+//        String actualName = product.getName();
55
+//        Assert.assertEquals(expectedName, actualName);
56
+//    }
57
+//
58
+//    @Test
59
+//    public void testGetSetPrice(){
60
+//        //Given
61
+//        double expectedPrice = 15.39;
62
+//        Product product = new Product();
63
+//
64
+//        //When
65
+//        product.setPrice(expectedPrice);
66
+//
67
+//        //Then
68
+//        double actualPrice = product.getPrice();
69
+//        Assert.assertEquals(expectedPrice, actualPrice, DELTA);
70
+//    }
71
+//
72
+//    @Test
73
+//    public void testGetSetSize(){
74
+//        //Given
75
+//        int expectedSize = 7;
76
+//        Product product = new Product();
77
+//
78
+//        //When
79
+//        product.setSize(expectedSize);
80
+//
81
+//        //Then
82
+//        int actualSize = product.getSize();
83
+//        Assert.assertEquals(expectedSize, actualSize);
84
+//    }
85
+//
86
+//    @Test
87
+//    public void testGetSetColor(){
88
+//        //Given
89
+//        String expectedColor = "Blue";
90
+//        Product product = new Product();
91
+//
92
+//        //When
93
+//        product.setColor(expectedColor);
94
+//
95
+//        //Then
96
+//        String actualColor = product.getColor();
97
+//        Assert.assertEquals(expectedColor, actualColor);
98
+//    }
99
+//
100
+//
101
+//    @Test
102
+//    public void testGetProductDescription(){
103
+//        //Given
104
+//        Product product = new Product();
105
+//
106
+//        //When
107
+//        product.setName("Coat");
108
+//        product.setColor("Black");
109
+//        product.setSize(14);
110
+//
111
+//        //Then
112
+//        String expectedDescription = "Coat (Color: Black, Size: 14)";
113
+//        String actualDescription = product.getDescription();
114
+//        Assert.assertEquals(expectedDescription, actualDescription);
115
+//    }
116
+}