Extract method to avoid duplicate code.

This commit is contained in:
Sebastian Sieber
2020-12-07 16:31:58 +01:00
parent 9e496f0f55
commit dcf7f82b01

View File

@@ -52,16 +52,20 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
* @return the total amount * @return the total amount
*/ */
protected BigDecimal getChargesForPercent(BigDecimal percent) { protected BigDecimal getChargesForPercent(BigDecimal percent) {
BigDecimal res = BigDecimal.ZERO;
IZUGFeRDAllowanceCharge[] charges = trans.getZFCharges(); IZUGFeRDAllowanceCharge[] charges = trans.getZFCharges();
if ((charges != null) && (charges.length > 0)) { return sumAllowanceCharge(percent, charges);
for (IZUGFeRDAllowanceCharge currentCharge : charges) { }
if ((percent==null)||(currentCharge.getTaxPercent().compareTo(percent)==0)) {
res = res.add(currentCharge.getTotalAmount(this)); private BigDecimal sumAllowanceCharge(BigDecimal percent, IZUGFeRDAllowanceCharge[] charges) {
} BigDecimal res = BigDecimal.ZERO;
} if ((charges != null) && (charges.length > 0)) {
} for (IZUGFeRDAllowanceCharge currentCharge : charges) {
return res; if ((percent==null)||(currentCharge.getTaxPercent().compareTo(percent)==0)) {
res = res.add(currentCharge.getTotalAmount(this));
}
}
}
return res;
} }
/*** /***
@@ -70,43 +74,38 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
* @return the space separated String * @return the space separated String
*/ */
protected String getChargeReasonForPercent(BigDecimal percent) { protected String getChargeReasonForPercent(BigDecimal percent) {
String res = " ";
IZUGFeRDAllowanceCharge[] charges = trans.getZFCharges(); IZUGFeRDAllowanceCharge[] charges = trans.getZFCharges();
if ((charges != null) && (charges.length > 0)) { String res = getAllowanceChargeReasonForPercent(percent, charges);
for (IZUGFeRDAllowanceCharge currentCharge : charges) { if ("".equals(res)) {
if ((percent==null)||(currentCharge.getTaxPercent().compareTo(percent)==0)) {
if (currentCharge.getReason()!=null) {
res = res+currentCharge.getReason()+" ";
}
}
}
}
res=res.substring(0,res.length()-1);
if (res.equals("")) {
res="Charges"; res="Charges";
} }
return res; return res;
} }
private String getAllowanceChargeReasonForPercent(BigDecimal percent, IZUGFeRDAllowanceCharge[] charges) {
String res = " ";
if ((charges != null) && (charges.length > 0)) {
for (IZUGFeRDAllowanceCharge currentCharge : charges) {
if ((percent==null)||(currentCharge.getTaxPercent().compareTo(percent)==0)) {
if (currentCharge.getReason()!=null) {
res = res+currentCharge.getReason()+" ";
}
}
}
}
res=res.substring(0,res.length()-1);
return res;
}
/*** /***
* returns a (potentially concatenated) string of allowance reasons, or "Allowances", if none are defined * returns a (potentially concatenated) string of allowance reasons, or "Allowances", if none are defined
* @param percent a specific rate, or null for any rate * @param percent a specific rate, or null for any rate
* @return the space separated String * @return the space separated String
*/ */
protected String getAllowanceReasonForPercent(BigDecimal percent) { protected String getAllowanceReasonForPercent(BigDecimal percent) {
String res = " ";
IZUGFeRDAllowanceCharge[] allowances = trans.getZFAllowances(); IZUGFeRDAllowanceCharge[] allowances = trans.getZFAllowances();
if ((allowances != null) && (allowances.length > 0)) { String res = getAllowanceChargeReasonForPercent(percent, allowances);
for (IZUGFeRDAllowanceCharge currentAllowance : allowances) { if ("".equals(res)) {
if ((percent==null)||(currentAllowance.getTaxPercent().compareTo(percent)==0)) {
if (currentAllowance.getReason()!=null) {
res = res+currentAllowance.getReason()+" ";
}
}
}
}
res=res.substring(0,res.length()-1);
if (res.equals("")) {
res="Allowances"; res="Allowances";
} }
return res; return res;
@@ -119,16 +118,8 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
* @return the total amount * @return the total amount
*/ */
protected BigDecimal getAllowancesForPercent(BigDecimal percent) { protected BigDecimal getAllowancesForPercent(BigDecimal percent) {
BigDecimal res = BigDecimal.ZERO;
IZUGFeRDAllowanceCharge[] allowances = trans.getZFAllowances(); IZUGFeRDAllowanceCharge[] allowances = trans.getZFAllowances();
if ((allowances != null) && (allowances.length > 0)) { return sumAllowanceCharge(percent, allowances);
for (IZUGFeRDAllowanceCharge currentAllowance : allowances) {
if ((percent==null)||(currentAllowance.getTaxPercent().compareTo(percent)==0)) {
res = res.add(currentAllowance.getTotalAmount(this));
}
}
}
return res;
} }
/*** /***