FileDocCategorySizeDatePackage
Stock.javaAPI DocExample2953Sat Sep 12 03:01:00 BST 1998examples.stock

Stock

public class Stock extends Object implements Serializable
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.

Fields Summary
String
symbol
float
current
private static Random
random
private static final float
MAX_VALUE
Constructors Summary
public Stock(String name)
Constructs a stock with the given name with a initial random stock price.


                      
       
    
	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);
	}
    
Methods Summary
public floatupdate()
Update the stock price (generates a random change).

	float change = ((float)(random.nextGaussian() * 1.0));
	if (symbol.equals("Sun") && current < MAX_VALUE - 5)
	    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;