StockExample.java
import java.util.*;
import sos.dated.util.*;
import sos.dated.util.Iterator;
public class StockExample
{
private Map stockMarket;
private static Date[] d;
private static final int NUM_DATES = 100;
private static Random r = new Random( 1 );
static
{
Calendar calendar = Calendar.getInstance();
calendar.set( 2003, 1, 1 );
d = new Date[NUM_DATES];
for( int i = 0; i < NUM_DATES; )
{
calendar.add( Calendar.DAY_OF_MONTH, 1 );
int day = calendar.get( Calendar.DAY_OF_WEEK );
if( day == Calendar.SATURDAY || day == Calendar.SUNDAY )
continue;
Date date = calendar.getTime();
d[i] = date;
i++;
}
}
public StockExample()
{
initializeStockMarket();
}
private void initializeStock( String stock, double initialValue )
{
DatedValue values = new ValueByDate();
stockMarket.put( stock, values );
double value = initialValue;
for( int i = 0; i < d.length - 1; i++ )
{
values.set( new Double( value ), d[i], d[i+1] );
double change = r.nextDouble();
value += change < 0.6? -change : change;
if( value < 0.0 )
value = 0.0;
}
}
private void initializeStockMarket()
{
stockMarket = new HashMap();
initializeStock( "Global Software For Geeks", 100.00 );
initializeStock( "Compewter Materials", 50.00 );
initializeStock( "Mice With Wheels", 10.50 );
}
private double lookupStockPrice( String stock, Date date )
{
DatedValue values = (DatedValue)stockMarket.get( stock );
if( values == null )
throw new NoSuchElementException();
Double value = (Double)values.get( date );
assert value != null;
return value.doubleValue();
}
private double getPortfolioValue( DatedMap portfolio, Date date )
{
double total = 0.0;
DatedSet entrySet = portfolio.entrySet();
Iterator iter = entrySet.iterator( date );
while( iter.hasNext() )
{
DatedMap.Entry entry = (DatedMap.Entry)iter.next();
String stock = (String)entry.getKey();
Integer shares = (Integer)entry.getValue();
double price = lookupStockPrice( stock, date );
double value = price * shares.intValue();
total += value;
}
return total;
}
private void buyStock( DatedMap portfolio, String stock, int numShares, Date date )
{
Integer currentShares = (Integer)portfolio.get( stock, date );
if( currentShares == null )
{
portfolio.put( stock, new Integer( numShares ), date, DatedObject.MAX_DATE );
return;
}
int totalShares = currentShares.intValue() + numShares;
portfolio.put( stock, new Integer( totalShares ), date, DatedObject.MAX_DATE );
}
public void run()
{
DatedMap portfolio = new TreeMapByKey();
double value = getPortfolioValue( portfolio, d[0] );
assert value == 0.0;
System.out.println( "Portfolio on " + d[0] + ": " + portfolio.toString( d[0] ));
buyStock( portfolio, "Compewter Materials", 100, d[10] );
buyStock( portfolio, "Mice With Wheels", 250, d[10] );
System.out.println( "Portfolio on " + d[15] + ": " + portfolio.toString( d[15] ));
value = getPortfolioValue( portfolio, d[15] );
System.out.println( "Portfolio value on " + d[15] + " is " + value );
buyStock( portfolio, "Global Software For Geeks", 10, d[50] );
System.out.println( "Portfolio on " + d[50] + ": " + portfolio.toString( d[50] ));
value = getPortfolioValue( portfolio, d[50] );
System.out.println( "Portfolio value on " + d[50] + " is " + value );
buyStock( portfolio, "Mice With Wheels", 20, d[75] );
System.out.println( "Portfolio on " + d[80] + ": " + portfolio.toString( d[80] ));
value = getPortfolioValue( portfolio, d[80] );
System.out.println( "Portfolio value on " + d[80] + " is " + value );
System.out.println( "Entire portfolio: " + portfolio );
System.out.println( "Entire stock market: " + stockMarket );
}
public static void main(String[] args)
{
StockExample example = new StockExample();
example.run();
}
}