Splitting payments in a number of installments I was reviewing today some code which gets as input a list of payments (paymentList) and a list of installments (installmentList) has to populate two other lists, respectively depositList and balanceList.

Basically it calculates the sum of all payments from paymentList and fills depositList with the amount enough to let each specific installment be payed.

If the sum of all payments is less then the total of installments it fills as many installments as it can and then sets the balanceList with the amount which is missing.

To make this clearer this is the output:

paymentList    : 1200, 1000, 400, 
installmentList: 1200, 1200, 1200, 900, 
depositList    : 1200, 1200, 200, 0, 
balanceList    : 0, 0, -1000, -1900, 


if I made a total of 3 payments for 2600$, and I had to pay 4 installments, I would be able to pay the first two completely (with balance 0) and the third only with 200$ still having to pay 1000$, and the last installment would be completely missed for a total debt of 1900$.

Seems easy enough however the code which does it stinks to me, here is a simplified version:

public class Payment
{

    public static void main(String[] args)
    {
        ArrayList&lt;Integer&gt; paymentList = new ArrayList&lt;Integer&gt;()
        {
            {
                add(1200);
                add(1000);
                add(400);
            }
        };

        ArrayList&lt;Integer&gt; installmentList = new ArrayList&lt;Integer&gt;()
        {
            {
                add(1200);
                add(1200);
                add(1200);
                add(900);
            }
        };

        printArray("paymentList    ", paymentList);
        printArray("installmentList", installmentList);

        ArrayList&lt;Integer&gt; depositList = new ArrayList();
        ArrayList&lt;Integer&gt; balanceList = new ArrayList();

        Integer settledTotalAmount = 0;
        for (Integer i : paymentList)
        {
            settledTotalAmount += i;
        }

        for (Integer currentInstallmentAmount : installmentList)
        {
            Integer settledAmount = 0;
            if (settledTotalAmount &gt; 0)
            {
                int tmpAmount = settledTotalAmount - currentInstallmentAmount;
                if (tmpAmount &gt; 0)
                {
                    settledAmount = currentInstallmentAmount;
                    settledTotalAmount = tmpAmount;
                    balanceList.add(0);
                }
                else
                {
                    settledAmount = currentInstallmentAmount+(tmpAmount);
                    settledTotalAmount = (currentInstallmentAmount-settledAmount)*-1;
                    balanceList.add((currentInstallmentAmount-settledAmount)*-1);
                }
            }
            else
            {
                settledAmount = 0;
                int k = (settledTotalAmount * -1)+currentInstallmentAmount;
                settledTotalAmount = k*-1;
                balanceList.add(settledTotalAmount);
            }
            depositList.add(settledAmount);
        }

        printArray("depositList    ", depositList);
        printArray("balanceList    ", balanceList);

    }

    public static void printArray(String name, ArrayList&lt;Integer&gt; array)
    {
        System.out.print(name + ": ");
        for (Integer i : array)
        {
            System.out.print(i + ", ");
        }
        System.out.println();
    }
}


in particular the series of if else switches seems redundant to me, is there something cleaner for the above (which seems to work for now)?
The version above actually is a simplified version, the original one I have uses BigDecimal and is even messier than that.
I tried to simplify the loop:

    int availableAsset = paymentList.stream().mapToInt(Integer::intValue).sum();

    for (Integer currentInstallmentAmount : installmentList) {
        if (availableAsset &gt; currentInstallmentAmount) {
            balanceList.add(0);
            depositList.add(currentInstallmentAmount);
            availableAsset -= currentInstallmentAmount;
        } else {
            if (availableAsset &gt; 0) {
                depositList.add(currentInstallmentAmount - availableAsset);
                balanceList.add(availableAsset - currentInstallmentAmount);
                availableAsset = 0;
            } else {
                balanceList.add(-currentInstallmentAmount);
                depositList.add(0);
            }
        }
    }


Since we have just this or that decisions (instead of a selection) it's not really a benefit to use polymorphism here.