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();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            TravelAgency agency = new TravelAgency();


            // start a thread using PricingModel method to calculate the room price, if the current price
            // is lower than the previous one, trigger the priceCut event
            Thread calculateRoomPrice = new Thread(new ThreadStart(TravelAgency.hotel.PricingModel));

            calculateRoomPrice.Start();

            // start a thread using orderProcessing method to receive orders from travel agencys and process
            // the orders, if an order is completed, trigger the orderCompleted event
            Thread order = new Thread(new ThreadStart(TravelAgency.hotel.orderProcessing));

            order.Start();

            // method(event handler) bookingAvailable(..) subscripts priceCut event
            HotelSupplier.priceCut += new priceCutEvent(agency.bookingAvailable);

            // method receiveTime(..) subscripts orderCompleted event
            HotelSupplier.orderCompleted += new orderCompletedEvent(agency.receiveTime);

            // start 10 travel agencys thread
            Thread[] agencys = new Thread[10];
            for (int i = 0; i < 10; i++)
            {
                agencys[i] = new Thread(new ThreadStart(agency.agencyFunc));
                // evaluate agency thread name from 1 to 10
                agencys[i].Name = (i).ToString();
                agencys[i].Start();
            }

            while (true)
            {
                // terminate getOrder thread and all travel agencys thread when calculateRoomPrice terminated
                if (!calculateRoomPrice.IsAlive)
                {
                    order.Abort();
                    for (int i = 0; i < 10; i++)
                    {
                        agencys[i].Abort();
                    }
                }
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            String       path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//for storing output file on current users desktop.
            FileStream   fs   = new FileStream(path + "\\TeamLogicAssgn2Output.txt", FileMode.Create);
            StreamWriter sw   = new StreamWriter(fs);

            Console.WriteLine("Main-> Program Started..Writing Output to File at" + path + "\\TeamLogicAssgn2Output.txt..Takes 2-3 mins, since we have used price cuts as:5 and the generated prices depends on random function..... Please Wait");

            TextWriter tmp = Console.Out;// First, save the standard output.

            Console.SetOut(sw);

            DateTime start = System.DateTime.Now;

            Console.WriteLine("Main-> Program Start Time:{0}", start);
            ArrayList threadlist = new ArrayList();

            Hotel []       hotel = new Hotel[3];        //3 hotel objects
            TravelAgency[] ta    = new TravelAgency[5]; //5 travel agency objects

            hotel[0] = new Hotel(1, 1500, 1200, 15);
            hotel[1] = new Hotel(2, 1600, 1300, 10);
            hotel[2] = new Hotel(3, 1200, 1000, 20);

            for (int i = 0; i < 5; i++)
            {
                ta[i] = new TravelAgency(i + 1, hotel);
            }

            //subscribing Travel Agencies to hotel events
            hotel[0].subscribePriceCut(ta[0]);
            hotel[0].subscribePriceCut(ta[2]);
            hotel[0].subscribePriceCut(ta[3]);

            hotel[1].subscribePriceCut(ta[1]);
            hotel[1].subscribePriceCut(ta[4]);

            hotel[2].subscribePriceCut(ta[3]);
            hotel[2].subscribePriceCut(ta[1]);
            hotel[2].subscribePriceCut(ta[2]);
            hotel[2].subscribePriceCut(ta[4]);


            try
            {
                for (int i = 0; i < 3; i++)
                {
                    Thread t = new Thread(new ThreadStart(hotel[i].runHotel));//starting hotel threads
                    t.Start();
                    threadlist.Add(t);
                    while (!hotel[i].isHotelAlive())
                    {
                        ;
                    }
                }

                for (int i = 0; i < 5; i++)
                {
                    Thread t = new Thread(new ThreadStart(ta[i].agencyRun));//starting Travel Agencies threads
                    t.Start();
                    threadlist.Add(t);
                }

                foreach (Thread item in threadlist)
                {
                    item.Join();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }



            DateTime end = System.DateTime.Now;

            Console.WriteLine("Main-> Total Orders Placed:{0}", TravelAgency.getOrderId());
            Console.WriteLine("Main-> End Time:{0}", end);
            Console.WriteLine("Main-> Total Execution Time:{0}", end - start);
            Console.SetOut(tmp);
            Console.WriteLine("Main->Done..Please check file");
            sw.Close();
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            String path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//for storing output file on current users desktop.
            FileStream fs = new FileStream(path+"\\TeamLogicAssgn2Output.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            Console.WriteLine("Main-> Program Started..Writing Output to File at" +path+"\\TeamLogicAssgn2Output.txt..Takes 2-3 mins, since we have used price cuts as:5 and the generated prices depends on random function..... Please Wait");

            TextWriter tmp = Console.Out;// First, save the standard output.
            Console.SetOut(sw);

            DateTime start = System.DateTime.Now;
            Console.WriteLine("Main-> Program Start Time:{0}",start);
            ArrayList threadlist = new ArrayList();
            Hotel []hotel=new Hotel[3];//3 hotel objects
            TravelAgency[] ta = new TravelAgency[5];//5 travel agency objects

            hotel[0] = new Hotel(1, 1500, 1200, 15);
            hotel[1] = new Hotel(2, 1600, 1300, 10);
            hotel[2] = new Hotel(3, 1200, 1000, 20);

            for (int i = 0; i < 5; i++)
            {
                ta[i] = new TravelAgency(i + 1, hotel);

            }

            //subscribing Travel Agencies to hotel events
            hotel[0].subscribePriceCut(ta[0]);
            hotel[0].subscribePriceCut(ta[2]);
            hotel[0].subscribePriceCut(ta[3]);

            hotel[1].subscribePriceCut(ta[1]);
            hotel[1].subscribePriceCut(ta[4]);

            hotel[2].subscribePriceCut(ta[3]);
            hotel[2].subscribePriceCut(ta[1]);
            hotel[2].subscribePriceCut(ta[2]);
            hotel[2].subscribePriceCut(ta[4]);

            try
            {
                for (int i = 0; i < 3; i++)
                {
                    Thread t = new Thread(new ThreadStart(hotel[i].runHotel));//starting hotel threads
                    t.Start();
                    threadlist.Add(t);
                    while (!hotel[i].isHotelAlive()) ;

                }

                for (int i = 0; i < 5; i++)
                {
                    Thread t = new Thread(new ThreadStart(ta[i].agencyRun));//starting Travel Agencies threads
                    t.Start();
                    threadlist.Add(t);

                }

                foreach (Thread item in threadlist)
                {
                    item.Join();
                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
            }

            DateTime end = System.DateTime.Now;
            Console.WriteLine("Main-> Total Orders Placed:{0}",TravelAgency.getOrderId());
            Console.WriteLine("Main-> End Time:{0}", end);
            Console.WriteLine("Main-> Total Execution Time:{0}", end-start);
            Console.SetOut(tmp);
            Console.WriteLine("Main->Done..Please check file");
            sw.Close();
        }
Esempio n. 5
0
 //calls the price cut event handler
 public void subscribePriceCut(TravelAgency agency)
 {
     priceCutEvent += new priceCutDelegate(agency.priceCut);
     Console.WriteLine("Hotel  {1}-> Agency {0} has been subscribed for pricecut Event", agency.getAgencyId(), hotelId);
 }
Esempio n. 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();
        }
Esempio n. 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();
        }
Esempio n. 8
0
 //calls the price cut event handler
 public void subscribePriceCut(TravelAgency agency)
 {
     priceCutEvent += new priceCutDelegate(agency.priceCut);
     Console.WriteLine("Hotel  {1}-> Agency {0} has been subscribed for pricecut Event", agency.getAgencyId(), hotelId);
 }