home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / RMI_OS / RMI-PREB / EXAMPLES / STOCK / STOCK.JAV < prev    next >
Encoding:
Text File  |  1996-11-08  |  2.2 KB  |  74 lines

  1. /*
  2.  * %W% %E%
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_pre-beta
  20.  */
  21. package examples.stock;
  22.  
  23. import java.util.Random;
  24.  
  25. /**
  26.  * A Stock object is used to encapsulate stock update information
  27.  * that is sent in an update notification. Note that the class
  28.  * implements the java.io.Serializable interface in order to be
  29.  * passed as an argument or return value in RMI.
  30.  */
  31. public class Stock implements java.io.Serializable {
  32.     String symbol;
  33.     float current;
  34.  
  35.     private static Random random = new Random();
  36.     private final static float MAX_VALUE = 67;
  37.  
  38.     /**
  39.      * Constructs a stock with the given name with a initial random
  40.      * stock price.
  41.      */
  42.     public Stock(String name) 
  43.     {
  44.     symbol = name;
  45.     if (symbol.equals("Sun")) {
  46.         current = 30.0f;
  47.     } else {
  48.         // generate random stock price between 20 and 60
  49.         current = (float)(Math.abs(random.nextInt()) % 40 + 20);
  50.     }
  51.     }
  52.  
  53.     /**
  54.      * Update the stock price (generates a random change).
  55.      */
  56.     public float update() 
  57.     {
  58.     float change = ((float)(random.nextGaussian() * 1.0));
  59.     if (symbol.equals("Sun"))
  60.         change = Math.abs(change);        // what did you expect?
  61.  
  62.     float newCurrent = current + change;
  63.  
  64.     // don't allow stock price to step outside range
  65.     if (newCurrent < 0 || newCurrent > MAX_VALUE)
  66.         change = 0;
  67.  
  68.     current += change;
  69.     
  70.     return change;
  71.     }
  72. }
  73.  
  74.