/// <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); } }
/// <summary> /// Test method for the pricing model. /// </summary> public static void TestPricingModel() { DateTime now = new DateTime(); Publisher p = new Publisher(1); Random rdm = new Random(); for (int i = 0; i < 20; i++) { now = DateTime.Now; OrderObject o = new OrderObject(i, (Int32)(rdm.NextDouble() * 10000), i, (Int32)(rdm.NextDouble() * 200), 100, now, DateTime.Now.Millisecond); double price = Math.Round(p.getModeler().calcPrice(o), 2); Console.WriteLine("ObjectOrder " + o.getBookStoreId() + " paid $" + price.ToString() + ". The number of books in the order was " + o.getAmount() + ". The number of orders in the recent orders " + "queue was " + p.getModeler().getQueue().Count + ". " + "The Publisher has " + p.getBooks().ToString() + " books.\n"); o.setUnitPrice(price); System.Threading.Thread.Sleep((Int32)(rdm.NextDouble() * 1000)); } Console.WriteLine("There are " + p.getModeler().getQueue().Count.ToString() + " in the recent orders queue.\n"); foreach (OrderObject order in p.getModeler().getQueue()) { Console.WriteLine("Order " + order.getBookStoreId() + " paid $" + order.getUnitPrice().ToString() + ".\n"); } }
//Thread entry point for OrderProcessing object public static void OrderProcessingThread(OrderObject obj) { //Validate the credit card number if (obj.getCardNo() > 6000 || obj.getCardNo() < 5000) { Console.WriteLine("OrderProcessingThread found an invalid credit card number, order not processed."); return; } //Calculate total price double totalPrice = obj.getAmount() * obj.getUnitPrice() //Unit price multiplied by total quantity of books ordered + obj.getAmount() * obj.getUnitPrice() * TAX_RATE //Add tax rate + (obj.getBookStoreId() * SHIPPING_PREMIUM); //Add shipping cost, assumed higher numbered bookstores are further away //Initiate event signifying order was processed orderComplete(obj.getBookStoreId(), obj.getPublisherId(), obj.getAmount(), obj.getUnitPrice(), totalPrice, obj.getTicks(), DateTime.Now.Ticks); }
// 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. }