Home > OS >  Calculation logic going wrong in Java
Calculation logic going wrong in Java

Time:09-11

I am trying to calculate locked-up shares based on the input of unlocked shares in my code. Say for the period of 1st July to 31st July, the unlock shares are 2000 and for the period of 1st Aug to Aug 29, the unlock shares are 3000, and for the period of 30th Aug to 30th September, the unlock shares are 4000, then the locked-up shares for the period of 1st July to 31st July should be 9000 i.e. addition of all the unlock shares. For the period of 1st Aug to 29th Aug, the locked up shares should be 9000-2000=7000 and for the period of 30th Aug to 30th Sep, the locked up shares should be 7000-3000=4000. My code gives me the first two values right but it is giving me the third value incorrect. I am getting 9000 and 7000 correctly calculated but further, the code is giving me value as 6000. Following is my code.

public static List<Long> getLockedUpShares(HashMap<String, String> testcase, String token,
            List<IPOGridData> ipoGridList) {
        Long totalUnlockShares = Long.valueOf(0);
        for (int i = 0; i < ipoGridList.size(); i  ) {
            totalUnlockShares = totalUnlockShares   ipoGridList.get(i).getUnlockShares();
        }
        List<Long> lockedUpSharesList = new ArrayList<>();
        lockedUpSharesList.add(totalUnlockShares);
        Long lockedUpShares = Long.valueOf(0);
        for (int i = 0; i < ipoGridList.size(); i  ) {
            lockedUpShares = totalUnlockShares - ipoGridList.get(i).getUnlockShares();
            lockedUpSharesList.add(lockedUpShares);
        }
        lockedUpSharesList.remove(lockedUpSharesList.size()-1);
        return lockedUpSharesList;
    }

I am adding the total unlock shares first and then I am subtracting the unlock share values which I get from ipoGridList one by one. But this is not giving me an ideal calculation. Can someone help me building the right logic?

CodePudding user response:

It can be done by just get the last element from the lockedUpStateList lockedUpShares = lockedUpShareList.get(i)-poGridList.get(i).getUnlockShares(); lockedUpSharesList.add(lockedUpShares);

  •  Tags:  
  • java
  • Related