for-loop&的逻辑难度if-else语句

时间:2014-01-01 02:41:56

标签: c# asp.net database logic

我在做包装系统时遇到一些逻辑问题。让我先解释一下情况,每个类别都有 listCapacity 。还有一个 SPUItemList 来存储标准包装单位项目。

首先,我需要检查SPUItemList中物品的库存是否足够。如果他们还不够,我会从 prodSubstitute 获得。 prodSubstitute是按stockLevel和每个类别降序排序的项目列表。一旦我加入 prodIDList ,我就减去listCapacity。

如果它们足够stockLevel,我马上加入prodIDList并减去listCapacity。

以下是代码:

        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--;
            }

所以我的问题是,如果它们足够stockLevel并且我添加到prodIDList中,我该如何修复我的代码以便他们知道这个类别的listCapacity已被扣除?因为到目前为止,我对这部分代码存在一些逻辑问题。

很抱歉我的解释不好,我希望你们都明白我在说什么。

提前致谢。

1 个答案:

答案 0 :(得分:1)

我会这样做(用categoryCheck替换listCapacity并检查它):

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);
        }
    }
}