stockportfolio
Class Portfolio

java.lang.Object
  extended bystockportfolio.Portfolio
All Implemented Interfaces:
IPortfolio

public class Portfolio
extends java.lang.Object
implements IPortfolio

A portfolio represents stocks that are owned. The portfolio remembers the name of each share (or group of shares) and its purchase price. The order in which the stocks are purchased is remembered so that a "oldest sold first" calculation can be made for capital gain when a stock is sold. It is assumed that stocks are added to the portfolio in the order they are purchased and this order never changes. The stock added in a single buy operation constitutes a "block". The portfolio remembers these blocks and deletes them only when a full block's worth of stock is sold. A block is never split up or combined with another block. HOWEVER, if a stock sale only uses part of a block, a new block is created to hold the remaining shares; such a block has the same date and price per share as the original. The fact that it has the same date implies it has the same order as the original block -- it does NOT go to the rear of the queue.


Constructor Summary
Portfolio()
          Create a new, empty portfolio.
 
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.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Portfolio

public Portfolio()
Create a new, empty portfolio.

Method Detail

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.

Specified by:
toString in interface IPortfolio

buy

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

Specified by:
buy in interface IPortfolio
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.

getBlocksOwned

public java.util.List getBlocksOwned()
Description copied from interface: IPortfolio
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...)

Specified by:
getBlocksOwned in interface IPortfolio
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.

getSharesOwned

public double getSharesOwned(java.lang.String stock)
Description copied from interface: IPortfolio
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.

Specified by:
getSharesOwned in interface IPortfolio
Parameters:
stock - a non-null stock.
Returns:
number of shares owned, which must be >= 0.

sell

public double sell(java.lang.String stock,
                   java.util.Date saleDate,
                   double shares,
                   double pricePerShare)
Description copied from interface: IPortfolio
"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.

Specified by:
sell in interface IPortfolio
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.