// method to check card validity and calculate total price of order
 public void processOrder(OrderClass order, Int32 unitPrice)
 {
     if (order.cardNo <= 7000 && order.cardNo >= 5000)
     {
         totalPrice = unitPrice * order.amount + (tax / 100) * (unitPrice * order.amount) + shippingCost;
         onOrderProcessed(order.senderId, order.amount, unitPrice, totalPrice, order.timeOfOrder);
     }
     else
     {
         Console.WriteLine("Invalid Card Number => {0}! Order could not be processed!", order.cardNo);
     }
 }
        // decode a given string to Order object
        public static OrderClass DecodeOrder(string encodedOrder)
        {
            OrderClass orderReceived = new OrderClass();

            string[] decodedOrder = coderServiceClient.Decrypt(encodedOrder).Split(',');

            if (decodedOrder.Length == 4)
            {
                orderReceived.senderId    = Convert.ToInt32(decodedOrder[0]);
                orderReceived.cardNo      = Convert.ToInt32(decodedOrder[1]);
                orderReceived.amount      = Convert.ToInt32(decodedOrder[2]);
                orderReceived.timeOfOrder = Convert.ToDateTime(decodedOrder[3]);
            }
            else
            {
                Console.WriteLine("Error in decoding order!");
            }

            return(orderReceived);
        }
        // event handler when order is created by a retailer and placed in buffer
        public void getOrder()
        {
            string orderReceived = Program.buffer.getOneCell();

            string[] orderDetails = new EncoderDecoderService.ServiceClient().Decrypt(orderReceived).Split(',');

            OrderClass order = new OrderClass();

            Console.WriteLine("Order Received from store {0}", orderDetails[0]);
            order.senderId    = Convert.ToInt32(orderDetails[0]);
            order.cardNo      = Convert.ToInt32(orderDetails[1]);
            order.amount      = Convert.ToInt32(orderDetails[2]);
            order.timeOfOrder = Convert.ToDateTime(orderDetails[3]);

            OrderProcessing op = new OrderProcessing();

            // thread created and started to process order fetched from buffer
            Thread processThread = new Thread(delegate() { op.processOrder(order, getChickenPrice()); });

            processThread.Start();
        }
        // creating order on price cut
        public void createOrder(int senderId, int price)
        {
            Int32 newChickenPrice = price;

            // create an order
            OrderClass order = new OrderClass();

            order.senderId    = senderId;
            order.cardNo      = randomNumberGenerator.Next(4500, 7000);
            amountOfChicken   = getAmountOfChicken(oldChickenPrice, newChickenPrice, amountOfChicken);
            order.amount      = amountOfChicken;
            order.timeOfOrder = DateTime.Now;

            Console.WriteLine("Order created for store {0}", senderId);

            //encode order
            string encodedOrder = EncodeDecodeOrder.EncodeOrder(order);

            // add order to Multi Cell Buffer
            Program.buffer.setOneCell(encodedOrder);

            // function for emitting event
            onOrderCreated();
        }
 // encode an Order object to a string
 public static string EncodeOrder(OrderClass order)
 {
     return(coderServiceClient.Encrypt(order.senderId.ToString() + ","
                                       + order.cardNo.ToString() + "," + order.amount.ToString() + "," + order.timeOfOrder.ToString()));
 }