Exemplo n.º 1
0
 public void DealerFunc()
 {
     need = r1.Next(1200, 3000);                                           // The number of cars needed by each Dealer is selected using random function
     while (Mainapp.price_cut_count < 20)                                  // Mainapp.price_cut_count keeps track of number of price cuts
     {
         cut_event.WaitOne();                                              // Checking for price-cut event
         if (need > 0 && Math.Abs(Unit_Price - previous_cut) < diff[ID])   // Place an order if number of cars required by a dealer is greater than one and Maximum difference between the previous price and the current price is within limit
         {
             Mainapp.order_thread_count++;
             OrderClass dealer_object = new OrderClass();                // Create OrderClass object to place an order after converting to string
             Int32      amount        = r1.Next(1, 50);
             need = need - amount;
             dealer_object.setSenderID(ID + 1);
             dealer_object.setAmount(amount);
             dealer_object.setCreditCard(CreditCard[ID]);
             dealer_object.setUnitPrice(Unit_Price);
             String res = Encoder.Encode(dealer_object);                // Call encoder to convert DataObject to String
             Monitor.Enter(Mainapp.Buffer);                             // Acquire an exclusive lock on Buffer object
             try
             {
                 Mainapp.Buffer.setOneCell(res);                        // Call setOneCell to add string to the cell
                 Console.WriteLine("Dealer " + (ID + 1) + " has placed the order-->  " + "Number Of Cars:" + dealer_object.getAmount() + "  UnitPrice:" + dealer_object.getUnitPrice() + "  CreditCard:" + dealer_object.getCreditCard());
             }
             catch
             {
                 Console.WriteLine("Error");
             }
             finally { Monitor.Exit(Mainapp.Buffer); }                // Release an exclusive lock on Buffer object
         }
         cut_event.Reset();                                           // Set the event to false
     }
 }
Exemplo n.º 2
0
        // Encode function to convert String to object of type OrderClass by splitting using space whcih is the delimiter used in Encoder class
        public static OrderClass Decode(String d1)
        {
            String[]   s       = d1.Split(' ');
            OrderClass object1 = new OrderClass();

            object1.setSenderID(Int32.Parse(s[0]));
            object1.setAmount(Int32.Parse(s[1]));
            object1.setCreditCard(Int32.Parse(s[2]));
            object1.setUnitPrice(Int32.Parse(s[3]));
            return(object1);
        }