我想避免很多if-else语句

时间:2014-03-01 16:20:39

标签: java if-statement conditional-statements

我想避免使用if-else语句。

我想使用另一种解决方案,因为如果有情况,我必须使用很多条件。这样效率不高。你可以在我的代码中看到我使用了很多if else语句,但这不是我想要的,有些人谈论地图

    if(purchaseAmount <= 100) {
        if(numberOfItems <= 3) { 
            switch(deliveryDay) {
                case NEXT_DAY:
                    cost = 25;
                    break;
                case IN_TWO_DAYS:
                    cost = 10;
                    break;
                case IN_A_WEEK:
                    cost = numberOfItems * 1.50;
                    break;
            }
        } else {
            switch(deliveryDay) {
                case NEXT_DAY:
                    cost = numberOfItems * 6.00;
                    break;
                case IN_TWO_DAYS:
                    cost = numberOfItems * 2.50;
                    break;
                case IN_A_WEEK:
                    cost = 0;
                    break;
            }
        }
    } else { /* purchaseAmount > 100 */
        if(numberOfItems <= 3) { 
            switch(deliveryDay) {
                case NEXT_DAY:
                    cost = 35;
                    break;
                case IN_TWO_DAYS:
                    cost = 15;
                    break;
                case IN_A_WEEK:
                    cost = 10;
                    break;
            }
        } else {
            switch(deliveryDay) {
                case NEXT_DAY:
                    cost = numberOfItems * 7.50;
                    break;
                case IN_TWO_DAYS:
                    cost = numberOfItems * 3.50;
                    break;
                case IN_A_WEEK:
                    cost = numberOfItems * 2.50;
                    break;
            }
        }
    }
    return cost;
}
public double calculateShipmentCost(Shipment shipment) { 

    double cost = 0;
    if(purchaseAmount <= 100) {
        if(numberOfItems <= 3) { 
            switch(deliveryDay) {
                case NEXT_DAY:
                    cost = 25;
                    break;
                case IN_TWO_DAYS:
                    cost = 10;
                    break;

        } else {
            switch(deliveryDay) {
                case NEXT_DAY:
                    cost = numberOfItems * 6.00;
                    break;
                case IN_TWO_DAYS:
                    cost = numberOfItems * 2.50;
                    break;

            }
        }
    } else { /* purchaseAmount > 100 */
        if(numberOfItems <= 3) { 
            switch(deliveryDay) {
                case NEXT_DAY:
                    cost = 35;
                    break;
                case IN_TWO_DAYS:
                    cost = 15;
                    break;

            }
        } else {
            switch(deliveryDay) {
                case NEXT_DAY:
                    cost = numberOfItems * 7.50;
                    break;
                case IN_TWO_DAYS:
                    cost = numberOfItems * 3.50;
                    break;
            }
        }
    }
    return cost;
}

4 个答案:

答案 0 :(得分:1)

你的switch / case语句看起来像是可以,应该用表查找替换 - 例如,第二个轴由purchaseAmount > 100(0或1)索引的2D数组和第二个轴由deliveryDay(0,1或2)索引。

答案 1 :(得分:0)

对于这类问题有许多可能的解决方案,例如:

  • 使用enums
  • 如果合适,使用责任链模式
  • 将您的swithes重构为私有方法以获得更易读的代码

Martin Fowler - 重构可能是一个很好的来源(第9章 - 简化条件表达式)

答案 2 :(得分:0)

您可以在另一台交换机内使用该开关。所以这样做的好处是,如果将来你想要再次使用/重用该代码,那么你将永远不会一次又一次地插入新的if-else条件,与使用if-else块相比,执行时间会更快。您可以进一步简化代码。

  Char ch=MyFunciton(); //get return value for condition
  switch(delieveryDay)
   {
     case NEXT_DAY:
        switch(ch)
         {
            case a:
               cost=25; 
               break;
            case b:
               cost = numberOfItems * 6.00;
               break;
            case c:
               cost = 35;
               break;
           case d:
               cost = numberOfItems * 7.50;
               break;
          }
       break;
     case IN_TWO_DAYS:
         switch(ch)
         {
            case a:
               cost = 10; 
               break;
            case b:
               cost = numberOfItems * 2.50;;
               break;
            case c:
               cost = 15;;
               break;
            case d: 
               cost = numberOfItems * 3.50;
                break;
          }
    break;
    case IN_A_WEEK:
       switch(ch)
         {
           case a:
                cost = numberOfItems * 1.50;
                break;
           case b:
                cost = 0;
                break;
           case c:
                cost = 10;
                break;
           case d:
                cost = numberOfItems * 2.50;
                break;
         }
  break;
  }

public char MyFunction() //this function will return character value for condition purpose there in switch
{
   if(purchaseAmount <= 100 && numberOfItems <= 3)
       return a;
   else if(purchaseAmount <= 100 && numberOfItems > 3)
       return b;
   else if(purchaseAmount > 100 && numberOfItems <= 3)
       return c;
   else if(purchaseAmount >100 && numberOfItems > 3)
       return d;
  }

答案 3 :(得分:-1)

我建议您将成本存储在数据库中,而不是硬编码。这将减少您的代码到数据库连接和查询。