stockportfolio
Interface IPortfolio

All Known Implementing Classes:
Portfolio

public interface IPortfolio

Stock portfolio manager. Allows buy and sell actions on a portfolio of stock, and computes capital gains on sell actions.


Method Summary
 boolean buy(java.lang.String stock, java.util.Date date, double shares, double pricePerShare)
          Add a block of stock to the portfolio.
 java.util.List getBlocksOwned()
          Return a list of all the blocks of stock that are owned.
 double getSharesOwned(java.lang.String stock)
          Tells the number of shares owned of a particular stock.
 double sell(java.lang.String stock, java.util.Date saleDate, double shares, double pricePerShare)
          "Sell" a block of a stock, in the process calculating the capital gain on the stock.
 java.lang.String toString()
          Gives a compact summary of the information in the Portfolio.
 

Method Detail

buy

public boolean buy(java.lang.String stock,
                   java.util.Date date,
                   double shares,
                   double pricePerShare)
Add a block of stock to the portfolio. A block is one purchase of a number of shares at a given price.

Parameters:
stock - a non-null stock
date - a non-null date/time. You may assume for now that each block is at a later date/time than any previous block bought.
shares - a number of shares, must be > 0.
pricePerShare - price of each share, must be > 0.
Returns:
true iff the buy was successful, i.e., if the block was added to the portfolio.
Throws:
java.lang.IllegalArgumentException - if any argument value is invalid.

sell

public double sell(java.lang.String stock,
                   java.util.Date saleDate,
                   double shares,
                   double pricePerShare)
"Sell" a block of a stock, in the process calculating the capital gain on the stock. After being sold, the amount of the stock held in the portfolio is decreased by the amount sold. The "first in first out" rule is used for determining which shares are sold. The block does not have to correspond exactly in size to any block purchased.

Parameters:
stock - non-null stock being sold; must be a stock currently owned.
saleDate - non-null date of the sale.
shares - number of shares being sold, must be >= 0.
pricePerShare - price of each share, must be >= 0.
Returns:
double the capital gain on the sale.
Throws:
java.lang.IllegalArgumentException - if any parameter value is invalid, or if the number of shares being sold is greater than the number of shares of that stock currently owned.

getSharesOwned

public double getSharesOwned(java.lang.String stock)
Tells the number of shares owned of a particular stock. It is not an error to ask about a stock which is not owned -- the method simply returns 0.0 in this case.

Parameters:
stock - a non-null stock.
Returns:
number of shares owned, which must be >= 0.

getBlocksOwned

public java.util.List getBlocksOwned()
Return a list of all the blocks of stock that are owned. The list should be in order by stock name, then by date, then by number of shares owned, then by share price. Hint: you can do this sort in ONE LINE using Collections.sort (if you have things set up right...)

Returns:
a list of non-null stock blocks, in the order mentioned. Each block is either the full block from some previous buy operation, or is a partial block of stocks left over after some sell operation.

toString

public java.lang.String toString()
Gives a compact summary of the information in the Portfolio. Should show the names of the stocks held, and the number of shares of each; one per line, or several per line, is recommended.