forked from andrewosipenko/phoneshop-servlet-api
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...a/com/es/phoneshop/model/product/DAO.java → ...main/java/com/es/phoneshop/model/DAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/com/es/phoneshop/model/cart/entity/Cart.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
//Can Entity-Bean contain some business-logic? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
30
src/main/java/com/es/phoneshop/model/cart/entity/CartItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
src/main/java/com/es/phoneshop/model/cart/service/CartService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/es/phoneshop/model/cart/service/HttpServletCartService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/test/java/com/es/phoneshop/model/product/ArrayListProductDaoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/test/java/com/es/phoneshop/model/product/ProductServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 3 additions & 5 deletions
8
src/test/java/com/es/phoneshop/web/ProductDemoDataServletContextListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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