public int RemoveProduct(IProduct product, int amount = 1) { if (!CartProductIds.Contains(product.GetId())) { throw new InvalidOperationException("No such product in a cart"); } ICartItem item = CartItems.Find(cartItem => cartItem.GetProduct().Equals(product)); if (item == null) { throw new InvalidOperationException("Item exist in HashSet but not in CartItems"); } if (item.GetAmount() != amount) { return(item.DecreaseAmountOn(amount)); } // here item should be deleted CartProductIds.Remove(product.GetId()); CartItems.RemoveAll(cartItem => cartItem.GetProduct().Equals(product)); return(0); }