package fauxExchange; /** * @author John Rayburn * @since JDK 1.3 */ public abstract class Order implements Comparable { /** Indicates a buying order */ public static final int BUY = 1; /** Indicates a selling order */ public static final int SELL = 0; /** LOCK */ public static final Object LOCK = new Object(); /** Number of shares */ int size; /** Type of order (BUY or SELL) */ int type; /** Indicates if this order is a MarketOrder */ boolean isMarket = false; /** Partipant of order */ int participant; /** Unique order number id */ int orderNumber; /** Price requested */ double price; /** Last Order Number */ private static int lastOrderNumber = -1; /** Time order was placed */ static long time; /** Security ID for which this order is being traded */ int securityId; /** */ protected static int getNextOrderNumber () { synchronized (LOCK){ lastOrderNumber++; return lastOrderNumber; } } /** */ public boolean equals (Object o){ return ((Order)o).getOrderNumber() == this.getOrderNumber(); } public void print (java.io.PrintStream io){ if (io == null){ io = System.out; } String buyorsell = ""; if (type == SELL){ buyorsell = "sell"; }else if (type == BUY){ buyorsell = "buy"; } java.text.NumberFormat nf = java.text.NumberFormat.getCurrencyInstance(); io.print("[" + (new java.util.Date(System.currentTimeMillis())).toString() + "] Order: " + " Participant " + getParticipant() + " wishes to " + buyorsell + " " + size + " shares of " + securityId); if (isMarket()) System.out.println(" at Market Price."); else System.out.println(" for " + nf.format(getPrice())); } public long getTime() { return time; } public int getSize(){ return size; } public int getParticipant(){ return participant; }; public double getPrice(){ return price; } public boolean isMarket(){ return isMarket; } public int getOrderNumber(){ return orderNumber; } public int getType(){ return type; } public boolean isSell(){ return type == SELL; } public boolean isBuy(){ return type == BUY; } public int getSecurityID () { return securityId; } /** To change the size */ public void setSize(int size){ this.size = size; } protected void setParticipant(int participant) { this.participant = participant; }; protected void setPrice(double price) { this.price = price; } protected void setIsMarket(boolean isMarket) { this.isMarket = isMarket; } protected void setOrderNumber(int orderNumber) { this.orderNumber = orderNumber; } protected void setType(int type) { this.type = type; } protected void setTime(long time) { this.time = time; } protected void setSecurityID(int id) { this.securityId = id; } }