Пример #1
0
 /// <summary>
 /// Adds Item to the order if item is not in combo
 /// Adds items to the combo if in combo
 /// Adds combo to the order if not combo
 /// </summary>
 /// <param name="that">item to add</param>
 public void AddItem(IOrderItem that)
 {
     if (OrderItem)
     {
         if (that is Entree)
         {
             Comb.meal.Entree = (Entree)that;
         }
         else if (that is Side)
         {
             Comb.meal.Side = (Side)that;
         }
         else if (that is Drink)
         {
             Comb.meal.Drink = (Drink)that;
         }
         ChangeLayerIndex(-1);
         BackButtonClick(new object(), new RoutedEventArgs());
     }
     if (isCombo)
     {
         if (that is Entree)
         {
             Comb.meal.Entree = (Entree)that;
         }
         else if (that is Side)
         {
             Comb.meal.Side = (Side)that;
         }
         else if (that is Drink)
         {
             Comb.meal.Drink = (Drink)that;
         }
         ChangeLayerIndex(-1);
         BackButtonClick(new object(), new RoutedEventArgs());
     }
     else
     {
         Right.AddItem(that);
         ChangeLayerIndex(-1);
         BackButtonClick(new object(), new RoutedEventArgs());
     }
 }
Пример #2
0
        /// <summary>
        /// Adds the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns>True if the item was added or false if it already existed and was not </returns>
        public bool AddItem(TItem item)
        {
            bool isBoundingNode;
            int  comparedToFirst = 0;

            //Is this the bounding node for the new item? If the node is empty it is considered to be the bounding node.
            if (ItemCount == 0)
            {
                isBoundingNode = true;
            }
            else
            {
                //Compare the item to be inserted to the first item in the data
                comparedToFirst = item.CompareTo(m_data[0]);
                isBoundingNode  = ((comparedToFirst >= 0) && (item.CompareTo(m_data[ItemCount - 1]) <= 0));
            }

            if (isBoundingNode)
            {
                //Is there space in this node?
                if (ItemCount < m_data.Length)
                {
                    //This is the bounding node, add the new item
                    return(InsertInCurrentNode(item, ShiftType.NoShift));
                }
                else
                {
                    //Copy the old minimum. This current item will be inserted into this node
                    TItem oldMinimum = m_data[0];

                    if (!InsertInCurrentNode(item, ShiftType.FullShiftToLeft))
                    {
                        return(false);
                    }

                    //Add the old minimum
                    if (Left == null)
                    {
                        //There is no left child, so create it
                        Left = CreateChild(oldMinimum);
                        UpdateHeight(HeightUpdateType.CurrentLevelOnly);
                        Rebalance(BalanceType.StopAfterFirstRotate);
                        return(true);
                    }
                    else
                    {
                        //Add the old minimum to the left child
                        return(Left.AddItem(oldMinimum));
                    }
                }
            }
            else
            {
                //If the item is less than the minimum and there is a left node, follow it
                if ((Left != null) && (comparedToFirst < 0))
                {
                    return(Left.AddItem(item));
                }

                //If the item is less than the maximum and there is a right node, follow it
                if ((Right != null) && (comparedToFirst > 0))
                {
                    return(Right.AddItem(item));
                }

                //If we are here then, there is no bounding node for this value.
                //Is there place in this node
                if (ItemCount < m_data.Length)
                {
                    //There is place in this node so add the new value. However since this value
                    // must be the new minimum or maximum (otherwise it would have found a bounding
                    // node) dont call InsertInCurrentNode which would do a binary search to find
                    // an insert location. Rather check for min/max and add it here.
                    if (comparedToFirst > 0)
                    {
                        //The item is greater than the minimum, therfore it must be the new maximum.
                        // Add it to the end of the array
                        m_data[ItemCount] = item;
                    }
                    else
                    {
                        //The item is the new miminum. Shift the array up and insert it.
                        Array.Copy(m_data, 0, m_data, 1, ItemCount);
                        m_data[0] = item;
                    }

                    ItemCount++;
                }
                else
                {
                    TTreeNode <TItem, TNode> newChild = CreateChild(item);

                    //Add it as the the left or the right child
                    if (comparedToFirst < 0)
                    {
                        Left = (TNode)newChild;
                    }
                    else
                    {
                        Right = (TNode)newChild;
                    }

                    UpdateHeight(HeightUpdateType.UpdateAllUpwards);
                    Rebalance(BalanceType.StopAfterFirstRotate);
                    return(true);
                }
            }

            return(true);
        }