0

I am having some logic problem when doing packaging system. Let me explain the situation first, there is listCapacity for each category. There is also a SPUItemList to store the standard packing unit items.

First, I need to check whether the stock for items in SPUItemList is enough or not. If they are not enough, I get from the prodSubstitute. prodSubstitute is a list of items sorted descending by stockLevel and by each category. Once I added into prodIDList, I minus the listCapacity.

If they are enough stockLevel, I straight away add into prodIDList and minus the listCapacity.

Here is the codes:

        List<DistributionStandardPackingUnitItems> SPUItemList = new     List<DistributionStandardPackingUnitItems>();
        List<string> prodIDList = new List<string>();
        List<DistributionStandardPackingUnitItems> distSPUItem = new List<DistributionStandardPackingUnitItems>();
        //Get total amount of packages needed by each distribution
        packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);

        //Get the items in standard packing unit
        SPUItemList = packBLL.getAllSPUItemByDistributionID(distributionID);
        for (int i = 0; i < SPUItemList.Count; i++)
        {
            //Get the product quantity of each item in standard packing unit
            productQuantity = Convert.ToInt32(SPUItemList[i].productQuantity);

            //Get the total stock unit of each product in standard packing unit
            totalProductUnit = prodPackBLL.getTotalProductUnit(SPUItemList[i].id);

            if ((productQuantity * packagesNeeded) > totalProductUnit)
            {
                //Get the category name of the item which has not enough stock
                category = SPUItemList[i].categoryName;

                //Get the list of substitute product with top 5 highest storage level
                List<ProductPacking> prodSubstitute = new List<ProductPacking>();

                //Find list of substitute with highest stock level and replace the product
                prodSubstitute = prodPackBLL.getProductIDWithHighestStock(category);

                for (int count = 0; count < prodSubstitute.Count; count++)
                {
                    //To prevent duplication of same product and check for the listCapacity
                    if (prodSubstitute[count].id != SPUItemList[i].id && !prodIDList.Contains(prodSubstitute[count].id) && count < listCapacity)
                    {
                        prodIDList.Add(prodSubstitute[count].id);
                        listCapacity--;
                    }
                }
            }
            else
            {
                //If the stock is enough, add it into the prodIDList straight away
                prodIDList.Add(SPUItemList[i].id);
                listCapacity--;
            }

So my question is, if they are enough stockLevel and I added into prodIDList, how can I fix my code so that they know the listCapacity for this category has been deducted? Because so far I have some logic problem with this portion of codes.

Sorry for my poor explanation and I hope you all understand what am I talking about.

Thanks in advance.

3
  • Is listCapacity a property of Category? Commented Jan 1, 2014 at 3:05
  • No, it's just a string variable for me to check the for-loop Commented Jan 1, 2014 at 3:06
  • Please see my answer below. Commented Jan 1, 2014 at 3:20

1 Answer 1

1

I would do like this (replaced the listCapacity with categoryCheck and checked it):

List<string> lstCategory = new List<string>();
List<DistributionStandardPackingUnitItems> SPUItemList = new     List<DistributionStandardPackingUnitItems>();
List<string> prodIDList = new List<string>();
List<DistributionStandardPackingUnitItems> distSPUItem = new List<DistributionStandardPackingUnitItems>();
//Get total amount of packages needed by each distribution
packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);

    //Get the items in standard packing unit
SPUItemList = packBLL.getAllSPUItemByDistributionID(distributionID);

for (int i = 0; i < SPUItemList.Count; i++)
{
    //Get the product quantity of each item in standard packing unit
    productQuantity = Convert.ToInt32(SPUItemList[i].productQuantity);

    //Get the total stock unit of each product in standard packing unit
    totalProductUnit = prodPackBLL.getTotalProductUnit(SPUItemList[i].id);

    if ((productQuantity * packagesNeeded) > totalProductUnit)
    {
        //Get the category name of the item which has not enough stock
        category = SPUItemList[i].categoryName;

        //Get the list of substitute product with top 5 highest storage level
        List<ProductPacking> prodSubstitute = new List<ProductPacking>();

        //Find list of substitute with highest stock level and replace the product
        prodSubstitute = prodPackBLL.getProductIDWithHighestStock(category);

        for (int count = 0; count < prodSubstitute.Count; count++)
        {
            //To prevent duplication of same product and check for the listCapacity
            if (prodSubstitute[count].id != SPUItemList[i].id && !prodIDList.Contains(prodSubstitute[count].id) &&  !(lstCategory.Where(x=>x.Equals(category)).Select(x=>x).Count() > 2))
            {
                prodIDList.Add(prodSubstitute[count].id);
                //listCapacity--;
                lstCategory.Add(category);
            }
        }
    }
    else
    {
        //If the stock is enough, add it into the prodIDList straight away
        category = SPUItemList[i].categoryName;
        if(!prodIDList.Contains(SPUItemList[i].id) &&   !(lstCategory.Where(x=>x.Equals(category)).Select(x=>x).Count() > 2))
        {
            prodIDList.Add(SPUItemList[i].id);
            //listCapacity--;
            lstCategory.Add(category);
        }
    }
}
Sign up to request clarification or add additional context in comments.

17 Comments

But I need to limit the listCapacity for each category to 2. So each category may have 2 products
@Gwen - can each category have same product twice?
Nope, cannot have duplicate and there is a check for duplicate inside the if statement. Unfortunately nope, there is still one product for each category. I am trying to set 2 products for each category
Okay, now limited to 2 products for each category.
@afzaluluh Let's say I got product 1, 2, 3, 4 and 5. My product 1 has enough storage so it's added into the prodIDList first. Then I loop thru. Product 2 is not enough so it skips. And product 3, 4 and 5 all enough storage. It supposed to return me product 1 and 3 only. Instead, it returns me 3, and 4.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.