static void Main(string[] args)
        {
            //Create a chicken farm object
            ChickenFarmClass chickenfarm = new ChickenFarmClass();

            //start the chicken farm
            ChickenFarmThread = new Thread(new ThreadStart(chickenfarm.StartChickenFarm));
            ChickenFarmThread.Start();

            //Create a Retailer Object
            Retailer retailer = new Retailer();

            //Attaching the event to its corresponding handlers
            ChickenFarmClass.pricecut       += new PriceCut(retailer.priceCutEventHandler);
            Retailer.placeorder             += new PlaceOrderEvent(chickenfarm.OrderProcessing);
            ChickenFarmClass.OrderCompleted += new OrderComplete(chickenfarm.OrderCompletedEvent);
            //start the retailers
            Retailers = new Thread[5];
            for (int i = 0; i < 5; i++)
            {
                Retailers[i] = new Thread(new ThreadStart(retailer.retailerthread));
                Retailers[i].Start();
            }

            // Hold the main thread until the Farm thread has completed execution.
            ChickenFarmThread.Join();

            Console.WriteLine("Orders Limit Have Been Reached. Chicken farm cannot take anymore orders now");
            Console.WriteLine("Press Y to exit");

            Console.ReadLine();
        }
Beispiel #2
0
        //This will initate the functioning of chicken farm
        public void StartChickenFarm()
        {
            Random rand = new Random();

            //to keep the count of number of retailer
            int RetailerCounter = 0;

            //Get retailers
            int[] RetailerID = GetRetailerIDs();
            while (PriceCounter < MaxOrders)
            {
                Thread.Sleep(1000);
                //any number between 150 to 249 as original price is set to 200
                int ChangedPrice = rand.Next(150, 250);
                ChickenFarmClass.SendoutNewPrice(ChangedPrice, RetailerID[RetailerCounter]);
                RetailerCounter += 1;
                //as number of order is greater than number of retailers so we rand
                if (RetailerCounter == 5)
                {
                    RetailerCounter = rand.Next(0, 5);
                }
            }
        }