将数量拆分为传递的输入

hec6srdp  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(324)

我有一个要求,在那里我得到总金额和多少部分,以分为投入金额。对于outputresponse,我使用构建器模式并避免空字段。总量是一个大的小数(如果为null或小于0,则抛出异常),输入的数量可以在1到3之间(如果超出此范围或非数字,则抛出异常)。我已经写了这段代码,但我不相信,并认为可能有一个更干净,更好的方式,也很容易理解,只要看看代码。另外,在拆分时,无论剩下多少分钱,都会加到第一笔金额中。
如果100.01是总金额,我必须分为3部分,我应该得到33.35为第一个金额和33.33为经常性金额。请告知是否有更干净更好的方法来实现这一点。

public OutputResponse splitAmount(BigDecimal totalAmount, int divideInto) {
if (!(1 <= divideInto && 3 >= divideInto)) {
            throw new Exception();
        }
        OutputResponse outputResponse;
        if (totalAmount != null && totalAmount.compareTo(BigDecimal.ZERO) > 0) {
            BigDecimal recurringAmounts = null;
            BigDecimal firstAmount = totalAmount;
            if (divideInto > 1) {
                recurringAmounts = totalAmount.divide(BigDecimal.valueOf(divideInto), 2, RoundingMode.FLOOR);
                firstAmount = totalAmount.subtract(recurringAmounts.multiply(new BigDecimal(divideInto - 1)));
            }

            outputResponse = OutputResponse.builder()
                    .firstAmt(firstAmount)
                    .secondPmtAmt(recurringAmounts)
                    .build();

            if (divideInto > 2) {
                outputResponse.setThirdPmtAmt(recurringAmounts);
            }
        } else {
            throw new Exception();
        }
}
v6ylcynt

v6ylcynt1#

可能是可选的,这有助于使代码具有自解释性,并避免嵌套的if-else块:

public OutputResponse splitAmount(BigDecimal totalAmount, int divideInto) throws Exception {
    IntStream.rangeClosed(1, 3)
            .filter(x -> x == divideInto)
            .findAny()
            .orElseThrow(IllegalArgumentException::new);

    Optional.ofNullable(totalAmount)
            .filter(d -> d.compareTo(BigDecimal.ZERO) > 0)
            .orElseThrow(IllegalArgumentException::new);

    BigDecimal recurringAmounts = totalAmount.divide(BigDecimal.valueOf(divideInto), 2, RoundingMode.FLOOR);
    BigDecimal rest = totalAmount.subtract(recurringAmounts.multiply(BigDecimal.valueOf(divideInto)));

    OutputResponse outputResponse = OutputResponse.builder();
    if(divideInto >= 1) {
       outputResponse = outputResponse.firstAmt(recurringAmounts.add(rest));
    }
    if(divideInto >= 2) {
       outputResponse = outputResponse.secondPmtAmt(recurringAmounts);
    }
    if(divideInto == 3) {
       outputResponse = outputResponse.setThirdPmtAmt(recurringAmounts);
    }

    return outputResponse.build();
}

如果选择不适合您的口味:

public OutputResponse splitAmount(BigDecimal totalAmount, int divideInto) throws Exception {
    if(divideInto < 1 || 3 < divideInto) {
       throw new IllegalArgumentException();
    }

    if(totalAmount == null || totalAmount.compareTo(BigDecimal.ZERO) <= 0) {
       throw new IllegalArgumentException();
    }

    BigDecimal recurringAmounts = totalAmount.divide(BigDecimal.valueOf(divideInto), 2, RoundingMode.FLOOR);
    BigDecimal rest = totalAmount.subtract(recurringAmounts.multiply(BigDecimal.valueOf(divideInto)));

    OutputResponse outputResponse = OutputResponse.builder();
    if(divideInto >= 1) {
       outputResponse = outputResponse.firstAmt(recurringAmounts.add(rest));
    }
    if(divideInto >= 2) {
       outputResponse = outputResponse.secondPmtAmt(recurringAmounts);
    }
    if(divideInto == 3) {
       outputResponse = outputResponse.setThirdPmtAmt(recurringAmounts);
    }

    return outputResponse.build();
}

相关问题