--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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 <T> Any item that is or extends the interface
+ * @param item An instance of a T item
+ */
+ public <T extends AddableBasketItem>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));
+ }
+}
/**
* An exception thrown when the given title is already used
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
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);
+ }
}
/**
* An exception thrown when the given title is already used
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
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
+ }
}
/**
* An interface for addable basket items
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
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);
*/
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<roland@mxchange.org>
* @param <T> Any instance that implements AddableBasketItem
*/
public abstract class BaseBasket<T extends AddableBasketItem> implements Basket<T> {
*/
private static final long serialVersionUID = 782_396_762_230_845_717L;
+ /**
+ * Ordered item list
+ */
+ private final Deque<T> 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<Long, T> getAll () {
// Init map
- // TODO: Map<Long, T> map = ((BasketFrontend) this.getFrontend()).getAll();
- Map<Long, T> map = null;
+ Map<Long, T> 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;
@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
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);
}
}
import java.io.Serializable;
import java.util.Map;
+import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException;
/**
* An interface for baskets
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
* @param <T> Any addable basket items
*/
public interface Basket<T extends AddableBasketItem> 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
*/
public boolean isEmpty ();
- /**
- * Initializes this instance with given ServletContext
- */
- public void init ();
-
/**
* Some "getter" for all entries in this basket
*
*/
package org.mxchange.jshopcore.model.basket;
-import java.sql.SQLException;
-
/**
* A basket for orderable items
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
public class ShopBasket extends BaseBasket<AddableBasketItem> implements Basket<AddableBasketItem> {
/**
/**
* 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();
}
/**
* A general product category class
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
public abstract class BaseCategory implements Category, Comparable<Category> {
/**
/**
* An interface for categories
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
public interface Category extends Serializable {
/**
* A product category
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
public class ProductCategory extends BaseCategory {
/**
/**
* A customer interface
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
public interface Customer extends Contact {
}
/**
* A shop customer class.
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
public class ShopCustomer extends BaseContact implements Customer {
/**
* 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<roland@mxchange.org>
*/
public abstract class BaseItem implements AddableBasketItem, Comparable<AddableBasketItem> {
/**
* A general basket item
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
public class BasketItem extends BaseItem implements AddableBasketItem {
/**
/**
* A general product class
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
public abstract class BaseProduct implements Product, Comparable<Product> {
/**
* Generic product class
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
* TODO: Find a better name
*/
public class GenericProduct extends BaseProduct implements Product {
/**
* An interface for in database storable products
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
*/
public interface Product extends Serializable {
/**