Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void calculateBalance() {
- new LoadBalanceView().doInBackground();
- HashMap<String, String> tmp = new HashMap<String, String>();
- HashMap<String, String> tmp2 = new HashMap<String, String>();
- HashMap<String, String> result = new HashMap<String, String>();
- sortingFlowList = balanceViewList;
- while (!sortingFlowList.isEmpty() ) {
- if(sortingFlowList.size()==0) break;
- Collections.sort(sortingFlowList, new MapComparator(TAG_TOTALAMOUNT));
- result = new HashMap<String, String>();
- tmp = sortingFlowList.get(0);
- sortingFlowList.remove(tmp);
- //if equals !!
- for (int i = 0; i < sortingFlowList.size(); i++) {
- Double b = Math.abs(Double.valueOf(tmp.get(TAG_TOTALAMOUNT))) - Math.abs(Double.valueOf(sortingFlowList.get(i).get(TAG_TOTALAMOUNT)));
- BigDecimal bd = new BigDecimal(b).setScale(2, RoundingMode.HALF_UP);
- double newInput = bd.doubleValue();
- if(Math.abs(newInput)<0.01){
- result = new HashMap<>();
- result.put(TAG_SENDER, tmp.get(TAG_LOGIN));
- //rounding result
- BigDecimal tmpBD = new BigDecimal(Double.valueOf(tmp.get(TAG_TOTALAMOUNT))).setScale(2, RoundingMode.HALF_UP);
- double newBD = tmpBD.doubleValue();
- result.put(TAG_AMOUNT, String.valueOf(Math.abs(newBD)));
- result.put(TAG_RECEIVER, sortingFlowList.get(i).get(TAG_LOGIN));
- balanceFlowList.add(result);
- sortingFlowList.remove(sortingFlowList.get(i));
- }
- }
- if (result.isEmpty()) {
- tmp2 = sortingFlowList.get(sortingFlowList.size()-1);
- //tmp2 > tmp
- if (Math.abs(Double.valueOf(tmp2.get(TAG_TOTALAMOUNT))) > Math.abs(Double.valueOf(tmp.get(TAG_TOTALAMOUNT)))) {
- result = new HashMap<>();
- result.put(TAG_SENDER, tmp.get(TAG_LOGIN));
- //rounding result
- BigDecimal tmpBD = new BigDecimal(Double.valueOf(tmp.get(TAG_TOTALAMOUNT))).setScale(2, RoundingMode.HALF_UP);
- double newBD = tmpBD.doubleValue();
- result.put(TAG_AMOUNT, String.valueOf(Math.abs(newBD)));
- result.put(TAG_RECEIVER, tmp2.get(TAG_LOGIN));
- Double tmpbalance = Double.valueOf(tmp2.get(TAG_TOTALAMOUNT)) - Math.abs(Double.valueOf(tmp.get(TAG_TOTALAMOUNT)));
- BigDecimal bd = new BigDecimal(tmpbalance).setScale(2, RoundingMode.HALF_UP);
- double balance = bd.doubleValue();
- if(Math.abs(balance) <0.01) balance =0.0;
- sortingFlowList.get(sortingFlowList.indexOf(tmp2)).put(TAG_TOTALAMOUNT, String.valueOf(balance));
- balanceFlowList.add(result);
- } else {
- //tmp2 < tmp
- result = new HashMap<>();
- result.put(TAG_SENDER, tmp.get(TAG_LOGIN));
- result.put(TAG_AMOUNT, tmp2.get(TAG_TOTALAMOUNT));
- result.put(TAG_RECEIVER, tmp2.get(TAG_LOGIN));
- //rounding result
- Double tmpbalance = Double.valueOf((tmp.get(TAG_TOTALAMOUNT))) + Double.valueOf(tmp2.get(TAG_TOTALAMOUNT));
- BigDecimal bd = new BigDecimal(tmpbalance).setScale(2, RoundingMode.HALF_UP);
- double balance = bd.doubleValue();
- if(Math.abs(balance) <0.05) balance =0.0;
- tmp.put(TAG_TOTALAMOUNT, String.valueOf(balance));
- sortingFlowList.remove(tmp2);
- sortingFlowList.add(tmp);
- balanceFlowList.add(result);
- }
- }
- }
- createView();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement