public void receiveConfirmation(OrderClass orderObject, DateTime confTimeStamp, Int32 finalAmount) //event handler
 {
     //receives confirmation on the event of a complete order
     if (this.name == orderObject.senderId) //checks if the broadcasted event specifies its sender id
     {
         Console.WriteLine("Store {0}'s order of {1} chickens was sucessfuly completed for a final amount of {2}" +
                           " at {3}", orderObject.senderId, orderObject.amount, finalAmount, confTimeStamp);
     }
 }
        /// <summary>
        /// This method decodes a string to an orderObject and sends it to the chikenFarm
        /// </summary>
        /// <param name="encodedObjString"></param>
        /// <returns></returns>
        public static OrderClass decode(String encodedObjString)
        {
            //deserializes the received objectstring to an orderObject
            XmlSerializer deserializer = new XmlSerializer(typeof(OrderClass));

            using (TextReader tr = new StringReader(encodedObjString))
            {
                OrderClass decodeOrderObj = (OrderClass)deserializer.Deserialize(tr);
                return(decodeOrderObj);
            }
        }
        /// <summary>
        /// This method converts(encodes) an object of the OrderClass to a String and sends it the retailer
        /// </summary>
        /// <param name="orderObject"></param>
        /// <returns></returns>
        public static String encode(OrderClass orderObject)
        {
            string serializedData = string.Empty;  // The string variable that will hold the serialized data

            XmlSerializer serializer = new XmlSerializer(orderObject.GetType());

            using (StringWriter sw = new StringWriter())
            {
                serializer.Serialize(sw, orderObject); //Serializes an object to a XML string
                serializedData = sw.ToString();
                return(serializedData);
            }
        }
        /// <summary>
        /// This method places an order. It creates an order object, encodes it and places it in a MultiCellBuffer
        /// </summary>
        public void placeOrder()
        {
            OrderClass order = new OrderClass();

            order.cardNumber = this.cardNumber;
            order.amount     = rng.Next(1, 10); //calculates the quantity of chiken to be ordered by generating a random number
            order.senderId   = this.name;
            order.timeStamp  = DateTime.Now;    //stores the timestamp at which the order was placed
            String orderObjString = EncodeDecode.encode(order);

            ResourcePool.countEmptySpaces.WaitOne(); //Semaphore allows the thread if buffer has space else blocks it
            myApplication.mcb.setOneCell(orderObjString);
        }
 /// <summary>
 /// This method is started as a thread by the farmerFunc thread.
 /// It checks the MultiCellBuffer once in every 500ms
 /// </summary>
 public void startAcceptOrder()
 {
     Console.WriteLine("looking for orders");
     while (myApplication.runApp)                  //thread accepts order as long as the farmer thread is running
     {                                             //myApplication.runApp is bool that checks if farmer thread is alive
         Thread.Sleep(500);
         ResourcePool.countFilledSpaces.WaitOne(); //Semaphore allows the thread if there is something in the buffer to process
         String     objString      = myApplication.mcb.getOneCell();
         OrderClass orderObj       = EncodeDecode.decode(objString);
         Int32      currPrice      = this.getPrice();
         Thread     orderProcessor = new Thread(() => orderProcessing(orderObj, currPrice));
         orderProcessor.Start();
     }
 }
 /// <summary>
 /// Method proceeses an order and emits completeOrder event on successful processing
 /// This method is started as a thread by startAcceptOrder thread
 /// </summary>
 /// <param name="orderObject"></param>
 /// <param name="p"></param>
 public void orderProcessing(OrderClass orderObject, Int32 p)
 {
     Console.WriteLine("started procesing order store {0}'s order of {1} chickens placed at {2}",
                       orderObject.senderId, orderObject.amount, orderObject.timeStamp);
     if (orderObject.cardNumber >= 900000 && orderObject.cardNumber <= 999999)
     {
         Int32 tax         = Convert.ToInt32((orderObject.amount * p) * (0.1)); //10% Tax and rounding the nearest integer
         Int32 shipping    = 5;                                                 //constnt amount of 5 for shipping
         Int32 finalAmount = (orderObject.amount * p) + tax + shipping;
         if (completeOrder != null)
         {
             completeOrder(orderObject, DateTime.Now, finalAmount); //emit event
         }
     }
     else
     {   //prints when order can't be processed
         Console.WriteLine("Sorry, {0}- your order was not processed", orderObject.senderId);
     }
 }