//second searching - exact amount
 private bool LookingExactAmount(DataSideY minDataY, int amount, ref bool sold, DataSideY maxDataY, DoubleLinkedList <DataSideX> suitableSizesX)
 {
     sold = false;
     foreach (var itemX in suitableSizesX)
     {
         DoubleLinkedList <DataSideY> suitableSizesY = itemX.tree.ScanBetweenTwoValues(minDataY, maxDataY);
         foreach (var itemY in suitableSizesY)
         {
             if (itemY.quantity < amount)
             {
                 return(false);
             }
             else if (itemY.quantity >= amount)
             {
                 sold = true;
                 if (itemY.quantity == amount)
                 {
                     myShowMessage($"We have last {amount} box/es (depth={itemX.lengthX}, height={itemY.lengthY}) for you. You can get it/them. We deleted this type of boxes from stock.");
                 }
                 else
                 {
                     myShowMessage($"We have  {amount} box/es for you. Also we have {itemY.quantity} more box/es (depth={itemX.lengthX}, height={itemY.lengthY}). If you need - you can buy it/them.");
                 }
                 RemoveFromTree(itemX, itemY, amount);
             }
             if (sold)
             {
                 break;
             }
         }
         if (sold)
         {
             break;
         }
     }
     return(sold);
 }
