Ejemplo n.º 1
0
        public OrderObject getBuffer()
        {
            read.WaitOne();
            OrderObject order = null;

            lock (bufferOrders)
            {
                while (count <= 0)          //Wait while there are no orders in the buffer.
                {
                    Monitor.Wait(bufferOrders);
                }
                for (int i = 0; i < N; i++)         //Find the order that is not null in the buffer and return it for order processing
                {
                    if (bufferOrders[i] != null)
                    {
                        order           = bufferOrders[i]; //Get the order and set that index in the buffer equal to null
                        bufferOrders[i] = null;
                        count--;
                        break;
                    }
                }
                read.Release();              //Release the semaphore
                Monitor.Pulse(bufferOrders); //Notify other threads
            }

            return(order);
        }
Ejemplo n.º 2
0
        public void Order(string customerId)
        {
            //This causes the orders to be processed asynchronously.
            //Commenting out the Thread.Sleep function will cause orders to be evaluated in order and instantly
            Console.WriteLine("ORDER PLACED FOR CUSTOMER #{0} \n\n", customerId);
            Thread.Sleep(rand.Next(500, 1000));
            OrderObject order  = Program.orderBuffer.getBuffer();       //Get the order from the buffer and start an order processing thread.
            Thread      thread = new Thread(() => OrderProcessing.processOrderFunc(order));

            thread.Start();
        }
Ejemplo n.º 3
0
 public void orderProcessed(OrderObject order)
 {
     // write to console all details of the order that has processed.
     Console.WriteLine("\t\t\t##########   ORDER PROCESSED FOR CUSTOMER #{0}   ###########", order.getRecieverId());
     Console.WriteLine("\t\t\tTravel Agency {0} has processed the order for customer {1}. \n\t\t\t\tTicket Price = ${2}.\n\t\t\t\tAmount Ordered = {3}. \n\t\t\t\tTotal Cost = ${4}",
                       order.getSenderId(),
                       order.getRecieverId(),
                       order.getUnitPrice(),
                       order.getAmount(),
                       (order.getAmount() * order.getUnitPrice()));
     Console.WriteLine("\t\t\t###########################################################\n\n");
 }
Ejemplo n.º 4
0
        public static void processOrderFunc(OrderObject order)
        {
            //Compute the total cost of the purchase.
            int totalCost = order.getUnitPrice() * order.getAmount();

            //Check to make sure card is valid. The program is designed to generate invalid card numbers to show that error checking is working
            int cardNo = order.getCardNo();

            if (cardNo < 1500 || cardNo > 9500)
            {
                //If card is invalid, then display message. Also trigger the orderFailed event.
                Console.WriteLine("ERROR. INVALID CARD NO. {0}.\n PLEASE USE A DIFFERENT CARD\n", cardNo);
                orderFailed(cardNo);
                return;
            }
            else
            {
                orderProcessed(order);          //Trigger the orderProcessed event if the card number is correct.
            }
        }
Ejemplo n.º 5
0
        public void createOrderObject(string threadId, int price, string receiver)  //Create an order object
        {
            //Order object needs the following:
            //senderId
            //cardNo
            //receiverId
            //amount
            //unit price

            string senderId   = threadId;              //SenderId is the thread name of the travel agency
            int    cardNo     = rand.Next(1000, 9999); //creates a random card number. If the card # is between 2000 and 9000 it is valid.
            string receiverId = receiver;              //ReceiverId customer number or the ordercount number that is initialized in Program.cs
            int    amount     = rand.Next(1, 10);      //Generates a random number of tickets the buyer will buy.
            int    unitPrice  = price;                 //Unit price is the current ticket price

            //generate an order object
            OrderObject order = new OrderObject(senderId, cardNo, receiverId, amount, unitPrice);

            //take the order object and put it in the multi cell buffer
            Program.orderBuffer.setBuffer(order);
            newOrderCreated(order.getRecieverId());                      //Trigger the order created event so that processing can begin
        }
Ejemplo n.º 6
0
 //setBuffer takes an order and adds it to the buffer.
 public void setBuffer(OrderObject order)
 {
     write.WaitOne();
     lock (bufferOrders)
     {
         while (count >= N)      //Wait until there is a spot available in the buffer.
         {
             Monitor.Wait(bufferOrders);
         }
         for (int i = 0; i < N; i++)
         {
             if (bufferOrders[i] == null)        //Find the empty spot in the buffer and assign it to the order
             {
                 bufferOrders[i] = order;
                 count++;
                 break;
             }
         }
         write.Release();             //Release the semaphore
         Monitor.Pulse(bufferOrders); //Notify other threads
     }
 }