//Calculates the unit price for a book with current market conditions public double calcPrice(OrderObject o) { //Base setting for output to keep final price calculation within 50 to 200 double unitPrice = 0; //Check to see if any orders are older than the reference timespan Int32 count = 0; //Counts number of OrderObjects that are relatively too old to include in price calculation foreach (OrderObject order in orders) { if (o.getTimestamp() - order.getTimestamp() > reference) { count++; } } //Deqeue the older OrderObjects while (count != 0) { orders.Dequeue(); count--; } orders.Enqueue(o); //Put new order in the queue setNumOrders(); //Set the appropriate number of recent orders unitPrice += this.getUnitPrice(); return(unitPrice); }
/// <summary> /// Testing method for the encoder and decode functions. /// </summary> public static void EncodeDecodeTest() { DateTime now = new DateTime(); Publisher p = new Publisher(1); Random rdm = new Random(); Bookstore bookstore = new Bookstore(1); for (int i = 0; i < 20; i++) { now = DateTime.Now; OrderObject o = new OrderObject ( i, //senderId (Int32)(rdm.NextDouble() * 10000), //cardNo i, //receiverId (Int32)(rdm.NextDouble() * 200), //amount (Int32)(rdm.NextDouble() * 100), //unitPrice now, //timestamp, DateTime.Now.Millisecond ); string str = bookstore.Encoder(o); Console.WriteLine("The OrderObject's actual contents were: " + o.getBookStoreId() + "," + o.getCardNo().ToString() + "," + o.getPublisherId() + "," + o.getAmount().ToString() + "," + o.getUnitPrice().ToString() + "," + o.getTimestamp().ToString()); Console.WriteLine("The string created by the encoder was: " + str); o = p.getDecoder().decode(str); Console.WriteLine("The Decoder created another OrderObject from the string created by the encoder whose contents were: \n" + o.getBookStoreId() + "," + o.getCardNo().ToString() + "," + o.getPublisherId() + "," + o.getAmount().ToString() + "," + o.getUnitPrice().ToString() + "," + o.getTimestamp().ToString() + "\n"); System.Threading.Thread.Sleep(50); } }
// Encoder -- turns OrderObject into a CSV string public String Encoder(OrderObject order) { String orderStr = null; // build CSV String orderStr = order.getBookStoreId().ToString();; orderStr += "," + order.getCardNo().ToString(); orderStr += "," + order.getPublisherId().ToString(); orderStr += "," + order.getAmount().ToString(); orderStr += "," + order.getUnitPrice().ToString(); orderStr += "," + order.getTimestamp(); orderStr += "," + order.getTicks().ToString(); return(orderStr); // return the encoded string just created. }