有没有办法最小化if和if-else条件

时间:2019-05-02 08:20:38

标签: java if-statement java-8 switch-statement

我写的代码可以正常工作,但是我使用了过多的If和else-if条件。有没有办法将其最小化?根据整数值shippingStatusinvoiceStatuspaymentStatus的值应更改。

    int qtyOrdered = dynamic integer values;
    int qtyShipped = dynamic integer value;
    int qtyReturned = dynamic integer values;
    int qtyInvoiced = dynamic integer values;

        OrderShippingStatus shippingStatus = shippingStatus(qtyOrdered,qtyShipped,qtyReturned);
        OrderInvoicingStatus invoiceStatus = invoiceStatus(qtyOrdered,qtyInvoiced,qtyReturned);
        OrderPaymentStatus paymentStatus =paymentStatus(salesOrder.getAmountPaid(),totalAmountAfterEvent);

private OrderPaymentStatus paymentStatus(BigDecimal amountPaid, BigDecimal totalAmountAfterEvent) {
        if (MathUtils.isFirstLessThanSecond(totalAmountAfterEvent, amountPaid)) {
            return OrderPaymentStatus.FULLY_PAID;
        } else if (MathUtils.areEqual(totalAmountAfterEvent, BigDecimal.ZERO)) {
            return OrderPaymentStatus.NOT_APPLICABLE;
        } else if (MathUtils.isFirstLessThanSecond(amountPaid, totalAmountAfterEvent) && (MathUtils.isFirstLessThanSecond(amountPaid, totalAmountAfterEvent))) {
            return OrderPaymentStatus.PARTIALLY_PAID;
        } else if (MathUtils.isFirstLessThanSecond(amountPaid, totalAmountAfterEvent) && (MathUtils.isFirstLessThanSecond(totalAmountAfterEvent, amountPaid) || (MathUtils.areEqual(totalAmountAfterEvent, amountPaid)))) {
            return OrderPaymentStatus.PARTIALLY_PAID;
        } else {
            return salesOrder.getPaymentStatus();
        }
    }

    private OrderInvoicingStatus invoiceStatus(int qtyOrdered, int qtyInvoiced, int qtyReturned) {
        if (qtyOrdered == qtyInvoiced && qtyInvoiced > qtyReturned) {
            return OrderInvoicingStatus.FULLY_INVOICED;
        } else if (qtyOrdered == qtyInvoiced && qtyInvoiced == qtyReturned) {
            return OrderInvoicingStatus.NOT_APPLICABLE;
        } else if (qtyOrdered > qtyInvoiced && qtyInvoiced > qtyReturned) {
            return OrderInvoicingStatus.PARTIALLY_INVOICED;
        } else if (qtyOrdered > qtyInvoiced && qtyInvoiced == qtyReturned) {
            return OrderInvoicingStatus.NOT_INVOICED;
        } else {
            return salesOrder.getInvoiceStatus();
        }
    }

    private OrderShippingStatus shippingStatus(int qtyOrdered, int qtyShipped, int qtyReturned) {
        if (qtyOrdered == qtyShipped && qtyShipped >= qtyReturned) {
            return OrderShippingStatus.FULLY_SHIPPED;
        } else if (qtyOrdered > qtyShipped && qtyShipped > qtyReturned) {
            return OrderShippingStatus.PARTIALLY_SHIPPED;
        } else if (qtyOrdered > qtyShipped && qtyShipped == qtyReturned) {
            return OrderShippingStatus.NOT_SHIPPED;
        } else {
            return salesOrder.getShippingStatus();
        }
    }

2 个答案:

答案 0 :(得分:2)

如果您很喜欢Java 8和lambda expresions,则可以执行以下操作:

1。使用现有的Functional Interface或定义一个符合您要求的新

@FunctionalInterface
public interface TriPredicate<A, B, C> {

    boolean test(A a, B b, C c);

}

2。创建一个包含条件的枚举。实现一种方法,该方法返回与枚举谓词匹配的元素

