home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / ODesign / SetupPSE.exe / data.z / Security.java < prev    next >
Encoding:
Java Source  |  1997-04-23  |  2.3 KB  |  107 lines

  1. package COM.odi.demo.pport;
  2.  
  3. /**
  4.  *      <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
  5.  *
  6.  * A Security has a history of its price over time.
  7.  */
  8.  
  9. import java.io.*;
  10. import java.util.*;
  11. import COM.odi.*;
  12. import COM.odi.util.*;
  13.  
  14. final
  15. public class Security
  16. {
  17.   /* The ticker symbol. */
  18.  
  19.   String symbol;
  20.  
  21.   /* The price history, i.e. what price did it have at a given date. */
  22.  
  23.   private TimeSeries priceHistory;
  24.  
  25.   /* Hash table of all Security objects, keyed by symbol. */
  26.  
  27.   static OSHashtable all = new OSHashtable();
  28.  
  29.  
  30.   Security(String symbol)
  31.   {
  32.     this.symbol = symbol;
  33.     priceHistory = new TimeSeries(50, 50);
  34.     all.put(symbol, this);
  35.   }
  36.  
  37.   static Security get(String symbol)
  38.   {
  39.     Security result = (Security)all.get(symbol);
  40.     if (result == null) {
  41.       result = new Security(symbol);
  42.       
  43.       /* Temporary, for debugging */
  44.       System.out.println("New security " + symbol);
  45.  
  46.     }
  47.     return result;
  48.   }
  49.  
  50.   int getPrice(TradeDate date)
  51.   {
  52.     return priceHistory.lookup(date.year, date.month, date.day);
  53.   }
  54.  
  55.   /**
  56.    * Load up a new database from the textual securities file.
  57.    * @param pathname The pathname of the securities file.
  58.    * @param db The database to store the objects into.
  59.    */
  60.   static void load(String pathname, Database db) throws IOException
  61.   {
  62.     BufferedReader stream = null;
  63.     try {
  64.       stream = new BufferedReader(new FileReader(pathname));
  65.       all.clear();
  66.       String line = stream.readLine();
  67.       while (line != null) {
  68.     StringTokenizer tkn = new StringTokenizer(line, "\t");
  69.     Security sec = get(tkn.nextToken());
  70.     TradeDate date = new TradeDate(tkn.nextToken());
  71.     int price = (int) (1000.0 * (new Float(tkn.nextToken())).floatValue());
  72.     sec.priceHistory.insert(date, price);
  73.     line = stream.readLine();
  74.       }
  75.     } finally {
  76.       if (stream != null)
  77.     stream.close();
  78.     }
  79.     db.createRoot("securities", all);
  80.   }
  81.  
  82.   static void initialize(Database db)
  83.   {
  84.     all = (OSHashtable)db.getRoot("securities");
  85.   }
  86.  
  87.   public String getSymbol() {
  88.     return symbol;
  89.   }
  90.  
  91.   public TimeSeries getPriceHistory() {
  92.     return priceHistory;
  93.   }
  94.  
  95.   static public Enumeration getAll() {
  96.     return all.elements();
  97.   }
  98.  
  99.  
  100.   /* This class is never used as a persistent hash key. */
  101.   public int hashCode() {
  102.     return super.hashCode();
  103.   }
  104.  
  105. }
  106.  
  107.