001 package market;
002
003 import sale.Shop;
004 import data.DataBasket;
005 import data.events.VetoException;
006 import data.ooimpl.CountingStockImpl;
007
008 /**
009 * A CountingStockImpl which is used to store orders of customers,
010 * which can be set active or not
011 */
012 public class CSOrder extends CountingStockImpl{
013
014 /**
015 * ID for serialization.
016 */
017 private static final long serialVersionUID = 6488120137458624557L;
018
019 /**
020 * Shows whether this CSOrder is active or not
021 */
022 private boolean isActive = true;
023
024 /**
025 * The time since this CSOrder exist
026 */
027 private Long time;
028
029 /**
030 * @param s the name of the CSOrder
031 * @param active if true the CSOrder will be active
032 */
033 public CSOrder(String s, boolean active) {
034 super(s, SMarket.getArticleCatalog());
035 Shop.getTheShop().addStock(this);
036 isActive = active;
037 }
038
039 /**
040 * Removes a number of items from this CSOrder
041 *
042 * @param sKey the name of the StockItem
043 * @param nCount the number of StockItems, that will be removed
044 * @param db the databasket related to this transaction
045 */
046 public void remove(String sKey, int nCount, DataBasket db){
047 try {
048 super.remove(sKey, nCount, db);
049 } catch (VetoException e) {
050 System.err.println(e.getMessage());
051 }
052 }
053
054 /**
055 * Removes all items with the specified key from this CSOrder
056 *
057 * @param sKey the name of the StockItem
058 * @return the number of items that are removed
059 */
060 public int removeAll(String sKey){
061 int i = this.countItems(sKey, null);
062 remove(sKey, i, null);
063 return i;
064 }
065
066 /**
067 * Returns whether this CSOrder is active or not
068 *
069 * @return true if this CSOrder is active, otherwise false
070 */
071 public boolean isActive() {
072 return isActive;
073 }
074
075 /**
076 * Sets this CSOrder active or not
077 *
078 * @param active if true the CSOrder will be active
079 */
080 public void setActive(boolean active) {
081 isActive = active;
082 }
083
084 /**
085 * Returns the time since this CSOrder exist
086 *
087 * @return the time since this CSOrder exist in milliseconds
088 */
089 public Long getTime() {
090 return time;
091 }
092
093 /**
094 * Returns a new CSOrder with a combination of owner-key and time as its key
095 *
096 * @param customer the owner of the new CSOrder
097 * @param active if true the CSOrder will be active
098 * @return the new CSOrder
099 */
100 public static CSOrder create(String customer, boolean active){
101 Long t = new Long(System.currentTimeMillis());
102 CSOrder cso = new CSOrder(customer+t.toString(), active);
103 cso.time = t;
104 return cso;
105 }
106 }