Nhu Nguyen f42009eafb init | 6 years ago | |
---|---|---|
src/test/java/org/zipcoder/store | 6 years ago | |
.gitignore | 6 years ago | |
README.md | 6 years ago | |
pom.xml | 6 years ago |
Purpose - to understand HashMap, objects and data encapsulation.
Objective - to create two type of maps
By default, every Java object has an equals and a hashCode method. Java uses the object's address to calculate the hashCode. 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.
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.hashCode
method by calling Objects.hash()
and gives it the id
and name
(e.g. Objects.hash(id, name)
)equals
and hashCode
methods with IntelliJ shortcut
Command + N
and select equals and hashCode
. Follow the wizard to generate the code.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
In the test
folder, there is a ProductTest
with most of the tested commented out.
testConstruction_WithNoParams
checks to see if the Product
class has an empty constructor. To test it, I created a new object with no parameter.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).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.title (Color: color, Size: size)
Hat (Color: Yellow, Size: LG)
A cart has an array of Product.
CartTest
and uncomment line 9-13. To make the test pass:
Cart
testGetProducts
test passtestGetSetDiscount
test pass:
discount
getProductTotal
tests to pass, you need add all the product prices together and return the result.The goal of this exercise is to know how to create and test a class.
CreditCardTest
CreditCard
cardHolderName
of the type String
CreditCard
classString number
int expiredMonth
int expiredYear
getDescription
getDescription
should return [cardHolderName] [last 4 digit of the number] [expiredMonth]/[expiredYear]
Tia Mowry 4551 10/2019
CreditCard
field to the Cart
class
creditCard
fieldThe goal of this exercise is to change the products
array in the Cart
class to an ArrayList.
In the CartTest
, change the products
array to an ArrayList
ArrayList<Product> products = new ArrayList();
products.add(new Product("Shirt", 15.01));
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);
}
addProduct
method to the Cart
class