コード例 #1
0
        public static int placed = 0, notified = 0; // To count total placed and executed orders

        #endregion Fields

        #region Methods

        static void Main(string[] args)
        {
            mcb.init_bufferCells();

            //2 hotels
            hotelSupplier[0] = new HotelSupplier(0);
            hotelSupplier[1] = new HotelSupplier(1);

            //5 travel agencies
            TravelAgency[] travelAgency = new TravelAgency[5];
            for (int i = 0; i < 5; i++)
            {
                travelAgency[i] = new TravelAgency(i);
            }

            HotelSupplier.priceChange += new priceChangeEvent(travelAgency[0].changeInHotelRoomPrice);
            HotelSupplier.priceChange += new priceChangeEvent(travelAgency[1].changeInHotelRoomPrice);
            HotelSupplier.priceChange += new priceChangeEvent(travelAgency[2].changeInHotelRoomPrice);
            HotelSupplier.priceChange += new priceChangeEvent(travelAgency[3].changeInHotelRoomPrice);
            HotelSupplier.priceChange += new priceChangeEvent(travelAgency[4].changeInHotelRoomPrice);

            HotelSupplier.bStatus += new bookingStatus(travelAgency[0].RoomStatus);
            HotelSupplier.bStatus += new bookingStatus(travelAgency[1].RoomStatus);
            HotelSupplier.bStatus += new bookingStatus(travelAgency[2].RoomStatus);
            HotelSupplier.bStatus += new bookingStatus(travelAgency[3].RoomStatus);
            HotelSupplier.bStatus += new bookingStatus(travelAgency[4].RoomStatus);

            Thread hotelSupplier1T1 = new Thread(hotelSupplier[0].hotelFunction); hotelSupplier1T1.Start();
            Thread hotelSupplier1T2 = new Thread(hotelSupplier[0].priceFunction); hotelSupplier1T2.Start();

            Thread hotelSupplier2T1 = new Thread(hotelSupplier[1].hotelFunction); hotelSupplier2T1.Start();
            Thread hotelSupplier2T2 = new Thread(hotelSupplier[1].priceFunction); hotelSupplier2T2.Start();

            Thread[] t = new Thread[5];
            for (int i = 0; i < 5; i++)
            {
                t[i] = new Thread(travelAgency[i].agency);
                t[i].Start();
            }

            // Wait for every thread to finish
            hotelSupplier1T1.Join();
            hotelSupplier2T1.Join();

            hotelSupplier1T2.Join();
            hotelSupplier2T2.Join();

            for (int i = 0; i < 5; i++)
            {
                t[i].Join();
            }
            Console.WriteLine("-------------------------------------------------------------------------");
            Console.WriteLine("Result:");
            Console.WriteLine("Total Orders placed by Travel Agencies: {0}\nTotal Orders processed succesfully by Hotel Suppliers: {1}", placed, notified);
            Console.WriteLine("-------------------------------------------------------------------------");
            Console.ReadKey();
        }
        public void placeOrderUsingTravelAgencyThread(HotelSupplier hs)
        {
            Random rand = new Random();

            this.amount = rand.Next(1, 500 - hs.Price);

            OrderClass o             = new OrderClass(this.senderId, this.cardNo, hs.ReceiverId, this.amount, hs.Price, DateTime.Now.ToString());
            String     encodedString = Encoder.encode(o, Program.key);

            // put this string into buffer
            Program.mcb.setOneCell(encodedString);
        }
コード例 #3
0
        public void orderProcessing()
        {
            String orderString;

            while (true)
            {
                orderString = Program.mcb.getOneCell();
                OrderClass orderObj = Coder.decoder(orderString);
                Monitor.Enter(roomPrice);
                if (HotelSupplier.checkCreditCardNumber(orderObj.getCardNo()))
                {
                    double amountOfCharge = orderObj.getUnitPrice() * orderObj.getAmt() + taxRate * orderObj.getUnitPrice() * orderObj.getAmt() + locationCharge;
                    orderObj.setReceiveTime(DateTime.Now);
                    if (orderCompleted != null)
                    {
                        orderCompleted(orderObj, amountOfCharge);
                    }
                }
                Monitor.Exit(roomPrice);
            }
        }
コード例 #4
0
        public static void Main(string[] args)
        {
            HotelSupplier supplier1 = new HotelSupplier();
            HotelSupplier supplier2 = new HotelSupplier();

            try
            {
              //  Console.WriteLine("********************************************************\n");
                Console.WriteLine("                       Hotel Block Booking System");
                Console.WriteLine("               *************************************\n");
                Console.WriteLine("Setup: (1) 3 BufferCells  (2) 5 Travel Agency Threads (3) 2 Hotel Supplier Objects");
                Console.WriteLine("***********************************************************************************\n");
                Console.SetBufferSize(80, 800);

                Thread Supplier1 = new Thread(new ThreadStart(supplier1.priceCutFunction));   // Two objects for Hotel Suppliers are instantiated
                Thread Supplier2 = new Thread(new ThreadStart(supplier2.priceCutFunction));
                Supplier1.Name = "HotelSupplier1";   //setting a name to the thread
                Supplier2.Name = "HotelSupplier2";
                supplier1.priceCut += new HotelSupplier.PriceCutEvent(TravelAgency.roomDiscount);  // Event in HotelSupplier class and the event handler in the travel agency class are linked
                supplier2.priceCut += new HotelSupplier.PriceCutEvent(TravelAgency.roomDiscount);
                Thread[] travelAgency = new Thread[5];   //creating 5 retailer threads
                for (int i = 0; i < 5; i++)
                {
                    travelAgency[i] = new Thread(new ThreadStart(TravelAgency.travelFunc));
                    travelAgency[i].Name = "Travel Agency " + (i + 1).ToString();
                    travelAgency[i].IsBackground = false;
                    travelAgency[i].Start();     //startin the thread
                }
                 Supplier1.Start();    //starting the Supplier1 thread
                 Supplier2.Start();   //starting the Supplier2 thead
                  Supplier1.Join();
                  Supplier2.Join();
            }
            catch (Exception e)
            {
                Console.WriteLine("Oops! Error occurred in main method\n" + e.Message);
            }
        }
        public void placeOrder(HotelSupplier hs)
        {
            Thread t = new Thread(() => this.placeOrderUsingTravelAgencyThread(hs));

            t.Start();
        }
