コード例 #1
0
        //function on the server side to get encoded order from the buffer
        public void OrderProcessingThread(MultiCellBuffer buffer)
        {
            while (priceCutCounter < 10)
            {
                //if the buffer is not empty, takes out an order
                while (Program.buffer.counter != 0)
                {
                    if (Program.buffer.counter != 0)
                    {
                        string orderFromBuffer = buffer.getOneCell();
                        //send the order to decoder
                        DecodeOrder(orderFromBuffer);
                    }
                }
                //change the price
                PricingModel();
            }

            //set price to -1 when meet the stopping condition
            if (priceCutCounter == 10)
            {
                chickenPrice = -1;
            }

            //confirm the remaining orders in the buffer, these orders are created before the last price cut
            while (Program.buffer.counter != 0)
            {
                if (Program.buffer.counter != 0)
                {
                    string orderFromBuffer = Program.buffer.getOneCell();
                    DecodeOrder(orderFromBuffer);
                }
            }
        }
コード例 #2
0
        //The function to start Retailer threads
        public void RetailerThread(MultiCellBuffer buffer)
        {
            ChickenFarm chicken = new ChickenFarm();

            //using loop to keep create the orders
            while (chicken.GetPrice() != -1)
            {
                Thread.Sleep(2000);
                //double check the price to avoid something happened during the sleep period
                if (chicken.GetPrice() != -1)
                {
                    //set random order properties
                    long cardNo = rand.Next(5000, 7000);
                    int  amount = rand.Next(1, 5);

                    //add the properties to the object
                    OrderClass newOrder = new OrderClass()
                    {
                        SenderID  = Thread.CurrentThread.Name,
                        CardNo    = cardNo,
                        Amount    = amount,
                        UnitPrice = currPrice
                    };

                    //print out order created message
                    Console.WriteLine("---- New Order Created ----\n" +
                                      "Sender ID: Store {0}\n" +
                                      "Card Number: {1}\n" +
                                      "Amount of Chickens Ordered: {2}\n" +
                                      "Current Unit Price: {3}\n" +
                                      "Order Received Time: {4}\n" +
                                      "=========================================\n",
                                      newOrder.SenderID, newOrder.CardNo, newOrder.Amount, newOrder.UnitPrice, DateTime.Now);
                    //encode the OrderClass object to string
                    EncodeOrder(newOrder);
                }
            }
        }