public String encode(OrderClass orderClassObject)
 {
     String orderString = "";
     orderString += orderClassObject.SenderId + ",";
     orderString += orderClassObject.ReceiverId + ",";
     orderString += orderClassObject.CardNo + ",";
     orderString += orderClassObject.Amount;
     return orderString;
 }
        public OrderClass decode(String order)
        {
            OrderClass orderObject = new OrderClass();
            if (order != null)
            {
                String[] orderArray = order.Split(',');
                orderObject.SenderId = orderArray[0];
                orderObject.ReceiverId = orderArray[1];
                orderObject.CardNo = orderArray[2];
                orderObject.Amount = Convert.ToInt32(orderArray[3]);
            }

            return orderObject;
        }
 public OrderProcessing(HotelSupplier hotel, OrderClass order, string supplierName)
 {
     this.hotel = hotel;
     this.order = order;
     this.supplierName = supplierName;
 }
        public void run()
        {
            while (counter < 3)
            {

                HotelSupplier hotel = new HotelSupplier();

                if (waitHandler > 0)
                {
                    autoReset.WaitOne();
                }
                waitHandler++;
                hotelPricingModel();

                String orderString;
                int count = 0;
                hotel.NoOfRoom = 30;
                hotel.NoOfAvailableRooms = 30;

                //Processing Order
              while (count < 5)
               {
                    for (int i = 0; i < 3; i++)
                    {
                        orderString = Program.buffer[i].getBuffer(hotelName);
                        if (orderString != null)
                        {
                            //decoding the buffere object
                            order = new Decoder().decode(orderString);
                           count++;
                            OrderProcessing orderProcessing = new OrderProcessing(hotel, order, hotelName);
                            Thread orderProcessingThread = new Thread(new ThreadStart(orderProcessing.processOrder));
                            orderProcessingThread.Start();
                            orderProcessingThread.Join();
                        }
                        else
                        {
                            Thread.Sleep(1000);
                        }
                    }
                    Thread.Sleep(500);

              }
                autoReset.Set();
                Console.WriteLine("");
                Thread.Sleep(500);

            }
        }
        public void run()
        {
            while (true)
            {

                Thread.Sleep(500);
                //Aborting Threads after the hotel supplier thread has stopped running
              if (totalRuns == maxRuns)
                {

                    manualReset.Set();
                    agencyCount++;
                    Thread.CurrentThread.Abort();

                }

              manualReset.WaitOne();

                if (totalRuns == maxRuns)
                {

                    agencyCount++;
                    if (agencyCount == 4)
                    {
                        try
                        {
                            Console.WriteLine("Program ran successfully");
                            Console.ReadLine();
                        }
                        catch
                        {

                        }
                    }
                    Thread.CurrentThread.Abort();
                }

                bankServiceClient = new BankService.Service1Client();
                OrderClass order = new OrderClass();
                order.ReceiverId = supplierName;
                order.SenderId = Thread.CurrentThread.Name;

                // order amount based on the previos price and the new price
                if(previousPrice<newPrice)
                {
                    order.Amount = random.Next(1, 10);
                }

                else
                {
                    order.Amount = random.Next(10, 15);
                }

                while ( order.CardNo == null || "".Equals(order.CardNo))
                {
                    order.CardNo = Convert.ToString(bankServiceClient.getCard());
                    if(order.CardNo.Equals('0'))
                    {
                        Console.WriteLine("Card will be issued after checking the available card numbers");
                    }
                }

                //placing order in the buffer after encoding
                Boolean b = true;
                DateTime time = DateTime.Now;
                while (b)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        if (Program.buffer[i].getBuffer("temp") == null)
                        {
                            lock (Program.buffer[i])
                            {
                                time = DateTime.Now;
                                Program.buffer[i].setBuffer(new Encoder().encode(order));
                                b = false;
                            }

                            break;
                        }

                    }
                }

                //checking the processed order;
                b = true;
                Thread.Sleep(500);
                string orderString = order.SenderId + "and" + order.ReceiverId;

                while (b)
                {
                    if (Program.processedOrder.getBuffer(orderString) != null)
                    {
                        Console.WriteLine("Time taken to process the order is :" + (DateTime.Now - time).TotalSeconds + " seconds");
                        b = false;

                    }
                    else
                        Thread.Sleep(500);
                }

                manualReset.Reset();

                totalRuns++;

            }
        }