Supermarket.Product.Split C# (CSharp) Method

Split() public method

Splits this product category in two with the new product category inheriting the specified quantity from this product.
public Split ( int quantity ) : Product
quantity int The quantity of product to be removed from this product and placed into the new /// product
return Product
        public Product Split(int quantity=0)
        {
            Product product = new Product(this.Model, this.Price);

            // if the some of the quantity of this product is being shared with the new product
            if (quantity > 0)
            {
                // Remove the desired quantity from this product
                this.Quantity -= quantity;

                // If there wasn't enough in this product category
                if (this.Quantity < 0)
                {
                    // Add to the new product whatever quantity is available
                    product.Quantity = quantity + this.Quantity;
                    this.Quantity = 0;      // This product category is now empty
                }
                else
                {
                    // Otherwise, split the amount of available quantity in the desired amount
                    product.Quantity = quantity;
                }
            }

            // Reference to a new product
            return product;
        }