コード例 #6
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            // Initialize the Hotel Agencies
            for (int i = 0; i < K; ++i)
            {
                HotelSupplier hotelSupplier = new HotelSupplier();
                hotelSuppliers[i]    = hotelSupplier;
                hotelThreads[i]      = new Thread(hotelSupplier.Run);
                hotelThreads[i].Name = "HotelSupplier_" + i;
                hotelThreads[i].Start();
                while (!hotelThreads[i].IsAlive)
                {
                    ;
                }
            }

            // Initialize the Travel Agencies
            for (int i = 0; i < N; ++i)
            {
                TravelAgency travelAgency = new TravelAgency();

                // Loop through the Hotel Suppliers and Subscribe to the Price Cut event
                for (int j = 0; j < K; ++j)
                {
                    travelAgency.Subscribe(hotelSuppliers[j]);
                }

                agencyThreads[i]      = new Thread(travelAgency.Run);
                agencyThreads[i].Name = "TravelAgency_" + i;
                agencyThreads[i].Start();
                while (!agencyThreads[i].IsAlive)
                {
                    ;
                }
            }

            // Wait for the Hotels to perform P_MAX price cuts
            for (int i = 0; i < K; ++i)
            {
                while (hotelThreads[i].IsAlive)
                {
                    ;
                }
            }

            // Alert the Travel Agencies that the Hotels are no longer active
            for (int i = 0; i < N; ++i)
            {
                TravelAgency.HotelsActive = false;
            }

            // Wait for the Travel Agency to close
            for (int i = 0; i < N; ++i)
            {
                while (agencyThreads[i].IsAlive)
                {
                    ;
                }
            }

            Console.WriteLine("\n\nPROGRAM COMPLETED");

            // Wait for user to hit a button
            Console.WriteLine("HIT ENTER TO QUIT");
            Console.ReadLine();
        }
コード例 #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hotel Booking System");

            hs = new HotelSupplier[hsCount];
            ta = new TravelAgency[taCount];
            Thread[] hst = new Thread[hsCount];

            //Create Hotel Supplier
            for (int i = 0; i < hsCount; i++)
            {
                hs[i] = new HotelSupplier(i + 1);
            }

            //create Travel Agencies
            for (int i = 0; i < taCount; i++)
            {
                ta[i] = new TravelAgency(i + 1, 5000 + i * 2);
            }

            // create MultiCell Buffer
            mcb = new MultiCellBuffer();

            // Register travel agencies for priceCut and orderConfirm Events
            for (int i = 0; i < hsCount; i++)
            {
                for (int j = 0; j < taCount; j++)
                {
                    hs[i].priceCut += new priceCutDelegate(ta[j].placeOrder);
                }
            }

            // Start pricing models for all hotel suppliers and this starts the overall program
            for (int i = 0; i < hsCount; i++)
            {
                hst[i] = new Thread(new ThreadStart(hs[i].PricingModel));
                hst[i].Start();
                l1.Add(hst[i]);
            }

            // Dispatcher Thread:  THis thread is responsible for reading from buffer and deliverying order to Hotel supplier Object
            // Hotel supplier will then spawn new thread to process this order
            Thread dispatcher = new Thread(new ThreadStart(HotelSupplier.receiveOrder));

            dispatcher.Start();
            l1.Add(dispatcher);


            //wait for all threads to complete
            for (int i = 0; i < l1.Count; i++)
            {
                l1[i].Join();
            }

            Console.WriteLine("/////////////////////////// \n Done With Booking \n" +
                              " Main Thread is Stopping \n GrandChildren thread may follow\n" +
                              "///////////////////////////");


            Console.WriteLine();
            Console.WriteLine("/////////////////-------Press Any Key to Exit--------------/////////////");



            Console.Read();
        }
コード例 #8
0
 /// <summary>
 /// Hook the PriceCut event to the CreateBulkOrder method, so a large order will be placed once the event is fired.
 /// </summary>
 /// <param name="hotel">Hotel to subscibe to price cut events</param>
 public void Subscribe(HotelSupplier hotelSupplier)
 {
     Console.WriteLine("SUBSCRIBING: Price Cut Event");
     hotelSupplier.PriceCut += IssueBulkOrder;
 }