When a share of common stock of some company is sold, the capital gain (or, sometimes, loss) is the difference between the share’s selling price and the price originally paid to buy it. This rule is easy to understand for a single share, but if we sell multiple shares of stock bought over a long period of time, then we must identify the shares actually being sold. A standard accounting principle for identifying which shares of a stock were sold in such a case is to use a FIFO protocol—the shares sold are the ones that have been held the longest (indeed, this is the default method built into several personal finance software packages) For example, suppose we buy 100 shares at $20 each on day 1, 20 shares at $24 on day 2, 200 shares at $36 on day 3, and then sell 150 shares on day 4 at $30 each. Then applying the FIFO protocol means that of the 150 shares sold, 100 were bought on day 1, 20 were bought on day 2, and 30 were bought on day 3. The capital gain in this case would therefore be 100 · 10+20 · 6+30 · (−6), or $940. Write a program that takes as input a sequence of transactions of the form “buy x share(s) at $y each” or “sell x share(s) at $y each,” assuming that the transactions occur on consecutive days and the values x and y are integers. Given this input sequence, the output should be the total capital gain (or loss) for the entire sequence, using the FIFO protocol to identify shares.
PROGRAM CODE:
//Header section
import java.util.ArrayList;
import java.util.List;
//Create a class name, Main
class Main
{
//Declare variables
private int shares;
private double price;
//Constructor
public Main(int shares, double price)
{
this.shares = shares;
this.price = price;
}
//Method defintion of getShares
public int getShares()
{
return shares;
}
//Method defintion of getPrice
public double getPrice()
{
return price;
}
}
//Create a another class name , FIFOProtocol
public class FIFOProtocol
{
private List<Main> transactions;
//Constructor
public FIFOProtocol()
{
transactions = new ArrayList<>();
}
//Method defintion of buyShares
public void buyShares(int shares, double price)
{
Main transaction = new Main(shares, price);
transactions.add(transaction);
}
//Method defintion of sellShares
public double sellShares(int shares, double price)
{
double capitalGain = 0;
while (shares > 0 && !transactions.isEmpty())
{
Main transaction = transactions.get(0);
int availableShares = transaction.getShares();
if (shares <= availableShares)
{
capitalGain += shares * (price - transaction.getPrice());
transaction.shares -= shares;
shares = 0;
if (transaction.getShares() == 0)
{
transactions.remove(0);
}
} else
{
capitalGain += availableShares * (price - transaction.getPrice());
shares -= availableShares;
transactions.remove(0);
}
}
return capitalGain;
}
//main method
public static void main(String[] args)
{
//Create a class FIFOProtocol object, fifo
FIFOProtocol fifo = new FIFOProtocol();
//Call the methods
fifo.buyShares(100, 20);
fifo.buyShares(20, 24);
fifo.buyShares(200, 36);
double capitalGain = fifo.sellShares(150, 30);
System.out.println("Total capital gain: $" + capitalGain);
}
}
OUTPUT OF THE PROGRAM CODE:
Total capital gain (or loss): $940