【问题标题】:Logic difficulty in for-loop & if-else statementfor-loop 和 if-else 语句中的逻辑困难
【发布时间】:2014-01-01 02:41:56
【问题描述】:

我在做包装系统时遇到了一些逻辑问题。让我先解释一下情况,每个类别都有 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 已被扣除?因为到目前为止我对这部分代码有一些逻辑问题。

对不起,我的解释很糟糕,我希望你们都明白我在说什么。

提前致谢。

【问题讨论】:

  • listCapacity 是 Category 的属性吗?
  • 不,它只是一个字符串变量让我检查for循环
  • @afzalulh 有什么建议吗?
  • 请看下面我的回答。

标签: c# asp.net database logic


【解决方案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);
        }
    }
}

【讨论】:

  • 但我需要将每个类别的 listCapacity 限制为 2。所以每个类别可能有 2 个产品
  • @Gwen - 每个类别可以有两次相同的产品吗?
  • 不,不能有重复,并且在 if 语句中有重复检查。不幸的是,没有,每个类别仍然有一个产品。我正在尝试为每个类别设置 2 个产品
  • 好的,现在每个类别仅限 2 个产品。
  • @afzaluluh 假设我有产品 1、2、3、4 和 5。我的产品 1 有足够的存储空间,因此首先将其添加到 prodIDList 中。然后我循环。产品 2 不够,所以它会跳过。并且产品 3、4 和 5 都足够存储。它应该只返回我的产品 1 和 3。相反,它返回我 3 和 4。
猜你喜欢
  • 2018-04-16
  • 2014-02-19
  • 1970-01-01
  • 2021-07-31
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
相关资源
最近更新 更多