0

I want to avoid lots of if-else statement.

I want to use another solution, because if there is situation, I have to use lots of condition.so this is not efficient. you can see in my code I use lots of if else statement, but that's not I want, some of them talk about map

    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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
programmer
  • 127
  • 1
  • 2
  • 14

4 Answers4

1

Your switch/case statements look like they could, and should, be replaced with table lookup -- for example, a 2D array where the first axis is indexed by purchaseAmount > 100 (0 or 1) and the second axis is indexed by deliveryDay (0, 1, or 2).

keshlam
  • 7,931
  • 2
  • 19
  • 33
0

there are many possible solutions for this kind of problem, eg:

  • use enums
  • use chain of responsibilty pattern if it is suitable
  • refactor your swithes to private methods for more readable code

Martin Fowler - Refactoring can be a good source (chapter 9 - Simplifying conditional expressions)

Community
  • 1
  • 1
Bela Vizer
  • 2,527
  • 22
  • 26
  • Actualy I used enums operation for (deliverydays) ,but ı want to use some structure other than if-else or swtich case condition for decision table – programmer Mar 01 '14 at 16:41
0

You can use the switch inside another switch. so the advantage of this is that if in future you want to expend/reuse that code again, then you will never insert new if-else condition again and again, along this the execution time will be faster as compare to use if-else block. You can further make the code simpler.

  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;
  }
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
-1

I suggest you to store costs somewhere in database instead of not hard-coded. this will reduce your code to db connection and query.

Bitman
  • 1,996
  • 1
  • 15
  • 18