英文:
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 <= 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();
}
}
答案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 -> 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();
}
If optionals do not suit your taste:
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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论