예제 #1
0
        static void Main(string[] args)

        {
            Car car1 = new Car(1, 40.4, 300, "B", "benzin", "Red", 123, "NS-999-MM", 3, 50, "mehanicka", "ford", rnd.Next(1000, 9000));
            Car car2 = new Car(2, 44.4, 340, "B", "benzin", "Blue", 321, "NS-000-MM", 5, 20, "mehanicka", "audi", rnd.Next(1000, 9000));

            cars.Add(car1);
            cars.Add(car2);

            Tractor tractor1 = new Tractor(3, 50, 700, "F", "benzin", "red", 777, 78.8, 7, "dvoosovinski");
            Tractor tractor2 = new Tractor(4, 70, 1200, "F", "benzin", "green", 888, 98.8, 9, "dvoosovinski");

            tractors.Push(tractor1);
            tractors.Push(tractor2);

            Truck truck1 = new Truck(5, 100, 2000, "C", "benzin", "grey", 987, 1500, 40, 3);
            Truck truck2 = new Truck(6, 122, 2222, "C", "benzin", "white", 851, 1800, 50, 5);

            trucks.Enqueue(truck1);
            trucks.Enqueue(truck2);

            car2.ChangeColor("Green", rnd.Next(1000, 9000)); //Car color change

            Tick();
            Console.Clear();

            //Creating and starting threads
            for (int i = 0; i < cars.Count; i++)
            {
                int    temp = i;
                Thread t    = new Thread(() => Preparation(cars[temp]));
                t.Start(); t.Join();
            }
            Car golf = new Car(1, 40.4, 300, "B", "benzin", "Orange", 123, "NS-999-MM", 3, 30, "mehanicka", "golf", rnd.Next(1000, 9000));

            cars.Add(golf);
            Console.WriteLine("\nJoins orange golf");

            //Creating and starting threads
            for (int i = 0; i < cars.Count; i++)
            {
                int    temp = i;
                Thread t    = new Thread(() => cars[temp].CarRace(cars[temp]));
                t.Start();
            }

            Thread semaphore = new Thread(() => Car.TrafficLight()); //Creating thread

            semaphore.Start();                                       // Start thread
            Console.ReadLine();
        }
예제 #2
0
        static void Main(string[] args)
        {
            Truck truck1 = new Truck();
            Truck truck2 = new Truck();

            Tractor tractor1 = new Tractor();
            Tractor tractor2 = new Tractor();

            Car car1 = new Car();

            car1.RegNumber = "NS123AB";
            Car car2 = new Car();

            car2.RegNumber = "BG987BC";

            List <Truck> trucks = new List <Truck> {
                truck1, truck2
            };
            List <Tractor> tractors = new List <Tractor> {
                tractor1, tractor2
            };
            List <Car> cars = new List <Car> {
                car1, car2
            };

            Stopwatch s = new Stopwatch();

            s.Start();
            for (int i = 0; i < 5; i++)
            {
                Thread.Sleep(1000);
                Console.WriteLine(s.ElapsedMilliseconds / 1000);
            }
            s.Stop();


            foreach (var car in cars)
            {
                car.SetToStart();
                car.RemainingFuel            = 100;
                car.fuelConsumptionPerSecond = r.Next(6, 9);
            }
            Car car3 = new Car();

            car3.Color                    = "orange";
            car3.Manufacturer             = "Golf";
            car3.RegNumber                = "NI456CD";
            car3.fuelConsumptionPerSecond = r.Next(6, 9);
            car3.RemainingFuel            = 100;
            car3.SetToStart();

            Thread t1        = new Thread(() => car1.ToRace());
            Thread t2        = new Thread(() => car2.ToRace());
            Thread t3        = new Thread(() => car3.ToRace());
            Thread semaphore = new Thread(() => Car.Semaphore());

            semaphore.Start();
            t1.Start();
            t2.Start();
            t3.Start();

            Console.ReadLine();
        }
