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 * Shows whether this CSOrder is active or not
016 */
017 private boolean isActive = true;
018
019 /**
020 * The time since this CSOrder exist
021 */
022 private Long time;
023
024 /**
025 * @param s the name of the CSOrder
026 * @param active if true the CSOrder will be active
027 */
028 public CSOrder(String s, boolean active) {
029 super(s, SMarket.getArticleCatalog());
030 Shop.getTheShop().addStock(this);
031 isActive = active;
032 }
033
034 /**
035 * Removes a number of items from this CSOrder
036 *
037 * @param sKey the name of the StockItem
038 * @param nCount the number of StockItems, that will be removed
039 * @param db the databasket related to this transaction
040 */
041 public void remove(String sKey, int nCount, DataBasket db){
042 try {
043 super.remove(sKey, nCount, db);
044 } catch (VetoException e) {
045 System.err.println(e.getMessage());
046 }
047 }
048
049 /**
050 * Removes all items with the specified key from this CSOrder
051 *
052 * @param sKey the name of the StockItem
053 * @return the number of items that are removed
054 */
055 public int removeAll(String sKey){
056 int i = this.countItems(sKey, null);
057 remove(sKey, i, null);
058 return i;
059 }
060
061 /**
062 * Returns whether this CSOrder is active or not
063 *
064 * @return true if this CSOrder is active, otherwise false
065 */
066 public boolean isActive() {
067 return isActive;
068 }
069
070 /**
071 * Sets this CSOrder active or not
072 *
073 * @param active if true the CSOrder will be active
074 */
075 public void setActive(boolean active) {
076 isActive = active;
077 }
078
079 /**
080 * Returns the time since this CSOrder exist
081 *
082 * @return the time since this CSOrder exist in milliseconds
083 */
084 public Long getTime() {
085 return time;
086 }
087
088 /**
089 * Returns a new CSOrder with a combination of owner-key and time as its key
090 *
091 * @param customer the owner of the new CSOrder
092 * @param active if true the CSOrder will be active
093 * @return the new CSOrder
094 */
095 public static CSOrder create(String customer, boolean active){
096 Long t = new Long(System.currentTimeMillis());
097 CSOrder cso = new CSOrder(customer+t.toString(), active);
098 cso.time = t;
099 return cso;
100 }
101 }