Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature lecture 3 #3

Merged
merged 4 commits into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.es.phoneshop.model.product;
package com.es.phoneshop.model;

import java.util.List;
import java.util.Optional;
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/es/phoneshop/model/cart/entity/Cart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.es.phoneshop.model.cart.entity;

import com.es.phoneshop.model.product.entity.Product;

import java.util.ArrayList;
import java.util.List;

public class Cart {
//LinkedHashMap would be better?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LinkedHashMap is a specific realization, instead of interface. This is the only drawback i could see

//Can Entity-Bean contain some business-logic?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, bean -- smth like POJO

private List<CartItem> cartItems;

public Cart() {
this.cartItems = new ArrayList<>();
}

public List<CartItem> getItems() {
return this.cartItems;
}

public void add(Product product, int quantity) {
cartItems.add(new CartItem(product, quantity));
}

public void add(CartItem cartItem){
cartItems.add(cartItem);
}

@Override
public String toString() {
return "Cart[" + cartItems + ']';
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/es/phoneshop/model/cart/entity/CartItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.es.phoneshop.model.cart.entity;

import com.es.phoneshop.model.product.entity.Product;

public class CartItem {
private Product product;

private int quantity;

public CartItem(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}

public Product getProduct() {
return product;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public void increaseQuantity(int quantity){
this.quantity+=quantity;
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/es/phoneshop/model/cart/service/CartService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.es.phoneshop.model.cart.service;

import com.es.phoneshop.model.cart.entity.Cart;
import com.es.phoneshop.web.exceptions.OutOfStockException;

public interface CartService<SessionResource> {

Cart getCart(SessionResource sessionResource);

void add(Cart cart, Long productId, int quantity) throws OutOfStockException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.es.phoneshop.model.cart.service;

import com.es.phoneshop.model.cart.entity.Cart;
import com.es.phoneshop.model.cart.entity.CartItem;
import com.es.phoneshop.model.product.dao.ArrayListProductDao;
import com.es.phoneshop.model.product.dao.ProductDao;
import com.es.phoneshop.model.product.entity.Product;
import com.es.phoneshop.web.exceptions.OutOfStockException;

import javax.servlet.http.HttpServletRequest;
import java.util.NoSuchElementException;
import java.util.Optional;

public enum HttpServletCartService implements CartService<HttpServletRequest> {
INSTANCE;

private static final String CART_SESSION_ATTRIBUTE = HttpServletCartService.class.getName() + ".cart";

private final ProductDao productDao = ArrayListProductDao.getInstance();

@Override
public Cart getCart(HttpServletRequest request) {
//i heard about AtomicReference solution of thread safety problem but it seems to be next level of knowledge
synchronized (request.getSession()) {
Cart cart = (Cart) request.getSession().getAttribute(CART_SESSION_ATTRIBUTE);
if (cart == null) {
request.getSession().setAttribute(CART_SESSION_ATTRIBUTE, cart = new Cart());
}
return cart;
}
}

@Override
public void add(Cart cart, Long productId, int quantity) throws OutOfStockException {
//some valid-checking should be here
synchronized (cart) {
try {
Product product = productDao.get(productId).get();

if (quantity > product.getStock()) {
throw new OutOfStockException();
}

Optional<CartItem> optionalCartItem = findItemInCart(cart, productId);
if (optionalCartItem.isPresent()) {
var cartItem = optionalCartItem.get();
if (product.getStock() >= cartItem.getQuantity() + quantity) {
cartItem.increaseQuantity(quantity);
} else {
throw new OutOfStockException();
}
} else {
cart.add(product, quantity);
}
} catch (NoSuchElementException e) {
throw new NoSuchElementException(String.valueOf(productId));
}
}
}

private Optional<CartItem> findItemInCart(Cart cart, Long productId) {
return cart.getItems()
.stream()
.filter(existingCartItem -> existingCartItem.getProduct().getId().equals(productId))
.findFirst();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.es.phoneshop.model.product;

import com.es.phoneshop.model.product.dao.ArrayListProductDao;
import com.es.phoneshop.model.product.dao.TestableSingletonProductDao;
import com.es.phoneshop.model.product.entity.Product;
import org.junit.Before;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.es.phoneshop.model.product;

import static org.junit.Assert.*;

import com.es.phoneshop.model.product.dao.ArrayListProductDao;
import com.es.phoneshop.model.product.dao.TestableSingletonProductDao;
import com.es.phoneshop.model.product.entity.Product;
import com.es.phoneshop.model.product.service.ProductServiceImpl;
import com.es.phoneshop.model.product.sortEnums.SortField;
import com.es.phoneshop.model.product.sortEnums.SortOrder;
import org.junit.Before;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.es.phoneshop.web;

import com.es.phoneshop.model.product.ArrayListProductDao;
import com.es.phoneshop.model.product.Product;
import com.es.phoneshop.model.product.ProductDao;
import com.es.phoneshop.model.product.TestableSingletonProductDao;
import org.junit.After;
import com.es.phoneshop.model.product.dao.ArrayListProductDao;
import com.es.phoneshop.model.product.entity.Product;
import com.es.phoneshop.model.product.dao.TestableSingletonProductDao;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.es.phoneshop.web;

import com.es.phoneshop.model.product.ArrayListProductDao;
import com.es.phoneshop.model.product.Product;
import com.es.phoneshop.model.product.TestableSingletonProductDao;
import org.junit.After;
import com.es.phoneshop.model.product.dao.ArrayListProductDao;
import com.es.phoneshop.model.product.entity.Product;
import com.es.phoneshop.model.product.dao.TestableSingletonProductDao;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -13,7 +12,6 @@
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.AreaAveragingScaleFilter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Currency;
Expand Down