Exemplo n.º 2
0
        private DoubleLinkedList <T> ScanBetweenTwoValues(T min, T max, Node searchNode, DoubleLinkedList <T> suitableSizes)
        {
            if (searchNode == null)
            {
                return(null);
            }

            if (min.CompareTo(searchNode.data) <= 0 && max.CompareTo(searchNode.data) >= 0)
            {
                suitableSizes.AddLast(searchNode.data);
            }
            if (min.CompareTo(searchNode.data) < 0)
            {
                ScanBetweenTwoValues(min, max, searchNode.left, suitableSizes);
            }
            if (max.CompareTo(searchNode.data) > 0)
            {
                ScanBetweenTwoValues(min, max, searchNode.right, suitableSizes);
            }
            return(suitableSizes);
        }
        public void SellBoxes(double x, double y, int amount = 1, double increasingX = 20, double increasingY = 20,
                              bool isDividable = true, bool isAcceptLess             = true)
        {
            bool      sold       = false;
            int       soldAmount = 0;
            double    maxX       = (x * increasingX) / 100 + x;
            double    maxY       = (y * increasingY) / 100 + y;
            DataSideX minDataX   = new DataSideX(x);
            DataSideX maxDataX   = new DataSideX(maxX);
            DataSideY minDataY   = new DataSideY(y);
            DataSideY maxDataY   = new DataSideY(maxY);

            if (mainTree.root == null)
            {
                myShowMessage($"The stock is empty.");
                return;
            }

            //first we check if we have suitable box with side x
            DoubleLinkedList <DataSideX> suitableSizesX = mainTree.ScanBetweenTwoValues(minDataX, maxDataX);

            if (suitableSizesX.end == null)
            {
                myShowMessage($"Sorry we haven't box with sizes like you need.");
            }
            else
            {
                //if we have suitable boxes by x side - first we will try to find exact box and amount that user ask
                suitableSizesX = mainTree.ScanBetweenTwoValues(minDataX, minDataX);
                sold           = LokingExactSizeAndAmount(minDataY, amount, ref sold, suitableSizesX);
                StringBuilder sb = new StringBuilder();
                if (!sold)
                {
                    //if we have didn't find exact box - we will try to find exact amount that user ask without sorting
                    suitableSizesX = mainTree.ScanBetweenTwoValues(minDataX, maxDataX);
                    sold           = LookingExactAmount(minDataY, amount, ref sold, maxDataY, suitableSizesX);
                }
                //if we have didn't find exact box from previous seaching- we will try to find some boxes that suitable for user input
                if (!sold && isDividable && isAcceptLess)
                {
                    sb.AppendLine("We have box/es like you need but with sorting.");
                    soldAmount = 0;
                    int rest = amount;
                    foreach (var itemX in suitableSizesX)
                    {
                        DoubleLinkedList <DataSideY> suitableSizesY = itemX.tree.ScanBetweenTwoValues(minDataY, maxDataY);
                        foreach (var itemY in suitableSizesY)
                        {
                            if (soldAmount == amount)
                            {
                                sold = true;
                                sb.AppendLine($"We sold to you {soldAmount} box/es.");
                                myShowMessage(sb.ToString());
                                break;
                            }
                            else if (itemY.quantity <= rest)
                            {
                                sb.AppendLine($"x={itemX.lengthX} y={itemY.lengthY} quantity={itemY.quantity}");
                                soldAmount = soldAmount + itemY.quantity;
                                RemoveFromTree(itemX, itemY, itemY.quantity);
                                rest = amount - soldAmount;
                            }
                            else
                            {
                                sb.AppendLine($"x={itemX.lengthX} y={itemY.lengthY} quantity={rest}");
                                soldAmount = soldAmount + rest;
                                RemoveFromTree(itemX, itemY, rest);
                                rest = amount - soldAmount;
                            }
                        }
                        if (soldAmount == amount)
                        {
                            sold = true;
                            sb.AppendLine($"We sold to you {soldAmount} box/es.");
                            myShowMessage(sb.ToString());
                            break;
                        }
                    }
                    if (!sold)
                    {
                        sb.AppendLine($"But we cant find {rest} box/es. So we sold to you only {soldAmount} box/es.");
                        myShowMessage(sb.ToString());
                    }
                }
                else if (!sold && isDividable && !isAcceptLess)
                {
                    soldAmount = 0;
                    int rest = amount;

                    foreach (var itemX in suitableSizesX)
                    {
                        DoubleLinkedList <DataSideY> suitableSizesY = itemX.tree.ScanBetweenTwoValues(minDataY, maxDataY);
                        foreach (var itemY in suitableSizesY)
                        {
                            soldAmount += itemY.quantity;
                            if (soldAmount >= amount)
                            {
                                sb.AppendLine("We have box/es like you need but with sorting.");
                                break;
                            }
                        }
                        if (soldAmount >= amount)
                        {
                            break;
                        }
                    }
                    if (soldAmount < amount)
                    {
                        myShowMessage($"Sorry we haven't {amount} box/es like you need.");
                    }
                    else
                    {
                        soldAmount = 0;
                        foreach (var itemX in suitableSizesX)
                        {
                            DoubleLinkedList <DataSideY> suitableSizesY = itemX.tree.ScanBetweenTwoValues(minDataY, maxDataY);
                            foreach (var itemY in suitableSizesY)
                            {
                                if (soldAmount == amount)
                                {
                                    sold = true;
                                    sb.AppendLine($"We sold to you {soldAmount} box/es like you wanted.");
                                    myShowMessage(sb.ToString());
                                    break;
                                }
                                if (itemY.quantity <= rest)
                                {
                                    sb.AppendLine($"x={itemX.lengthX} y={itemY.lengthY} quantity={itemY.quantity}");
                                    soldAmount += itemY.quantity;
                                    RemoveFromTree(itemX, itemY, itemY.quantity);
                                    rest = amount - soldAmount;
                                }
                                else
                                {
                                    sb.AppendLine($"x={itemX.lengthX} y={itemY.lengthY} quantity={rest}");
                                    soldAmount += rest;
                                    RemoveFromTree(itemX, itemY, rest);
                                    rest = amount - soldAmount;
                                }
                            }
                            if (soldAmount == amount)
                            {
                                sold = true;
                                sb.AppendLine($"We sold to you {soldAmount} box/es like you wanted.");
                                myShowMessage(sb.ToString());
                                break;
                            }
                        }
                    }
                }
                else if (!sold && !isDividable && isAcceptLess)
                {
                    int maxAmount = 0;
                    foreach (var itemX in suitableSizesX)
                    {
                        DoubleLinkedList <DataSideY> suitableSizesY = itemX.tree.ScanBetweenTwoValues(minDataY, maxDataY);
                        foreach (var itemY in suitableSizesY)
                        {
                            if (itemY.quantity > maxAmount)
                            {
                                maxAmount = itemY.quantity;
                            }
                        }
                    }
                    {
                        foreach (var itemX in suitableSizesX)
                        {
                            DoubleLinkedList <DataSideY> suitableSizesY = itemX.tree.ScanBetweenTwoValues(minDataY, maxDataY);
                            foreach (var itemY in suitableSizesY)
                            {
                                if (itemY.quantity == maxAmount)
                                {
                                    sold = true;
                                    RemoveFromTree(itemX, itemY, maxAmount);
                                    myShowMessage($"We have only {maxAmount} box/es like you need (x={itemX.lengthX} y={itemY.lengthY}). You can take it/them.");
                                }
                                if (sold)
                                {
                                    break;
                                }
                            }
                            if (sold)
                            {
                                break;
                            }
                        }
                    }
                }
                else
                {
                    if (!sold)
                    {
                        myShowMessage($"Sorry we haven't {amount} box/es like you need.");
                    }
                }
            }
        }