Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public void processTransactions() { boolean error = false; gains = 0; availableQ

ID: 3706735 • Letter: P

Question

public void processTransactions()

{

boolean error = false;

gains = 0;

availableQty = 0;

while(!allTransactions.isEmpty() && !error)

{

Transaction t = allTransactions.remove();

System.out.println("Processing transaction: " + t);

if(!t.isSell())

{

buyTransactions.add(t);

availableQty += t.getNumber();

}

else

{

int sellQty = t.getNumber();

while(sellQty > 0)

{

if(buyTransactions.isEmpty())

{

error = true;

break;

}

Transaction bt = buyTransactions.peek();

if(bt.getNumber() <= sellQty)

{

gains += (t.getPrice() - bt.getPrice()) * bt.getNumber();

sellQty -= bt.getNumber();

buyTransactions.remove();

}

else

{

gains += (t.getPrice() - bt.getPrice()) * sellQty;

bt.changeNumber(bt.getNumber() - sellQty);

sellQty = 0;

}

}

if(sellQty != 0)

{

System.out.println("Error: attempt to sell non-existing shares!");

}

else

availableQty -= t.getNumber();

}

}

if(!error)

{

System.out.println(" Capital Gain/Loss : $" + gains);

System.out.println("There are " + availableQty + " shares left");

}

}

can you explain in detail using line comment what this method do and its method specification?

Explanation / Answer

Please find the commented code below.

CODE

==================

public void processTransactions() {

       boolean error = false;

       gains = 0;

       availableQty = 0;

      

       // Loop until all transactions are processed or an error is encountered

       while(!allTransactions.isEmpty() && !error)

       {  

           // remove the first transaction from the list for processing

           Transaction t = allTransactions.remove();

           System.out.println("Processing transaction: " + t);

           // if the transaction is of type "Buy"

           if(!t.isSell())

           {

               // add this transaction 't' to the list of buyTransactions

               buyTransactions.add(t);

               // increment the number of available quantities by t's buy quantity

               availableQty += t.getNumber();

           }

           // if the transaction is of type "Sell"

           else

           {

               // find the number of shares to be sold (we are now selling the last transaction 't' shares)

               int sellQty = t.getNumber();

               // loop until sell quantity is greater than 0

               while(sellQty > 0)

               {

                   // if the buy transactions list is empty, initialize error to true so that the outer

                   // while loop is exited

                   if(buyTransactions.isEmpty())

                   {

                       error = true;

                       break;

                   }

                   // get the last transaction details

                   Transaction bt = buyTransactions.peek();

                   // bt's shares is <= sell quantity, we can sell only bt's shares

                   if(bt.getNumber() <= sellQty)

                   {

                       // increment gains by difference of price between last two transactions multiplied by bt's number of shares

                       gains += (t.getPrice() - bt.getPrice()) * bt.getNumber();

                       // now, the selling number of shares remaining is decremented by the bt.getNumber() as they are already sold in the last step

                       sellQty -= bt.getNumber();

                       // remove the transaction 'bt' from the stack

                       buyTransactions.remove();

                   }

                   // else sell all the bt's shares

                   else

                   {

                       // increment gains by difference of price between last two transactions multiplied by total sell quantity

                       gains += (t.getPrice() - bt.getPrice()) * sellQty;

                       // Now, bt's number of shares need to reduced by sellQuantity as they are sold in the last step

                       bt.changeNumber(bt.getNumber() - sellQty);

                       // initialize sellQuantity to 0

                       sellQty = 0;

                   }

               }

               // if all the shares are not sold, print an error message

               if(sellQty != 0)

               {

                   System.out.println("Error: attempt to sell non-existing shares!");

               }

              

               // now available shares is decremented by t's shares as they are completely sold

               else

                   availableQty -= t.getNumber();

           }

       }

       //if no error was encountered, print the overall gain/loss and number of shares left

       if(!error)

       {

           System.out.println(" Capital Gain/Loss : $" + gains);

           System.out.println("There are " + availableQty + " shares left");

       }

   }