home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / bullorbear.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.5 KB  |  157 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  4.  
  5.     Dow Jones makes no representations or warranties about 
  6.     the suitability of this software, either express or 
  7.     implied, including but not limited to the implied warranties 
  8.     of merchantability, fitness for a particular purpose, 
  9.     or non-infringement.  Dow Jones will not be liable for 
  10.     any damages suffered by a user as a result of using, 
  11.     modifying or distributing this software or its derivatives.
  12.  
  13.  
  14.     @(#)BullOrBear.java  0.00 01-Jan-96
  15.  
  16.         An image that observes the paper, and becomes a bull or 
  17.         a bear when the change DJIA arrives.
  18.  
  19.     Authors:
  20.  
  21.         Ted      Skolnick
  22.  
  23.  
  24.     Version Ident:
  25.  
  26.         $Header: /PjJavaClient/src/pj/awt/BullOrBear.java 3     2/11/96 4:39p Rphall $
  27.  
  28.     History:
  29.  
  30.         0.00 18-Mar-96  Ted S.      Initial Creation
  31.  
  32. ---------------------------------------------------------------------------*/
  33.  
  34. package pj.awt;
  35.  
  36. import pj.awt.GifCanvas;
  37. import pj.io.Paper;
  38. import pj.io.PaperSection;
  39. import pj.io.PaperStory;
  40. import pj.io.StoryContainerNotification;
  41. import pj.awt.PjImages;
  42.  
  43.  
  44. import java.util.Observable;
  45. import java.util.Observer;
  46. import java.util.Vector;
  47. import java.util.StringTokenizer;
  48. import java.lang.Float;
  49.  
  50.  
  51. /**
  52.  * An image that observes the paper, and becomes a bull or 
  53.  *       a bear when the change DJIA arrives.
  54.  *
  55.  * @version 0.00 18-Mar-96
  56.  * @author  Ted S.
  57. */
  58. public class BullOrBear extends GifCanvas implements Observer
  59.     {
  60.  
  61.      // --- Public constructors
  62.  
  63.     /**
  64.      * Construct a BullOrBear
  65.      * @param p         The paper that contains the observed section.
  66.     */
  67.     public BullOrBear( Paper p)
  68.         {
  69.         // PjImages holds all of Pj's images.
  70.         PjImages Imgs = new PjImages();
  71.  
  72.         // Add the bull as a default
  73.         setImage( Imgs.imagespecs()[ PjImages.idxBull ].getImage() );
  74.                
  75.         m_paper = p;
  76.         m_paper.addObserver(this);
  77.         }
  78.  
  79.     // --- Public operations
  80.  
  81.     /**
  82.      * Update Bull or bear upon notification from a PaperSection.
  83.      * @param src The object that sent or forwarded the notification.
  84.      * @param arg The notification.  An instance of StoryContainerNotification
  85.      * is expected.
  86.     */
  87.     public synchronized void update(Observable src, Object arg)
  88.         {
  89.         if ( !(arg instanceof StoryContainerNotification) )
  90.             return;
  91.  
  92.         StoryContainerNotification scn = (StoryContainerNotification) arg;
  93.  
  94.         if ( !(Paper.idMrkStkIdx.equals(scn.strSubsect)) )
  95.             return;
  96.  
  97.         if (scn.newState != PaperSection.stateAdded)
  98.             return;
  99.  
  100.         ChooseBullOrBear();        
  101.         } // update
  102.  
  103.  
  104.  
  105.     // Return the index of the icon in PjImages that has the Bull 
  106.     // or the Bear, depending on the change in the DJIA
  107.     private void  ChooseBullOrBear()
  108.         {
  109.         String szTemp = "";        
  110.         Float fPrev = new Float( 0 );
  111.         Float fLast = new Float( 0 );
  112.         String szStory;        
  113.         PjImages Imgs = new PjImages();
  114.         
  115.  
  116.         //Get the 1st and 2nds token in the Stock indexes story
  117.         // These are the last and the close values.
  118.         //
  119.         try
  120.             {
  121.             szStory = m_paper.section( Paper.idMrkStkIdx ).firstStory().getBody();
  122.             StringTokenizer stEntries = new StringTokenizer( szStory, ",", false );
  123.             szTemp = stEntries.nextToken();   
  124.             szTemp = stEntries.nextToken(); 
  125.  
  126.             // Convert Prev value token to a float
  127.             try 
  128.                 {
  129.                 fPrev = fPrev.valueOf( szTemp );
  130.                 }
  131.             catch ( Exception e ){}  
  132.  
  133.             // Convert last value token to a float
  134.             szTemp = stEntries.nextToken();            
  135.             try 
  136.                 {
  137.                 fLast = fLast.valueOf( szTemp );
  138.                 }
  139.             catch ( Exception e ){}            
  140.             }
  141.         catch ( Exception e ){}
  142.         
  143.         // If it starts with a "+" then the change is positive and the market
  144.         // is charging like a bull.
  145.         if ( (fLast.floatValue() >= fPrev.floatValue() ) )
  146.             setImage( Imgs.imagespecs()[ PjImages.idxBull ].getImage() );
  147.         else
  148.             setImage( Imgs.imagespecs()[ PjImages.idxBear ].getImage() );
  149.         }
  150.              
  151.  
  152.     
  153. // --- Instance variables
  154.     private Paper m_paper;   
  155.     
  156.     } // BullOrBear
  157.