import java.util.Arrays;
import java.util.Optional;

public enum ShiStatus {
    FULLY_SHIPPED((qtyOrdered, qtyShipped, qtyReturned) -> qtyOrdered.equals(qtyShipped) && qtyShipped >= qtyReturned),
    PARTIALLY_SHIPPED((qtyOrdered, qtyShipped, qtyReturned) -> qtyOrdered > qtyShipped && qtyShipped > qtyReturned),
    NOT_SHIPPED((qtyOrdered, qtyShipped, qtyReturned) -> qtyShipped != 0.0 && qtyShipped.equals(qtyReturned));

    private TriPredicate<Double, Double, Double> predicate;

    ShiStatus(TriPredicate<Double, Double, Double> predicate) {
        this.predicate = predicate;
    }

    public TriPredicate<Double, Double, Double> getPredicate() {
        return predicate;
    }

    public static Optional<ShiStatus> getStatus(Double qtyOrdered, Double qtyShipped, Double qtyReturned) {
        return Arrays.stream(ShiStatus.values())
                .filter(shiStatus -> shiStatus.getPredicate().test(qtyOrdered, qtyShipped, qtyReturned))
                .findFirst();
    }

}

3。使用枚举方法根据枚举条件获取状态

    @Test
    public void testEnum() {
        ShiStatus shippingStatus = ShiStatus.NOT_SHIPPED; // salesOrder.getShippingStatus()

        Assert.assertEquals(ShiStatus.PARTIALLY_SHIPPED, ShiStatus.getStatus(3D, 2D, 1D).orElse(shippingStatus));
        Assert.assertEquals(ShiStatus.NOT_SHIPPED, ShiStatus.getStatus(1D, 2D, 3D).orElse(shippingStatus));
    }

答案 1 :(得分:1)

正如@Mark Jeronimus在评论中已经提到的那样,您可以使用三种单独的方法来使其更具可读性。由于您正在比较不同的方面,所以switch()函数就没有那么大的意义了。你也可以使用更多的空格... 我的建议:

将此功能设为

if (qtyOrdered == qtyShipped && qtyShipped >= qtyReturned) {
    shippingStatus = ShiStatus.FULLY_SHIPPED;
} 
else if (qtyOrdered > qtyShipped && qtyShipped > qtyReturned) {
    shippingStatus = ShiStatus.PARTIALLY_SHIPPED;
} 
else if (qtyOrdered > qtyShipped && qtyShipped == qtyReturned) {
    shippingStatus = ShiStatus.NOT_SHIPPED;
} 
else {
    shippingStatus = salesOrder.getShippingStatus();
}

使它具有第二个功能:

if (qtyOrdered == qtyInvoiced && qtyInvoiced > qtyReturned) {
    invoiceStatus = ShiStatus.FULLY_INVOICED;
} 
else if (qtyOrdered == qtyInvoiced && qtyInvoiced == qtyReturned) {
    invoiceStatus = InvStatus .NOT_APPLICABLE;
} 
else if (qtyOrdered > qtyInvoiced && qtyShipped > qtyReturned) {
    invoiceStatus = InvStatus .PARTIALLY_INVOICED;
} 
else if (qtyOrdered > qtyInvoiced && qtyShipped == qtyReturned) {
    invoiceStatus = ShiStatus.NOT_INVOICED;
} 
else {
    invoiceStatus = salesOrder.getInvoiceStatus();
}

使这成为您的第三个功能:

