Question: Write a program in Java that reads a file of stock transactions. The file will contain lines of transactions in the form: Buy x shares
Write a program in Java that reads a file of stock transactions. The file will contain lines of transactions in the form: Buy x shares at $y each or Sell x shares at $y each As you read the file, process it line by line:
A buy should be placed on queue. The queue should be a queue of type Purchases. You will create a Purchases class which stores information about a purchase: a quantity and a cost.
A sell should remove items from the queue as needed. For each sale, display the total money gained (or lost) on the sale.
When a share of common stock of some company is sold, the capital gain (or sometimes loss) is the difference between the stockss 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, 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.
Example:
Suppose we:
Buy 100 shares at $20 each on day 1
Buy 20 shares at $24 each on day 2
Buy 200 shares at $36 on day 3
Sell 150 shares on day 4 at $30 each
Applying FIFO means of the 150 shares sold:
100 were bought on day 1,
20 were bought on day 2
30 were bought on day 3
The capital gain in this case would be computed by:
Total amount for sale: 150 * 30 = $4500
Total amount of original purchase: (100 * 20) + (20 * 24) + (30 * 36) = $3560
Total capital gain: 4500 3560 = $940
Note: after this sale, the queue would contain: 170 shares at $36
Sample input file:
Buy 100 shares at 20 each.
Buy 20 shares at 24 each.
Buy 200 shares at 36 each.
Sell 150 shares at 30 each.
Sell 50 shares at 40 each.
Results:
Welcome to Debbie's Capital Gains Program!
Enter the filename: purchases.txt
Buying 100 shares at $20.0
Buying 20 shares at $24.0
Buying 200 shares at $36.0
Selling 150 shares at $30.0
Selling value: $4500.0
Debug A. total:2000.0 quantity:100 cost: 20.0
Debug A. total:2480.0 quantity:20 cost: 24.0
Debug B. total:3560.0 quantity:30 cost: 36.0
Total paid for shares: $3560.0
Total money gained: $940.0
Selling 50 shares at $40.0
Selling value: $2000.0
Debug B. total:1800.0 quantity:50 cost: 36.0
Total paid for shares: $1800.0
Total money gained: $200.0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
