背景說明 最近在做財務相關的系統,對賬單核銷預付款從技術角度來看就是將兩個數組進行合併 對賬單核銷預付款前提條件: 對賬單總金額必須等於未核銷金額 數據示例 對賬單數據 | 單號 | 金額 | | | | | B0001 | 100 | | B0002 | 80 | | B0003 | 120 | ...
背景說明
最近在做財務相關的系統,對賬單核銷預付款
從技術角度來看就是將兩個數組進行合併
對賬單核銷預付款前提條件:
- 對賬單總金額必須等於未核銷金額
數據示例
對賬單數據
單號 | 金額 |
---|---|
B0001 | 100 |
B0002 | 80 |
B0003 | 120 |
預付款數據
單號 | 未核銷金額 |
---|---|
PRE001 | 110 |
PRE002 | 190 |
結果數據
預付款單號 | 核銷金額 | 對賬單號 |
---|---|---|
PRE001 | 100 | B001 |
PRE001 | 10 | B002 |
PRE002 | 70 | B002 |
PRE002 | 120 | B003 |
通過分析可以使用JAVA雙指針演算法可以實現這個需求
雙指針主要用於遍曆數組,兩個指針指向不同的元素,從而協同完成任務。
代碼實現
public static void main(String[] args) {
class Info {
private String formId;
private BigDecimal amount;
public String getFormId() {
return formId;
}
public void setFormId(String formId) {
this.formId = formId;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
}
List<Info> billBatchlist = new ArrayList<>();
Info info = new Info();
info.setFormId("B0001");
info.setAmount(new BigDecimal(100));
billBatchlist.add(info);
info = new Info();
info.setFormId("B0002");
info.setAmount(new BigDecimal(80));
billBatchlist.add(info);
info = new Info();
info.setFormId("B0003");
info.setAmount(new BigDecimal(120));
billBatchlist.add(info);
List<Info> payVerifyControlList = new ArrayList<>();
Info info1 = new Info();
info1.setFormId("PRE0001");
info1.setAmount(new BigDecimal(110));
payVerifyControlList.add(info1);
info1 = new Info();
info1.setFormId("PRE0002");
info1.setAmount(new BigDecimal(190));
payVerifyControlList.add(info1);
billBatchlist.forEach(a -> {
System.out.println("BillFormId:" + a.formId + ",Amount:" + a.getAmount());
});
payVerifyControlList.forEach(a -> {
System.out.println("PreFormId:" + a.formId + ",Amount:" + a.getAmount());
});
System.out.println("==================================");
int i = 0, j = 0;
BigDecimal preAmount = new BigDecimal(0);
BigDecimal billAmount = new BigDecimal(0);
while (i < billBatchlist.size() && j < payVerifyControlList.size()) {
if (preAmount.compareTo(BigDecimal.ZERO) == 0) {
preAmount = billBatchlist.get(i).amount;
}
if (billAmount.compareTo(BigDecimal.ZERO) == 0) {
billAmount = payVerifyControlList.get(j).amount;
}
//當前核銷金額
BigDecimal verifyAmount = new BigDecimal(0);
if (preAmount.compareTo(billAmount) > -1) {
verifyAmount = billAmount;
// pre剩餘
preAmount = preAmount.subtract(verifyAmount);
billAmount = new BigDecimal(0);
} else {
verifyAmount = preAmount;
// bill剩餘
billAmount = billAmount.subtract(verifyAmount);
preAmount = new BigDecimal(0);
}
System.out.println("當前核銷金額:" + verifyAmount + ",preAmount剩餘:" + preAmount + ",billAmount剩餘:" + billAmount);
//大於等於0
if (preAmount.compareTo(BigDecimal.ZERO) == 0) {
i++;
}
if (billAmount.compareTo(BigDecimal.ZERO) == 0) {
j++;
}
}
}
列印結果:
BillFormId:B0001,Amount:100
BillFormId:B0002,Amount:80
BillFormId:B0003,Amount:120
PreFormId:PRE0001,Amount:110
PreFormId:PRE0002,Amount:190
==================================
當前核銷金額:100,preAmount剩餘:0,billAmount剩餘:10
當前核銷金額:10,preAmount剩餘:70,billAmount剩餘:0
當前核銷金額:70,preAmount剩餘:0,billAmount剩餘:120
當前核銷金額:120,preAmount剩餘:0,billAmount剩餘:0
作者:taoz
出處:www.cnblogs.com/bigbrid
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
本文如對您有幫助,還請多幫 【推薦】 下此文。
如果喜歡我的文章,請關註我的公眾號