// Travel agency's thread method
        public void runTravelAgency()
        {
            Console.Write("\n {0} is inside runTravelAgency() ", Thread.CurrentThread.Name);

            // declaring some variables that would be local to each thread
            DateTime orderTimeStamp;

            for (int i = 0; i < 70; i++)
            {
                Thread.Sleep(800);

                // travel agency is event driven, hence checking if an event occurred only then proceeding with creating order
                if (hasPriceCutEventHappened)
                {
                    OrderClass orderObject = new OrderClass();
                    orderObject.SenderId   = Thread.CurrentThread.Name;
                    orderObject.CardNo     = cardNo;
                    orderObject.ReceiverID = receiverId;
                    orderObject.NumTickets = numOfTickets;
                    orderObject.UnitPrice  = ticketUnitPrice;

                    string orderString = EncoderDecoderClass.encodeOrderObject(orderObject);
                    Console.WriteLine(" {0} built order: {1}", Thread.CurrentThread.Name, orderString);

                    // saving timestamp of the order before sending it to buffer
                    orderTimeStamp = DateTime.Now;

                    // travel agency sends the encoded string to one of the free cells in buffer
                    buffer.addToBuffer(orderString);

                    // setting the flag as false as the order for this event has been added to buffer
                    hasPriceCutEventHappened = false;
                }
            }
        }
Exemple #2
0
        // thread method of airline class
        public void runAirline()
        {
            Console.Write(" {0} is inside runAirline() \n", Thread.CurrentThread.Name);

            // declaring price cut counter for each airline thread
            int    priceCutCounter     = 0;
            double baseTicketUnitPrice = 0;

            for (int i = 0; i < 70; i++)
            {
                Thread.Sleep(300);

                // setting ticket price for all airline threads
                if (Thread.CurrentThread.Name.Equals("Airline[1]"))
                {
                    baseTicketUnitPrice = Airline_1_Price;
                }
                if (Thread.CurrentThread.Name.Equals("Airline[2]"))
                {
                    baseTicketUnitPrice = Airline_2_Price;
                }
                if (Thread.CurrentThread.Name.Equals("Airline[3]"))
                {
                    baseTicketUnitPrice = Airline_3_Price;
                }

                // fetching new price from pricing model for each airline thread
                double newPrice = getPriceFromPricingModel();

                Console.WriteLine(" {0} offered New Price is {1} ", Thread.CurrentThread.Name, newPrice);

                // calling change price method that will change the ticket price and emit price cut event in case of price drop
                priceCutCounter = changePrice(baseTicketUnitPrice, newPrice, Thread.CurrentThread.Name, priceCutCounter);

                // after 20 price cuts an airline thread will terminate
                if (priceCutCounter < 20)
                {
                    OrderClass orderObject = null;

                    // removing a order cell from buffer by sending the current thread's name
                    string orderString = buffer.removeFromBuffer(Thread.CurrentThread.Name);

                    // checking if the orderString received from buffer is not null and then proceeding with order processing
                    if (orderString != null)
                    {
                        Console.WriteLine(" {0} has removed order: {1}", Thread.CurrentThread.Name, orderString);

                        orderObject = EncoderDecoderClass.decodeOrderObject(orderString);

                        // sending the order object for order processing
                        OrderProcessingClass orderProc = new OrderProcessingClass(orderObject);
                        Thread orderProcessThread      = new Thread(new ThreadStart(orderProc.runOrderProcessing));
                        orderProcessThread.Start();
                    }
                }
                else
                {
                    Console.WriteLine(" Stopping {0} because it's price counter reached {1}", Thread.CurrentThread.Name, priceCutCounter);
                    break; //  the airline thread exits when it completes 20 price cuts
                }
            }
        }