Exemple #1
0
            public static Int32 reevaluatePrice()
            {
                Int32 newPrice = 5000 / ChickenFarm.getNumChickens();

                if (newPrice < 1)
                {
                    newPrice++;
                }
                return(newPrice);
            }
Exemple #2
0
        public void farmerFunc()
        {
            // Timestamp used to mark when the thread begins.
            DateTime startTime = new DateTime();

            startTime = DateTime.Now;

            System.Timers.Timer eventTimer = new System.Timers.Timer();
            eventTimer.Elapsed  += new ElapsedEventHandler(recountChickens);
            eventTimer.Interval  = 50;
            eventTimer.AutoReset = true;
            eventTimer.Start();

            while (priceCutCounter < 10)
            {
                Thread.Sleep(100);

                Console.Out.WriteLine("ChickenFarm has {0} chickens left", numChickens);
                newPrice = PricingModel.reevaluatePrice();
                ChickenFarm.changePrice(newPrice);

                lock (Program.orderBuffer)
                {
                    if (Program.orderBuffer.IsFull())
                    {
                        orderProcessingObject = new OrderProcessing();
                        for (int i = 0; i < Program.NUM_RETAILERS - 1; i++)
                        {
                            String s = Program.orderBuffer.Consume(i);
                            if (s != null)
                            {
                                Order cfOrder = Decoder.decode(s);
                                orderProcessingObject.setOrder(cfOrder);
                                Thread orderProcessingThread = new Thread(new ThreadStart(orderProcessingObject.orderProcessing));
                                orderProcessingThread.Start();
                            }
                        }
                    }
                }
            }

            // Timestamp used to mark when the thread ends.
            DateTime endTime = new DateTime();

            endTime = DateTime.Now;
            // Time span between start time and end time (thread duration)
            TimeSpan span = endTime.Subtract(startTime);

            System.Console.WriteLine("Total Elapsed Time for ChickenFarm Thread: {0} ms", span.TotalMilliseconds);
            System.Console.WriteLine("ChickenFarm DIES! :(");

            // do not wait for OrderProcessing threads to terminate! This is a poor design
            // choice if this software was implemented in real life, but for the sake of
            // finishing the assignment on time, we will not deal with it.
        }
        static void Main(string[] args)
        {
            Random randNum      = new Random();
            bool   childIsAlive = true;

            farmRamRod  = new ChickenFarm();
            orderBuffer = new OrderBuffer();
            farmer      = new Thread(new ThreadStart(farmRamRod.farmerFunc));
            farmer.Start();         // Start one farmer thread
            chickenstore          = new Retailer(-1);
            ChickenFarm.priceCut += new priceCutEvent(chickenstore.chickenOnSale);
            Thread[] retailers = new Thread[NUM_RETAILERS];
            for (int i = 0; i < NUM_RETAILERS; i++)
            {   // Start N retailer threads
                retailers[i]      = new Thread(new ThreadStart(new Retailer(i).retailerFunc));
                retailers[i].Name = (i + 1).ToString();
                retailers[i].Start();
            }

            // wait until the retailer threads stop before ending
            // this implies the ChickenFarm thread is still running
            // because the retailer threads run until the CF ends
            while (childIsAlive)
            {
                Thread.Sleep(randNum.Next(1, 100));

                childIsAlive = false;
                for (int i = 0; i < retailers.Length; i++)
                {
                    if (retailers[i].IsAlive)
                    {
                        childIsAlive = true;
                    }
                }
            }
        }
Exemple #4
0
        public void orderProcessing()
        {
            float tax, shipping;
            Int32 subtotal, total;

            if (pOrder.getAmount() <= (ChickenFarm.getNumChickens() - 2))
            {
                if ((pOrder.getCardNum() > 999) && (pOrder.getCardNum() < 10000))
                {
                    subtotal = pOrder.getUnitPrice() * pOrder.getAmount();
                    tax      = subtotal * TAXRATE;
                    shipping = pOrder.getAmount() * SHIPPINGRATE;
                    total    = (Int32)(subtotal + tax + shipping);
                    ChickenFarm.setNumChickens(ChickenFarm.getNumChickens() - pOrder.getAmount());

                    // Order finished timestamp
                    DateTime endTime = DateTime.Now.ToUniversalTime();
                    TimeSpan span    = endTime.Subtract(pOrder.getStartTime());

                    //send receipt (output to console)
                    Console.Out.WriteLine("Retailer {0} bought {1} chickens for a total of ${2} at price {3} in {4}ms!", pOrder.getThreadId(), pOrder.getAmount(), total, pOrder.getUnitPrice(), span.TotalMilliseconds);
                }
            }
        }