将金额拆分为输入传递的部分。

huangapple go评论76阅读模式
英文:

Split amount into inputs passed

问题

以下是翻译好的代码部分:

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();
    }
}

希望这对您有所帮助。如果您有任何问题或需要进一步的帮助,请随时问我。

英文:

I have a requirement where I get totalAmount and how much parts to divide that amount into as inputs. For the outputresponse i am using builder pattern and also avoiding null fields.
Total amount is a big decimal (if null or less than 0, throw exception) and number of inputs can range between 1-3 (if outside of this range or non numeric, throw exception). I have written this code, but I am not convinced and think there may be a cleaner and better way that is also easy to understand just by looking at the code.
Also while splitting, whatever additional cents are left, that would be added to 1st amount.

eg. if 100.01 is total amount that i have to divide into 3 parts, i should get 33.35 for first amount and 33.33 for recurring amounts. Please advise if there is a cleaner and better way to achieve this.

public OutputResponse splitAmount(BigDecimal totalAmount, int divideInto) {
if (!(1 &lt;= divideInto &amp;&amp; 3 &gt;= divideInto)) {
            throw new Exception();
        }
        OutputResponse outputResponse;
        if (totalAmount != null &amp;&amp; totalAmount.compareTo(BigDecimal.ZERO) &gt; 0) {
            BigDecimal recurringAmounts = null;
            BigDecimal firstAmount = totalAmount;
            if (divideInto &gt; 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 &gt; 2) {
                outputResponse.setThirdPmtAmt(recurringAmounts);
            }
        } else {
            throw new Exception();
        }
}

答案1

得分: 1

也许可选项(Optionals)可以帮助使您的代码更加自解释,避免嵌套的 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();
}

如果可选项(Optionals)不适合您的口味:

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();
}
英文:

May be Optionals could help to make your code self-explanatory and avoid the nested if-else blocks:

public OutputResponse splitAmount(BigDecimal totalAmount, int divideInto) throws Exception {
    IntStream.rangeClosed(1, 3)
            .filter(x -&gt; x == divideInto)
            .findAny()
            .orElseThrow(IllegalArgumentException::new);
    
    Optional.ofNullable(totalAmount)
            .filter(d -&gt; d.compareTo(BigDecimal.ZERO) &gt; 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 &gt;= 1) {
       outputResponse = outputResponse.firstAmt(recurringAmounts.add(rest));
    }
    if(divideInto &gt;= 2) {
       outputResponse = outputResponse.secondPmtAmt(recurringAmounts);
    }
    if(divideInto == 3) {
       outputResponse = outputResponse.setThirdPmtAmt(recurringAmounts);
    }
    
    return outputResponse.build();
}

If optionals do not suit your taste:

public OutputResponse splitAmount(BigDecimal totalAmount, int divideInto) throws Exception {
    if(divideInto &lt; 1 || 3 &lt; divideInto) {
       throw new IllegalArgumentException();
    }
    
    if(totalAmount == null || totalAmount.compareTo(BigDecimal.ZERO) &lt;= 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 &gt;= 1) {
       outputResponse = outputResponse.firstAmt(recurringAmounts.add(rest));
    }
    if(divideInto &gt;= 2) {
       outputResponse = outputResponse.secondPmtAmt(recurringAmounts);
    }
    if(divideInto == 3) {
       outputResponse = outputResponse.setThirdPmtAmt(recurringAmounts);
    }
    
    return outputResponse.build();
}

huangapple
  • 本文由 发表于 2020年8月22日 00:24:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63526584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定