예제 #3
0
        static void Main(string[] args)
        {
            //create vecihles
            Car car1 = new Car("Blue");
            Car car2 = new Car("Red");

            car1.Registration = "000-PD-XX";
            car2.Registration = "111-PD-YY";
            Truck   truck1   = new Truck("White");
            Truck   truck2   = new Truck("Black");
            Tractor tractor1 = new Tractor("Green");
            Tractor tractor2 = new Tractor("Yellow");

            //create collections and add vehicles to them
            List <MotorVecihle> cars     = new List <MotorVecihle>();
            List <MotorVecihle> trucks   = new List <MotorVecihle>();
            List <MotorVecihle> tractors = new List <MotorVecihle>();

            cars.Add(car1);
            cars.Add(car2);
            trucks.Add(truck1);
            trucks.Add(truck2);
            tractors.Add(tractor1);
            tractors.Add(tractor2);

            //simulate 5 second countdown
            for (int i = 5; i > 0; i--)
            {
                Console.WriteLine(i);
                Thread.Sleep(1000);
            }
            //every car from the list approached the start
            foreach (Car item in cars)
            {
                item.ApproachStart();
                //initialize full gas tank and consumption per second
                item.FuelLeft          = 100;
                item.FuelSpentBySecond = rnd.Next(4, 10);
            }
            //creating orange golf =>by specification in task text
            Car car3 = new Car("Orange");

            car3.Manufacturer      = "Golf";
            car3.Registration      = "222-PD-ZZ";
            car3.FuelLeft          = 100;
            car3.FuelSpentBySecond = rnd.Next(4, 10);
            car3.ApproachStart();

            //start threads that represent cars racing and start thread that represents semaphore
            Thread t1        = new Thread(() => car1.RaceMethod());
            Thread t2        = new Thread(() => car2.RaceMethod());
            Thread t3        = new Thread(() => car3.RaceMethod());
            Thread Green_Red = new Thread(() => Car.Sempaphore());

            Green_Red.Start();
            t1.Start();
            t2.Start();
            t3.Start();

            Console.ReadLine();
        }
예제 #4
0
        static void Main(string[] args)
        {
            //Stopwatch starts at the begining of program.
            s.Start();

            //Collection userd for Car objects.
            List <Car> Cars = new List <Car>();

            Car car1 = new Car("Red", "NS", 111);
            Car car2 = new Car("Red", "BG", 222);

            Cars.Add(car1);
            Cars.Add(car2);

            //Collection userd for Truck objects
            Queue <Truck> Trucks = new Queue <Truck>();

            Truck truck1 = new Truck("Red", "PA", 333);
            Truck truck2 = new Truck("Blue", "SU", 444);

            Trucks.Enqueue(truck1);
            Trucks.Enqueue(truck2);

            //Collection userd for Tractor objects.
            Stack <Tractor> Tractors = new Stack <Tractor>();

            Tractor tractor1 = new Tractor("White", "KR", 555);
            Tractor tractor2 = new Tractor("Red", "ZR", 666);

            Tractors.Push(tractor1);
            Tractors.Push(tractor2);

            //Vehicles are waiting at the start line, after being added to the coresponding lists.
            Console.WriteLine("Vehicles are ready for the race.");
            Console.WriteLine("--------------------------------");

            //When 5 seconds pass, Orange "Golf" is joining and race begins.
            while (s.ElapsedMilliseconds != 5000)
            {
            }
            //Stopwatch is reseted, to properly caculate race time.
            s.Stop();
            s.Reset();

            Car car3 = new Car("Orange", "NI", 777);

            Cars.Add(car3);

            Console.WriteLine("Golf ({0}) has joined, and the race begins!", car3.Paint);
            Console.WriteLine("----------------------------------------------");

            //Stopwatch starts to measure time once more.
            s.Start();

            //Each vehicle initiates its corresponding thread.
            foreach (Car car in Cars)
            {
                car.Start();
            }

            foreach (Truck truck in Trucks)
            {
                truck.Start();
            }

            foreach (Tractor tractor in Tractors)
            {
                tractor.Start();
            }

            //Countdown event waits for 7 threads to signal their completion.
            c.Wait();

            Console.ResetColor();

            //Only vehicles with Red paint are added to "finish" ArrayList collection and first one added is displayed on the console.
            if (finish.Count != 0)
            {
                Console.WriteLine();
                Console.WriteLine("Fastest red vehicle was: {0}", finish[0]);
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("Not one red vehicle has managed to finish the race.");
            }

            Console.ReadLine();
        }