Question: Using queues, write two implementations to compute the Capital Gain in a sale of stock. Suppose that you buy n shares of a stock for
Using queues, write two implementations to compute the Capital Gain in a sale of stock. Suppose that you buy n shares of a stock for d dollars each. Later when you sell some of these shares, if the sales price exceeds the purchase price, you have made a profit which is called a capital gain. If the sale price is lower than the purchase price, you have a loss which is called a negative capital gain.
Investors buy shares in a particular company over a period of time. For example, last year you bought 20 shares of Amazon at $45 per share. Last month, you bought 20 additional shares at $75 per share and today, you sold 30 shares at $65 per share. What is your capital gain? Well, which of your 40 shares did you actually sell? Unfortunately, you cannot pick and choose. When computing capital gains, you must assume that you sell shares in the order in which you purchased them. Stock sales are a first-in-first-out application. In our example, you sold 20 shares that you bought at $45 each and 10 of the shares that you bought at $75 each. Your cost for the $30 shares is $1650. You sold them for $1950, a capital gain of $300. You will design a way to record your investment transactions chronologically and to compute the capital gain of any stock.
Requirements for iteration 1: (25 points)
1. For the first iteration use a standard queue implementation from chapter 4. Assume that all transactions are for stocks of a single company and there is no commission charge for the transactions. Write a class called StockPurchase that records the cost of a single share of stock. (see below)
2. Write a class called StockLedger that enables us to record stock purchases in chronological order. (see below)
3. At the time of sale, the StockLedger class computes the capital gain and updates the record of stocks owned. It contains a queue that stores each share individually. (20 shares equal 20 enqueues into the queue.)
4. Write an application program that allows the user to buy and sell stocks for a company. It has to use the StockLedger and StockPurchase classes. For every sale compute the capital gain and display the result. If the capital gain is positive it is a profit, if the capital gain is negative it is a loss.
5. The following statements demonstrate how we could use StockLedger in our application program to record the transactions given in the problem set:
StockLedger myStocks = new StockLedger();
myStocks.buy(20, 45);
myStocks.buy(20, 75);
double capGain = myStocks.sell(30, 65);
6. StockLedger records instances of StockPurchase which represents the shares we own in a queue. The queue orders the shares so we can sell them in the order in which we purchased them. The method buyenqueues each share that is bought. The method sell removes from the queue as many shares as are sold. As it does this, it computes the total capital gain from the sale and returns it.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
