Ejemplo n.º 1
0
        public void OrderProcessingFunc()
        {
            // request = "-1";
            try
            {
                //  while (request.Equals("-1"))                  // Take the order form the queue
                Monitor.Enter(Mainapp.Buffer);                    // Acquire lock on Buffer object
                request = Mainapp.Buffer.getOneCell();            // Get the order in form of the string
            }
            finally { Monitor.Exit(Mainapp.Buffer); }             // Release lock on Buffer object
            OrderClass order = Decoder.Decode(request);           // Convert string to object of type OrderClass

            Tax            = r.Next(1, 10);
            LocationCharge = r.Next(10, 150);
            if (order.getCreditCard() >= 30000 && order.getCreditCard() <= 70000)        // Check whether credit card id valid
            {
                Thread.Sleep(r.Next(1, 3000));
                Console.WriteLine();
                Console.WriteLine("-----------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine("Order from the Dealer " + order.getSenderID() + " has been processed");
                Int32 price        = order.getUnitPrice() * order.getAmount();                                                                                                                                                                           // Calculate the final amount
                float final_amount = price + (float)Tax / 100 * price + LocationCharge;
                Console.WriteLine("Total number of cars:" + order.getAmount() + "  Unit Price:" + order.getUnitPrice() + " $k" + "  Tax: " + (float)Tax / 100 * price + " LocationCharge: " + LocationCharge + " Total amount:" + final_amount + " $k"); // Print the final amount and order detailss
                Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------");
            }
            else
            {
                Console.WriteLine("Credit card details are incorrect");
            }
        }
Ejemplo n.º 2
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
     }
 }
Ejemplo n.º 3
0
        // Encode function to convert object of type OrderClass to String by adding space as delimiter
        public static String Encode(OrderClass e)
        {
            String s = Convert.ToString(e.getSenderID()) + " " + Convert.ToString(e.getAmount()) + " " + Convert.ToString(e.getCreditCard()) + " " + Convert.ToString(e.getUnitPrice());;

            return(s);
        }