/// <summary> /// Remove from stock 'amountToBuy' products /// </summary> /// <param name="numberToRemove"></param> /// <returns>new amount id succeed ,otherwise -1</returns> private int removeFromStock(Product p, int amountToBuy) { if (!amountIsLegal(p.amount - amountToBuy)) { return(-1); } p.amount -= amountToBuy; WorkShop.Update(this); return(p.amount); }
/// <summary> /// succseed - callback that return the products to the stock. if fail - returns null /// </summary> /// <param name="user"></param> /// <param name="p"></param> /// <param name="amountToBuy"></param> /// <returns> succseed - callback that return the products to the stock., fail - returns null</returns> public callback buyProduct(Product p, int amountToBuy) { callback callback = delegate() { if (GetStockAsDictionary().ContainsKey(p.getId())) { GetStockAsDictionary()[p.getId()].amount += amountToBuy; } WorkShop.Update(this); }; if (!GetStockAsDictionary().ContainsKey(p.getId()) || removeFromStock(GetStockAsDictionary()[p.getId()], amountToBuy) == -1) { return(null); } return(callback); }