From: Roland Haeder Date: Fri, 21 Aug 2015 10:41:41 +0000 (+0200) Subject: Continued with project, added more checks for items and handling of already added... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=eca6e8ac6b0287fa70da58c050741c83f616d122;p=pizzaservice-war.git Continued with project, added more checks for items and handling of already added items Signed-off-by:Roland Häder --- diff --git a/src/java/org/mxchange/pizzaapplication/basket/BaseBasket.java b/src/java/org/mxchange/pizzaapplication/basket/BaseBasket.java index fa81756f..ffd62df2 100644 --- a/src/java/org/mxchange/pizzaapplication/basket/BaseBasket.java +++ b/src/java/org/mxchange/pizzaapplication/basket/BaseBasket.java @@ -17,14 +17,15 @@ package org.mxchange.pizzaapplication.basket; import java.sql.SQLException; -import org.mxchange.pizzaapplication.database.frontend.basket.BasketDatabaseFrontend; import java.text.MessageFormat; import java.util.LinkedHashMap; import java.util.Map; import org.mxchange.jcore.BaseFrameworkSystem; import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; +import org.mxchange.pizzaapplication.database.frontend.basket.BasketDatabaseFrontend; import org.mxchange.pizzaapplication.database.frontend.basket.BasketFrontend; import org.mxchange.pizzaapplication.item.AddableBasketItem; +import org.mxchange.pizzaapplication.product.Product; /** * A general basket class @@ -45,7 +46,7 @@ public class BaseBasket extends BaseFrameworkSystem implements Basket(); @@ -54,7 +55,7 @@ public class BaseBasket extends BaseFrameworkSystem implements Basket extends FrameworkInterface * Adds given item instance to this basket * @param item Item instance to add */ - public void addItem (final AddableBasketItem item); - + public void addItem (final T item); + + /** + * Checks whether the given item as already been added. Mostly + * implementations may check the id number as this is mostly believed being + * unique. + * + * @param item Item instance to check + * @return Whether the given item has been found + */ + public boolean isItemAdded (final T item); + + /** + * Returns an item for given product instance or null if not found. + * + * @param product Product instance + * @return Item instance or null + */ + public T getItem (final Product product); } diff --git a/src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java b/src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java index 21fecbca..e6c7e4e3 100644 --- a/src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java +++ b/src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java @@ -42,18 +42,18 @@ public class BasketItemAddedFilter extends BaseServletFilter implements Filter { @Override public void doFilter (final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { // Trace message - this.getLogger().trace(MessageFormat.format("request={0},response={1},chain={2} - CALLED!", request, response, chain)); + this.getLogger().trace(MessageFormat.format("request={0},response={1},chain={2} - CALLED!", request, response, chain)); //NOI18N // All must be set if (request == null) { // request is null - throw new NullPointerException("request is null"); + throw new NullPointerException("request is null"); //NOI18N } else if (response == null) { // response is null - throw new NullPointerException("response is null"); + throw new NullPointerException("response is null"); //NOI18N } else if (chain == null) { // chain is null - throw new NullPointerException("chain is null"); + throw new NullPointerException("chain is null"); //NOI18N } // Call doFilter to move on @@ -63,29 +63,32 @@ public class BasketItemAddedFilter extends BaseServletFilter implements Filter { HttpSession session = ((HttpServletRequest) request).getSession(); // Debug message - this.getLogger().debug(MessageFormat.format("session={0}", session)); + this.getLogger().debug(MessageFormat.format("session={0}", session)); //NOI18N // Should not be null if (session == null) { // session is null - throw new NullPointerException("session is null"); + throw new NullPointerException("session is null"); //NOI18N } // Get item instance - Object item = session.getAttribute("item"); + Object object = session.getAttribute("item"); //NOI18N // Debug message - this.getLogger().debug(MessageFormat.format("item={0}", item)); + this.getLogger().debug(MessageFormat.format("object={0}", object)); //NOI18N // item should not be null - if (item == null) { + if (object == null) { // item is null - throw new NullPointerException("item is null"); - } else if (!(item instanceof AddableBasketItem)) { + throw new NullPointerException("item is null"); //NOI18N + } else if (!(object instanceof AddableBasketItem)) { // Not right instance - throw new IllegalArgumentException("item does not implement OrderableItem"); + throw new IllegalArgumentException("item does not implement AddableBasketItem"); //NOI18N } + // Now it is secure to cast + AddableBasketItem item = (AddableBasketItem) object; + // Get basket instance Basket basket; try { @@ -95,11 +98,17 @@ public class BasketItemAddedFilter extends BaseServletFilter implements Filter { throw new ServletException(ex); } + // Is the item already added? + if (basket.isItemAdded(item)) { + // Yes, then throw exception here + throw new ServletException(MessageFormat.format("item id={0} has already been added.", item.getId())); + } + // Register item with it - basket.addItem((AddableBasketItem) item); + basket.addItem(item); // Trace message - this.getLogger().trace("EXIT!"); + this.getLogger().trace("EXIT!"); //NOI18N } } diff --git a/src/java/org/mxchange/pizzaapplication/item/AddableBasketItem.java b/src/java/org/mxchange/pizzaapplication/item/AddableBasketItem.java index 34acdc08..4c0acd38 100644 --- a/src/java/org/mxchange/pizzaapplication/item/AddableBasketItem.java +++ b/src/java/org/mxchange/pizzaapplication/item/AddableBasketItem.java @@ -25,6 +25,18 @@ import org.mxchange.jcore.FrameworkInterface; */ public interface AddableBasketItem extends FrameworkInterface { + /** + * Item amount + * @return the amount + */ + public Long getAmount (); + + /** + * Item amount + * @param amount the amount to set + */ + public void setAmount (final Long amount); + /** * @return the id */ diff --git a/src/java/org/mxchange/pizzaapplication/item/BaseItem.java b/src/java/org/mxchange/pizzaapplication/item/BaseItem.java index 636fd3ca..8500a455 100644 --- a/src/java/org/mxchange/pizzaapplication/item/BaseItem.java +++ b/src/java/org/mxchange/pizzaapplication/item/BaseItem.java @@ -23,6 +23,11 @@ import org.mxchange.jcore.BaseFrameworkSystem; * @author Roland Haeder */ public class BaseItem extends BaseFrameworkSystem implements AddableBasketItem { + /** + * Item amount + */ + private Long amount; + /** * Item id number */ @@ -33,6 +38,22 @@ public class BaseItem extends BaseFrameworkSystem implements AddableBasketItem { */ private String type; + /** + * Item amount + * @return the amount + */ + public final Long getAmount () { + return this.amount; + } + + /** + * Item amount + * @param amount the amount to set + */ + public final void setAmount (final Long amount) { + this.amount = amount; + } + /** * @return the id */ diff --git a/src/java/org/mxchange/pizzaapplication/item/basket/BasketItem.java b/src/java/org/mxchange/pizzaapplication/item/basket/BasketItem.java index ae149e9f..f968c75b 100644 --- a/src/java/org/mxchange/pizzaapplication/item/basket/BasketItem.java +++ b/src/java/org/mxchange/pizzaapplication/item/basket/BasketItem.java @@ -16,12 +16,40 @@ */ package org.mxchange.pizzaapplication.item.basket; +import java.text.MessageFormat; +import org.mxchange.pizzaapplication.item.AddableBasketItem; import org.mxchange.pizzaapplication.item.BaseItem; +import org.mxchange.pizzaapplication.product.Product; /** * A basket item for Pizza service * * @author Roland Haeder */ -public class BasketItem extends BaseItem { +public class BasketItem extends BaseItem implements AddableBasketItem { + /** + * Default constructor + */ + public BasketItem () { + } + + /** + * Constructor for an item from given Product instance + * + * @param product Product instance + */ + public BasketItem (final Product product) { + // Trace message + this.getLogger().debug(MessageFormat.format("product={0} - CALLED!", product)); + + // product must not be null + if (product == null) { + // Abort here + throw new NullPointerException("product is null"); + } + + // Copy all neccessary values + this.setId(product.getId()); + this.setType("Product"); //NOI18N + } } diff --git a/web/form_handler/add_item.jsp b/web/form_handler/add_item.jsp index aa17cca5..3e033fc6 100644 --- a/web/form_handler/add_item.jsp +++ b/web/form_handler/add_item.jsp @@ -18,10 +18,8 @@ PizzaApplication app = PizzaServiceApplication.getInstance(application); // Redirect to proper URL - // @TODO Commented out for debugging - //response.sendRedirect(request.getContextPath() + "/?add=1"); + response.sendRedirect(request.getContextPath() + "/?add=1"); %> - diff --git a/web/index.jsp b/web/index.jsp index 916ccd50..37824419 100644 --- a/web/index.jsp +++ b/web/index.jsp @@ -10,6 +10,10 @@ <%@page import="org.mxchange.pizzaapplication.application.PizzaServiceApplication"%> <%@page import="org.mxchange.pizzaapplication.application.PizzaApplication"%> <%@page import="org.mxchange.pizzaapplication.product.Product"%> +<%@page import="org.mxchange.pizzaapplication.basket.Basket"%> +<%@page import="org.mxchange.pizzaapplication.basket.item.ItemBasket"%> +<%@page import="org.mxchange.pizzaapplication.item.basket.BasketItem"%> +<%@page import="org.mxchange.pizzaapplication.item.AddableBasketItem"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> @@ -66,6 +70,39 @@ // Get product instance Product product = iterator.next(); + // Get basket instance + Basket basket = ItemBasket.getInstance(session); + + // Create an item instance form this product + AddableBasketItem item = basket.getItem(product); + + // Has it been already added to the basket? + if (item != null) { + // Some nice output ... + %> + + + + + + + + + +
+ Warenkorb + + <%=item.getAmount()%> + + <%=product.getTitle()%> + + <%=product.getPrice()%> +
+ <% + // Then skip this item + continue; + } + // Unmark as ordered app.unmarkProductAsOrdered(product, session); %> @@ -76,7 +113,7 @@ - +