Introduce to classes and objects by creating a product and a cart.
Nhu Nguyen 5a19d83418 Created Cart and Product tests 8 anos atrás
src/test/java/org/zipcoder/store Created Cart and Product tests 8 anos atrás
.gitignore Created Cart and Product tests 8 anos atrás
README.md Created Cart and Product tests 8 anos atrás
pom.xml Created Cart and Product tests 8 anos atrás

README.md

Store Lab

Part 1 - Product

In the test folder, there is a ProductTest with most of the tested commented out.

  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.
    • To make the test pass, create a new empty constructor with no parameter
  2. Uncomment the second test, line 14 - 26.
    • Create a new constructor that takes a String for the product name
  3. Continue to uncomment the test and make it pass
  4. For the get and set test, you need to create a field and methods to get and set the field. Ex:

    public class Book {
      private String title;
    
      public void setTitle(String title){
        this.title = title;
      }
    
      public String getTitle() {
        return this.title;
      }
    }
    
  5. Continue to uncomment the test and add code to make the test pass

  6. For the product description, it should be in this format title (Color: color, Size: size)

    • ex: Hat (Color: Yellow, Size: LG)

Part 2 - Cart

A cart has an array of Product.

  1. Open the CartTest and uncomment line 9-13. To make the test pass:
    • Create a new class named Cart
    • Create a new constructor that takes an array of Product
    • Run the test, it should pass
  2. Create a getter for Product to make testGetProducts test pass
  3. To make testGetSetDiscount test pass:
    • Create a new decimal field called discount
    • Create a getter and setter for the field
    • Run the test
  4. To get getProductTotal tests to pass, you need add all the product prices together and return the result.
  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

Part 3 - CreditCard

The goal of this exercise is to know how to create and test a class.

  1. Create a new class called CreditCard
  2. Create a test case to get and set the cardHolderName of the type String
    • Create getter and setter methods in the CreditCard class
    • Add a field called cardHolderName
  3. Do the same for the following fields:
    • String number
    • int expiredMonth
    • int expiredYear
  4. Create a getter and setter for each field
  5. Create a test for getDescription
    • getDescription should return [cardHolderName] [last 4 digit of the number] [expiredMonth]/[expiredYear]
      • ex: Tia Mowry 4551 10/2019
  6. Add a CreditCard field to the Cart class
    • Add a getter and setter for the creditCard field

Part 4 - Product as an ArrayList - BONUS

The goal of this exercise is to change the products array in the Cart class to an ArrayList.

  1. In the CartTest, change the products array to an ArrayList java ArrayList<Product> products = new ArrayList(); products.add(new Product("Shirt", 15.01));
  2. Add this test to the CartTest class

    @Test
    public void testAddToCard(){
        // Given
        ArrayList<Product> products = new ArrayList();
        Cart cart = new Cart(products);
    
        // When
        Product shirt = new Product("Shirt", 1.99);
        cart.addProduct(shirt);
    
        // Then
        double expectedTotal = 1.99;
        double actualTotal = cart.getProductTotal();
        Assert.assertEquals(expectedTotal, actualTotal, DELTA);
    }
    
    • Add a addProduct method to the Cart class
    • Run the test. It should pass.