I have this question on buying and selling of equipment.

I am implementing the purchasing part with 2 array list. One to store the amount of equipment bought, the other to store the buying price.
However i am having some problem understanding the logic behind the selling part whereby i have to calculate the capital gain. Any sample coding or explanation would be greatly appreciated.

Example:
I have purchased the following equipment
300 equipment - $10
200 equipment - $20
100 equipment - $30

But i might be selling them at
150 equipment - $50
Ptm3j.png

This is my buying implementation.

public void buy(int x, int y)
Stocks.add(x);
StockPrice.add(y);
//purchase = x*y;
numStocks = numStocks + x;