From: Roland Haeder Date: Thu, 10 Sep 2015 13:39:44 +0000 (+0200) Subject: Updated author (email address) + jars + implemented very basic basket X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=5732496c13f13fec19c7630530ba2c448f53cf28;p=jcustomer-core.git Updated author (email address) + jars + implemented very basic basket Signed-off-by:Roland Häder --- diff --git a/lib/jcore-ee-logger.jar b/lib/jcore-ee-logger.jar index 5ebf6d0..c652122 100644 Binary files a/lib/jcore-ee-logger.jar and b/lib/jcore-ee-logger.jar differ diff --git a/lib/jcoreee.jar b/lib/jcoreee.jar index 963ff76..935a2c9 100644 Binary files a/lib/jcoreee.jar and b/lib/jcoreee.jar differ diff --git a/src/org/mxchange/jshopcore/exceptions/BasketItemAlreadyAddedException.java b/src/org/mxchange/jshopcore/exceptions/BasketItemAlreadyAddedException.java new file mode 100644 index 0000000..efd2492 --- /dev/null +++ b/src/org/mxchange/jshopcore/exceptions/BasketItemAlreadyAddedException.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jshopcore.exceptions; + +import java.text.MessageFormat; +import org.mxchange.jshopcore.model.basket.AddableBasketItem; + +/** + * An exception thrown when the given item is already added to the basket. + * + * @author Roland Haeder + */ +public class BasketItemAlreadyAddedException extends Exception { + /** + * Serial number + */ + private static final long serialVersionUID = 64_828_391_485_785_167L; + + /** + * Constructor with item instance T + * + * @param Any item that is or extends the interface + * @param item An instance of a T item + */ + public BasketItemAlreadyAddedException (final T item) { + // Create message and pass it along + super(MessageFormat.format("Item {0} has already been added. Did you miss to call isAdded()?", item)); + } +} diff --git a/src/org/mxchange/jshopcore/exceptions/CategoryTitleAlreadyUsedException.java b/src/org/mxchange/jshopcore/exceptions/CategoryTitleAlreadyUsedException.java index 4936d65..fa290f4 100644 --- a/src/org/mxchange/jshopcore/exceptions/CategoryTitleAlreadyUsedException.java +++ b/src/org/mxchange/jshopcore/exceptions/CategoryTitleAlreadyUsedException.java @@ -22,7 +22,7 @@ import org.mxchange.jshopcore.model.category.Category; /** * An exception thrown when the given title is already used * - * @author Roland Haeder + * @author Roland Haeder */ public class CategoryTitleAlreadyUsedException extends Exception { /** @@ -39,4 +39,14 @@ public class CategoryTitleAlreadyUsedException extends Exception { // Call super constructor super(MessageFormat.format("Title {0} is already used.", category.getTitle())); //NOI18N } + + /** + * Constructor with HttpServletRequest instance + * + * @param cause Cause for this exception + */ + public CategoryTitleAlreadyUsedException (final Throwable cause) { + // Call super constructor + super(cause); + } } diff --git a/src/org/mxchange/jshopcore/exceptions/ProductTitleAlreadyUsedException.java b/src/org/mxchange/jshopcore/exceptions/ProductTitleAlreadyUsedException.java index 3191646..53ec406 100644 --- a/src/org/mxchange/jshopcore/exceptions/ProductTitleAlreadyUsedException.java +++ b/src/org/mxchange/jshopcore/exceptions/ProductTitleAlreadyUsedException.java @@ -22,7 +22,7 @@ import org.mxchange.jshopcore.model.product.Product; /** * An exception thrown when the given title is already used * - * @author Roland Haeder + * @author Roland Haeder */ public class ProductTitleAlreadyUsedException extends Exception { /** @@ -39,4 +39,14 @@ public class ProductTitleAlreadyUsedException extends Exception { // Call super constructor super(MessageFormat.format("Title {0} is already used.", product.getTitle())); //NOI18N } + + /** + * Constructor with HttpServletRequest instance + * + * @param cause Cause for this exception + */ + public ProductTitleAlreadyUsedException (final Throwable cause) { + // Call super constructor + super(cause); //NOI18N + } } diff --git a/src/org/mxchange/jshopcore/model/basket/AddableBasketItem.java b/src/org/mxchange/jshopcore/model/basket/AddableBasketItem.java index db0855a..24997a8 100644 --- a/src/org/mxchange/jshopcore/model/basket/AddableBasketItem.java +++ b/src/org/mxchange/jshopcore/model/basket/AddableBasketItem.java @@ -22,66 +22,76 @@ import org.mxchange.jshopcore.model.product.Product; /** * An interface for addable basket items * - * @author Roland Haeder + * @author Roland Haeder */ public interface AddableBasketItem extends Serializable { /** - * Item amount + * Getter for item amount * * @return the amount */ public Long getAmount (); /** - * Item amount + * Setter for item amount * * @param amount the amount to set */ public void setAmount (final Long amount); /** - * Entry id (from database backend) + * Getter for entry id (from database backend) * * @return the id */ public Long getId (); /** - * Entry id (from database backend) + * Setter for entry id (from database backend) * * @param id the id to set */ public void setId (final Long id); /** + * Getter for item id (e.g. from product) + * * @return the id */ public Long getItemId (); /** + * Setter for item id (e.g. from product) + * * @param id the id to set */ public void setItemId (final Long id); /** + * Getter for item type + * * @return the type */ public String getItemType (); /** + * Setter for item type + * * @param type the type to set */ public void setItemType (final String type); /** + * Getter for product instance + * * @return the product */ public Product getProduct (); /** - * Product instance - * + * Setter fo product instance + * * @param product the product to set */ public void setProduct (final Product product); diff --git a/src/org/mxchange/jshopcore/model/basket/BaseBasket.java b/src/org/mxchange/jshopcore/model/basket/BaseBasket.java index 54ac276..f510552 100644 --- a/src/org/mxchange/jshopcore/model/basket/BaseBasket.java +++ b/src/org/mxchange/jshopcore/model/basket/BaseBasket.java @@ -16,13 +16,17 @@ */ package org.mxchange.jshopcore.model.basket; +import java.util.Deque; +import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.Map; +import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException; /** * A general basket class. This class does not store any properties, it only * contains logic for handling the items (T). * - * @author Roland Haeder + * @author Roland Haeder * @param Any instance that implements AddableBasketItem */ public abstract class BaseBasket implements Basket { @@ -31,43 +35,56 @@ public abstract class BaseBasket implements Basket< */ private static final long serialVersionUID = 782_396_762_230_845_717L; + /** + * Ordered item list + */ + private final Deque deque; + /** * Protected constructor with session instance */ protected BaseBasket () { + // Init queue + this.deque = new LinkedList<>(); } @Override - public void init () { - } - - @Override - public void addItem (final T item) { + public void addItem (final T item) throws BasketItemAlreadyAddedException { // item must not be null if (null == item) { // Then abort here throw new NullPointerException("item is null"); //NOI18N } else if (this.isAdded(item)) { // Already been added - throw new IllegalArgumentException("item has already been added. Did you miss to call isAdded()?"); //NOI18N + throw new BasketItemAlreadyAddedException(item); //NOI18N } - // Add item to database - // TODO: ((BasketFrontend) this.getFrontend()).addItem(item, this.getSessionId()); + // Add it here + this.deque.add(item); } @Override public boolean isEmpty () { // Deligate call to frontend - // TODO: return ((BasketFrontend) this.getFrontend()).isEmpty(); - throw new UnsupportedOperationException("Not yet implmeneted."); + return this.deque.isEmpty(); } @Override public Map getAll () { // Init map - // TODO: Map map = ((BasketFrontend) this.getFrontend()).getAll(); - Map map = null; + Map map = new LinkedHashMap<>(this.deque.size()); + + // Iterate over full item list + for (T item : this.deque) { + // item should not be null + if (null == item) { + // Abort here + throw new NullPointerException("item is null"); //NOI18N + } + + // Add to map, use the item's id as key + map.put(item.getItemId(), item); + } // Return it return map; @@ -75,16 +92,17 @@ public abstract class BaseBasket implements Basket< @Override public T getLast () { - // Deligate to frontend - // TODO: return ((BasketFrontend) this.getFrontend()).getLast(); - throw new UnsupportedOperationException("Not yet implmeneted."); + // Deligate to list + return this.deque.getLast(); } @Override public int getLastNumRows () { - // Deligate to frontend - // TODO: return this.getFrontend().getLastNumRows(); - throw new UnsupportedOperationException("Not yet implmeneted."); + // Is the list empty? + assert this.isEmpty() : "deque is empty"; //NOI18N + + // It is size-1 + return (this.deque.size() - 1); } @Override @@ -95,11 +113,7 @@ public abstract class BaseBasket implements Basket< throw new NullPointerException("item is null"); //NOI18N } - // Call map's method - // TODO: boolean isAdded = ((BasketFrontend) this.getFrontend()).isAdded(item, this.getSessionId()); - boolean isAdded = true; - // Return it - return isAdded; + return this.deque.contains(item); } } diff --git a/src/org/mxchange/jshopcore/model/basket/Basket.java b/src/org/mxchange/jshopcore/model/basket/Basket.java index 72d9e07..51f4a58 100644 --- a/src/org/mxchange/jshopcore/model/basket/Basket.java +++ b/src/org/mxchange/jshopcore/model/basket/Basket.java @@ -18,20 +18,23 @@ package org.mxchange.jshopcore.model.basket; import java.io.Serializable; import java.util.Map; +import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException; /** * An interface for baskets * - * @author Roland Haeder + * @author Roland Haeder * @param Any addable basket items */ public interface Basket extends Serializable { /** * Adds given item instance to this basket + * * @param item Item instance to add + * @throws org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException If the item instance has already been added */ - public void addItem (final T item); + public void addItem (final T item) throws BasketItemAlreadyAddedException; /** * Checks whether the given item as already been added. If the product's @@ -49,11 +52,6 @@ public interface Basket extends Serializable { */ public boolean isEmpty (); - /** - * Initializes this instance with given ServletContext - */ - public void init (); - /** * Some "getter" for all entries in this basket * diff --git a/src/org/mxchange/jshopcore/model/basket/ShopBasket.java b/src/org/mxchange/jshopcore/model/basket/ShopBasket.java index fa5213b..c24221b 100644 --- a/src/org/mxchange/jshopcore/model/basket/ShopBasket.java +++ b/src/org/mxchange/jshopcore/model/basket/ShopBasket.java @@ -16,12 +16,10 @@ */ package org.mxchange.jshopcore.model.basket; -import java.sql.SQLException; - /** * A basket for orderable items * - * @author Roland Haeder + * @author Roland Haeder */ public class ShopBasket extends BaseBasket implements Basket { /** @@ -31,9 +29,8 @@ public class ShopBasket extends BaseBasket implements Basket< /** * Default constructor to be able to throw exceptions from super constructor - * @throws java.sql.SQLException If an SQL error occurs */ - public ShopBasket () throws SQLException { + public ShopBasket () { // Call super constructor super(); } diff --git a/src/org/mxchange/jshopcore/model/category/BaseCategory.java b/src/org/mxchange/jshopcore/model/category/BaseCategory.java index fe5f491..b83b6bb 100644 --- a/src/org/mxchange/jshopcore/model/category/BaseCategory.java +++ b/src/org/mxchange/jshopcore/model/category/BaseCategory.java @@ -21,7 +21,7 @@ import java.util.Objects; /** * A general product category class * - * @author Roland Haeder + * @author Roland Haeder */ public abstract class BaseCategory implements Category, Comparable { /** diff --git a/src/org/mxchange/jshopcore/model/category/Category.java b/src/org/mxchange/jshopcore/model/category/Category.java index d0432fd..a1c4962 100644 --- a/src/org/mxchange/jshopcore/model/category/Category.java +++ b/src/org/mxchange/jshopcore/model/category/Category.java @@ -21,7 +21,7 @@ import java.io.Serializable; /** * An interface for categories * - * @author Roland Haeder + * @author Roland Haeder */ public interface Category extends Serializable { diff --git a/src/org/mxchange/jshopcore/model/category/ProductCategory.java b/src/org/mxchange/jshopcore/model/category/ProductCategory.java index 9bd2a61..d89077f 100644 --- a/src/org/mxchange/jshopcore/model/category/ProductCategory.java +++ b/src/org/mxchange/jshopcore/model/category/ProductCategory.java @@ -18,7 +18,7 @@ package org.mxchange.jshopcore.model.category; /** * A product category - * @author Roland Haeder + * @author Roland Haeder */ public class ProductCategory extends BaseCategory { /** diff --git a/src/org/mxchange/jshopcore/model/customer/Customer.java b/src/org/mxchange/jshopcore/model/customer/Customer.java index 4524d65..21cf303 100644 --- a/src/org/mxchange/jshopcore/model/customer/Customer.java +++ b/src/org/mxchange/jshopcore/model/customer/Customer.java @@ -21,7 +21,7 @@ import org.mxchange.jcore.model.contact.Contact; /** * A customer interface * - * @author Roland Haeder + * @author Roland Haeder */ public interface Customer extends Contact { } diff --git a/src/org/mxchange/jshopcore/model/customer/ShopCustomer.java b/src/org/mxchange/jshopcore/model/customer/ShopCustomer.java index 209dd71..3e0e0cf 100644 --- a/src/org/mxchange/jshopcore/model/customer/ShopCustomer.java +++ b/src/org/mxchange/jshopcore/model/customer/ShopCustomer.java @@ -21,7 +21,7 @@ import org.mxchange.jcore.model.contact.BaseContact; /** * A shop customer class. * - * @author Roland Haeder + * @author Roland Haeder */ public class ShopCustomer extends BaseContact implements Customer { /** diff --git a/src/org/mxchange/jshopcore/model/item/BaseItem.java b/src/org/mxchange/jshopcore/model/item/BaseItem.java index b788238..7a8d50c 100644 --- a/src/org/mxchange/jshopcore/model/item/BaseItem.java +++ b/src/org/mxchange/jshopcore/model/item/BaseItem.java @@ -24,7 +24,7 @@ import org.mxchange.jshopcore.model.product.Product; * An item (addedable to a basket) could respresent a product or a discount * coupon. This depends on the type of the item. * - * @author Roland Haeder + * @author Roland Haeder */ public abstract class BaseItem implements AddableBasketItem, Comparable { diff --git a/src/org/mxchange/jshopcore/model/item/BasketItem.java b/src/org/mxchange/jshopcore/model/item/BasketItem.java index a56ccc2..0e19354 100644 --- a/src/org/mxchange/jshopcore/model/item/BasketItem.java +++ b/src/org/mxchange/jshopcore/model/item/BasketItem.java @@ -22,7 +22,7 @@ import org.mxchange.jshopcore.model.product.Product; /** * A general basket item * - * @author Roland Haeder + * @author Roland Haeder */ public class BasketItem extends BaseItem implements AddableBasketItem { /** diff --git a/src/org/mxchange/jshopcore/model/product/BaseProduct.java b/src/org/mxchange/jshopcore/model/product/BaseProduct.java index a2fb9b2..1120636 100644 --- a/src/org/mxchange/jshopcore/model/product/BaseProduct.java +++ b/src/org/mxchange/jshopcore/model/product/BaseProduct.java @@ -10,7 +10,7 @@ import java.util.Objects; /** * A general product class * - * @author Roland Haeder + * @author Roland Haeder */ public abstract class BaseProduct implements Product, Comparable { diff --git a/src/org/mxchange/jshopcore/model/product/GenericProduct.java b/src/org/mxchange/jshopcore/model/product/GenericProduct.java index ecd215a..a7e8f88 100644 --- a/src/org/mxchange/jshopcore/model/product/GenericProduct.java +++ b/src/org/mxchange/jshopcore/model/product/GenericProduct.java @@ -19,7 +19,7 @@ package org.mxchange.jshopcore.model.product; /** * Generic product class * - * @author Roland Haeder + * @author Roland Haeder * TODO: Find a better name */ public class GenericProduct extends BaseProduct implements Product { diff --git a/src/org/mxchange/jshopcore/model/product/Product.java b/src/org/mxchange/jshopcore/model/product/Product.java index efcdd7b..79b94f9 100644 --- a/src/org/mxchange/jshopcore/model/product/Product.java +++ b/src/org/mxchange/jshopcore/model/product/Product.java @@ -21,7 +21,7 @@ import java.io.Serializable; /** * An interface for in database storable products * - * @author Roland Haeder + * @author Roland Haeder */ public interface Product extends Serializable { /**