コード例 #1
0
        // EVENT HANDLERS
        /// <summary>
        ///
        /// Event handler for  Book Sale broadcasted by publisher thread
        ///
        /// </summary>
        /// <param name="price"></param>
        /// <param name="PubNum"></param>
        public void BookSale(double price, Int32 PubNum)
        {
            // books are on sale for a price.
            // see if I want to buy at that price
            OrderObject newOrder = CreateOrder(PubNum);

            if (StoreNumber == 0)  // Every store will get this event but I only need noe store to write it out!!
            {
                Console.WriteLine(" *****************  BOOK SALE from Publisher # {0}  Price{1:C}    *******************\n", PubNum, price);
            }

            if (newOrder != null)
            {
                String eOrder = Encoder(newOrder);
                // add to multicell buffer
                BufferString mybString = new BufferString();
                mybString.setBufferString(eOrder, PubNum);
                Program.mcb.setOneCell(mybString); // order the books.
                Console.WriteLine("\t\t ^^^  order created  for store {0} for {1} books", StoreNumber, newOrder.getAmount());
            }

            else
            {
                Console.WriteLine("\t\t *** No order created  for store {0}", StoreNumber);
            }
        }
コード例 #2
0
        // functional thread for the bookstore.
        // THis thread will generate demand, check price, and create purchase orders if necessary.
        /// <summary>
        /// Functional method for thread.
        /// Sell Books  -- This will create demand.  The amount of books is random
        /// The process in this thread is to get the price from the publishers (use the global varialbes)
        /// Pick the lowest price
        /// See if we need to purchase books
        /// create and order
        /// Encode the order
        /// Push onto the multicell buffer
        /// Repeat...
        /// </summary>
        public void BookStoreFunc()
        {
            while (Program.BookStoreThreadRunning)
            {
                //Console.WriteLine("\n\n");
                OrderObject newOrder;
                int         PubNum = 0;
                //CurrentPrice = pub1.getPrice();

                SellBooks(); // create demand


                // check for lowest price
                double p1 = Program.GV.get_Pub1_Price();
                double P2 = Program.GV.get_Pub2_Price();
                //Console.WriteLine(" pub1 price : {0}, pub2 price : {1}", p1, P2);
                if (p1 < P2)
                {
                    PubNum       = 1;
                    CurrentPrice = p1;
                }
                else
                {
                    PubNum       = 2;
                    CurrentPrice = P2;
                }

                // using the lowest price, create an order.  This method can return NULL if no books are needed.
                newOrder = CreateOrder(PubNum);

                if (newOrder != null)  // See if we need to move forward with the order for the store.
                {
                    // encode the order object
                    String eOrder = Encoder(newOrder);
                    // add to multicell buffer
                    BufferString mybString = new BufferString();
                    mybString.setBufferString(eOrder, PubNum);
                    Program.mcb.setOneCell(mybString);
                    Console.WriteLine("\t\t ^^^  order created  for store {0} for {1} books", StoreNumber, newOrder.getAmount());
                }

                else
                {
                    Console.WriteLine("\t\t *** No order created  for store {0}", StoreNumber);
                }
                //}
                LastPrice = CurrentPrice; // set the last known price
                Thread.Sleep(1000);       // sleep for 2 seconds
            }

            Console.WriteLine("@@@@   BookStoreFunc threaded exited gracefully...Thread Name {0}", Thread.CurrentThread.Name);
        }