if (MathUtils.isFirstLessThanSecond(totalAmountAfterEvent, salesOrder.getAmountPaid())) {
    paymentStatus = PayStatus .FULLY_PAID;
} 
else if (MathUtils.areEqual(totalAmountAfterEvent, BigDecimal.ZERO)) {
    paymentStatus = PayStatus .NOT_APPLICABLE;
} 
else if (MathUtils.isFirstLessThanSecond(salesOrder.getAmountPaid(), totalAmountAfterEvent) && (MathUtils.isFirstLessThanSecond(salesOrder.getAmountPaid(), totalAmountAfterEvent))) {
    paymentStatus = PayStatus .PARTIALLY_PAID;
} 
else if (MathUtils.isFirstLessThanSecond(salesOrder.getAmountPaid(), totalAmountAfterEvent) && (MathUtils.isFirstLessThanSecond(totalAmountAfterEvent, salesOrder.getAmountPaid()) || (MathUtils.areEqual(totalAmountAfterEvent, salesOrder.getAmountPaid())))) {
     paymentStatus = PayStatus .PARTIALLY_PAID;
} 
else {
     paymentStatus = salesOrder.getPaymentStatus();
}

____________________________________________________________

或者,您可以将所有if事物(?)设为int值:

private static int conditions() {
     if(your_first_statement) {
         return 0;
     }
     else if(your_second_statement) {
         return 1;
     } 
     // continue with all your if statements and put this method way down in your programm

所以您的主要功能将是:

  private static void whatever_function_youre_in() {
      int condition = conditions();
      switch(condition) {
      case 0: {
          //your code from the first if statement
      }
      case 1: {
          // your code from the second if statement
      }
      } // continue with all your if conditions
   }    // your else conditions could be the default case from switch (maybe)

编辑

由于尚不清楚我对这三个函数的含义:

在创建者给您的代码中,您需要将它们打包到空隙中,然后像这样执行它们:

function1();
function2();
function3();

在那之后,继续我的开关盒。

但是请记住,您必须首先执行conditons()并向整个类声明所有int值。例如:

 private static int your_int;
 private static int your_second_int;
 // and so on...

EDIT2

对于那里的可怜的家伙(包括创建者),这里是完整的代码:

public class your_class() {
private static int conditions() {
if (qtyOrdered == qtyShipped && qtyShipped >= qtyReturned) {
       return 0;
    } 
    else if (qtyOrdered > qtyShipped && qtyShipped > qtyReturned) {
        return 1;
    } 
    else if (qtyOrdered > qtyShipped && qtyShipped == qtyReturned) {
        return 2;
    } 
    else {
        return 3;
    }
    if (qtyOrdered == qtyInvoiced && qtyInvoiced > qtyReturned) {
        return 4;
    } 
    else if (qtyOrdered == qtyInvoiced && qtyInvoiced == qtyReturned) {
        return 5;
    } 
    else if (qtyOrdered > qtyInvoiced && qtyShipped > qtyReturned) {
        return 6;
    } 
    else if (qtyOrdered > qtyInvoiced && qtyShipped == qtyReturned) {
        return 7;
    } 
    else {
        return 8;
    }
    if (MathUtils.isFirstLessThanSecond(totalAmountAfterEvent, salesOrder.getAmountPaid())) {
        return 9;
    } 
    else if (MathUtils.areEqual(totalAmountAfterEvent, BigDecimal.ZERO)) {
        return 10;
    } 
    else if (MathUtils.isFirstLessThanSecond(salesOrder.getAmountPaid(), totalAmountAfterEvent) && (MathUtils.isFirstLessThanSecond(salesOrder.getAmountPaid(), totalAmountAfterEvent))) {
        return 11;
    } 
    else if (MathUtils.isFirstLessThanSecond(salesOrder.getAmountPaid(), totalAmountAfterEvent) && (MathUtils.isFirstLessThanSecond(totalAmountAfterEvent, salesOrder.getAmountPaid()) || (MathUtils.areEqual(totalAmountAfterEvent, salesOrder.getAmountPaid())))) {
        return 12;
    } 
    else {
        return 13;
    }
}

private static void main(Strings[] args) {
int conditions = conditions();
switch(condition) {
case 0: {
    shippingStatus = ShiStatus.FULLY_SHIPPED;
}
case 1: {
    shippingStatus = ShiStatus.PARTIALLY_SHIPPED;
}
// all your other cases
}
}

我认为您现在可以更好地理解它并完成其余的切换。注意:您必须使您的所有动态int的私有静态int都可用。