示例#1
0
        /// <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);
            }
        }
示例#2
0
            //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);
            }
示例#3
0
        // 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.
        }