home *** CD-ROM | disk | FTP | other *** search
- /*
- * %W% %E%
- *
- * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
- *
- * Permission to use, copy, modify, and distribute this software
- * and its documentation for NON-COMMERCIAL purposes and without
- * fee is hereby granted provided that this copyright notice
- * appears in all copies. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
- * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
- * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
- * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- *
- * CopyrightVersion 1.1_pre-beta
- */
- package examples.stock;
-
- import java.util.Random;
-
- /**
- * A Stock object is used to encapsulate stock update information
- * that is sent in an update notification. Note that the class
- * implements the java.io.Serializable interface in order to be
- * passed as an argument or return value in RMI.
- */
- public class Stock implements java.io.Serializable {
- String symbol;
- float current;
-
- private static Random random = new Random();
- private final static float MAX_VALUE = 67;
-
- /**
- * Constructs a stock with the given name with a initial random
- * stock price.
- */
- public Stock(String name)
- {
- symbol = name;
- if (symbol.equals("Sun")) {
- current = 30.0f;
- } else {
- // generate random stock price between 20 and 60
- current = (float)(Math.abs(random.nextInt()) % 40 + 20);
- }
- }
-
- /**
- * Update the stock price (generates a random change).
- */
- public float update()
- {
- float change = ((float)(random.nextGaussian() * 1.0));
- if (symbol.equals("Sun"))
- change = Math.abs(change); // what did you expect?
-
- float newCurrent = current + change;
-
- // don't allow stock price to step outside range
- if (newCurrent < 0 || newCurrent > MAX_VALUE)
- change = 0;
-
- current += change;
-
- return change;
- }
- }
-
-