Class Trader

java.lang.Object
  |
  +--Trader
All Implemented Interfaces:
Enron, Runnable

public class Trader
extends Object
implements Runnable, Enron

CS 537, Fall 2002, Project 3. A centralized trader. This trader "manufactures" one unit of each commodity from time to time and server customers' requests as soon as possible. If it has multiple requests, it satisfies them in a variety of orders, depending on the algorithm specified in the constructor.

Copyright © 2002 by Marvin Solomon. All rights reserved.


Field Summary
private  int algorithm
          The algorithm used by this Trader.
private  int[] delivered
          The total amount delivered to Customers
private  boolean done
          Flag to indicate that the Supplier has told us to shut down.
private  int fulfilled
          Number of requests fulfilled
private  int iterations
          The number of iterations to run.
private static int NUM_ALGORITHMS
          The number of algorithms implemented.
private  int[] onHand
          The current stock on hand of each of the commodities
private  int shutdownCount
          Used to keep track of the number of Customers who have been informed that the system is shutting down.
private  int[] supplied
          The total amount received from the Supplier
private  List waiters
          Queue of waiting customers.
 
Fields inherited from interface Enron
cname, COAL, COMMODITIES, cunit, ELEC, GAS, OIL
 
Constructor Summary
Trader(int algorithm)
          Creates a new Trader.
 
Method Summary
private  void algorithm1()
          Tries to satisfy and release one or more customers.
private  void algorithm2()
          Tries to satisfy and release one or more customers.
private  void algorithm3()
          Tries to satisfy and release one or more customers.
private  void algorithm4()
          Tries to satisfy and release one or more customers.
 boolean buy(int id, int[] amt)
          Accepts a request from a customer and blocks the customer until the request can be satisfied.
private  Order enqueue(int id, int[] amt)
          Creates an Order object and places it onto the waiters list.
private static void incr(int[] a, int[] b)
          Utility procedure to update an array.
private static void pl(Object o)
          Abbreviation for System.out.println.
private  void recordFulfillment(int[] amt)
          Record the fact that an order was fulfilled.
 void run()
          Main loop.
private  void showQueue()
           
 void shutDown()
          Tells this Trader to shut down.
 void supply(int[] amount)
          Accepts more resources from the supplier.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

iterations

private int iterations
The number of iterations to run.


algorithm

private int algorithm
The algorithm used by this Trader.


NUM_ALGORITHMS

private static final int NUM_ALGORITHMS
The number of algorithms implemented.

See Also:
Constant Field Values

done

private boolean done
Flag to indicate that the Supplier has told us to shut down.


waiters

private List waiters
Queue of waiting customers.


onHand

private int[] onHand
The current stock on hand of each of the commodities


supplied

private int[] supplied
The total amount received from the Supplier


fulfilled

private int fulfilled
Number of requests fulfilled


delivered

private int[] delivered
The total amount delivered to Customers


shutdownCount

private int shutdownCount
Used to keep track of the number of Customers who have been informed that the system is shutting down.

Constructor Detail

Trader

public Trader(int algorithm)
Creates a new Trader.

Parameters:
algorithm - the algorithm used to choose among requests.
Method Detail

supply

public void supply(int[] amount)
Accepts more resources from the supplier.

Parameters:
amount - the number of each commodity being supplied.

buy

public boolean buy(int id,
                   int[] amt)
Accepts a request from a customer and blocks the customer until the request can be satisfied.

Parameters:
id - the requesting customer's id (for debugging output).
amt - the request.
Returns:
false if the request is rejected because the system is being shut down.

shutDown

public void shutDown()
Tells this Trader to shut down.


enqueue

private Order enqueue(int id,
                      int[] amt)
Creates an Order object and places it onto the waiters list.

Parameters:
id - the requesting customer's id (for debugging output).
amt - the request.
Returns:
the created Order, or null if we are shutting down (done == true).

pl

private static void pl(Object o)
Abbreviation for System.out.println.

Parameters:
o - the object to be printed.

run

public void run()
Main loop.

Specified by:
run in interface Runnable

showQueue

private void showQueue()

algorithm1

private void algorithm1()
Tries to satisfy and release one or more customers. Uses algorithm 1. Should be called whenever conditions change. Only called from synchronized methods.


algorithm2

private void algorithm2()
Tries to satisfy and release one or more customers. Uses algorithm 2. Should be called whenever conditions change. Only called from synchronized methods.


algorithm3

private void algorithm3()
Tries to satisfy and release one or more customers. Uses algorithm 3. Should be called whenever conditions change. Only called from synchronized methods.


algorithm4

private void algorithm4()
Tries to satisfy and release one or more customers. Uses algorithm 4. Should be called whenever conditions change. Only called from synchronized methods.


incr

private static void incr(int[] a,
                         int[] b)
Utility procedure to update an array. a[i] += b[i] for each i.

Parameters:
a - the array to be updated.
b - the amount by which to update.

recordFulfillment

private void recordFulfillment(int[] amt)
Record the fact that an order was fulfilled.

Parameters:
amt - the order.