From: Roland Haeder Date: Fri, 11 Sep 2015 10:05:49 +0000 (+0200) Subject: Finally added calculateItemPrice() ... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=cd78d3fc8aa671e7ac8907c4354bfb6be87fb8b7;p=pizzaservice-war.git Finally added calculateItemPrice() ... Signed-off-by:Roland Häder --- diff --git a/src/java/org/mxchange/pizzaapplication/beans/basket/BasketWebBean.java b/src/java/org/mxchange/pizzaapplication/beans/basket/BasketWebBean.java index c0cb2ed5..e3965945 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/basket/BasketWebBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/basket/BasketWebBean.java @@ -71,6 +71,27 @@ public class BasketWebBean extends BaseFrameworkBean implements BasketWebControl this.basketBean = (BasketSessionBeanRemote) context.lookup("ejb/stateless-basket"); //NOI18N } + @Override + public Float calculateItemPrice () { + // Is the current item/amount set? + if (this.getCurrentItem() == null) { + // Current item is null + throw new NullPointerException("currentItem is null"); + } else if (this.getCurrentItem().getProduct() == null) { + // Product is null + throw new NullPointerException("currentItem.product is null"); + } else if (this.getCurrentItem().getAmount() == null) { + // Amount is null + throw new NullPointerException("currentItem.amount is null"); + } + + // Caculate item's price + Float totalPrice = (this.getCurrentItem().getProduct().getPrice() * this.getCurrentItem().getAmount()); + + // Return it + return totalPrice; + } + @Override public Long getAmount () { return this.amount; diff --git a/src/java/org/mxchange/pizzaapplication/beans/basket/BasketWebController.java b/src/java/org/mxchange/pizzaapplication/beans/basket/BasketWebController.java index 72bf7db8..d0713506 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/basket/BasketWebController.java +++ b/src/java/org/mxchange/pizzaapplication/beans/basket/BasketWebController.java @@ -26,6 +26,7 @@ import org.mxchange.jshopcore.model.product.Product; * @author Roland Haeder */ public interface BasketWebController extends Serializable { + /** * Checks whether the basket is empty * @@ -34,7 +35,8 @@ public interface BasketWebController extends Serializable { public boolean isEmpty (); /** - * Checks whether the basket has items in it. This method is wrapper to isEmpty() + * Checks whether the basket has items in it. This method is wrapper to + * isEmpty() * * @return Whether the basket is empty */ @@ -49,7 +51,8 @@ public interface BasketWebController extends Serializable { public boolean isProductAdded (final Product product); /** - * Adds given product instance to basket by adding amount from form data to it. + * Adds given product instance to basket by adding amount from form data to + * it. * * @param product Product instance to add * @return Redirect target or null @@ -83,4 +86,12 @@ public interface BasketWebController extends Serializable { * @param currentItem Current item */ public void setCurrentItem (final AddableBasketItem currentItem); + + /** + * Calculates total price (no tax added) of current item. If no current item + * is set and no amount, a NPE is thrown. + * + * @return Current item's total price + */ + public Float calculateItemPrice (); }