Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            List <IPerson> personnel = new List <IPerson>();
            int            numberOfClearanceLevels;
            List <Floor>   floors = new List <Floor>();

            AddFloors(floors);
            numberOfClearanceLevels = floors.Count + 1;
            Elevator elevator       = new Elevator();
            Control  control        = new Control(elevator, floors[0]);
            int      numberOfSpawns = 20;

            Console.WriteLine($"Spawning {numberOfSpawns} people");
            //Thread t = new Thread(() => { InitiateSpawning(floors, personnel, numberOfClearanceLevels, numberOfSpawns); });
            //t.Start();
            new Thread(() => { InitiateSpawning(floors, personnel, numberOfClearanceLevels, numberOfSpawns); }).Start();

            bool runElevator = true;

            while (runElevator)
            {
                // Putting this sleep in here to avoid elevator checking empty personnel list without pause
                if (personnel.Count == 0)
                {
                    Thread.Sleep(3000);
                }

                // Using ToList() here to create a new list to iterate through for each loop, which avoids "list has been modified" exception
                foreach (IPerson person in personnel.ToList())
                {
                    if (person.SpawnFloor.Personnel.Count > 0 && person == person.SpawnFloor.Personnel[0])
                    {
                        person.CallElevator(elevator);
                        person.SpawnFloor.scanner.ScanPerson(person);
                    }
                }

                control.elevator.ShowElevatorQueue();
                Floor floorThatCalled = control.elevator.MoveToNextFloorInQueue();

                if (floorThatCalled != null)
                {
                    IPerson caller = floorThatCalled.Personnel[0];
                    if (caller.HasCalledElevator && elevator.CurrentFloor == caller.SpawnFloor)
                    {
                        caller.EnterElevator(elevator);
                    }

                    control.RetrieveScanResult(floorThatCalled);
                }

                CheckFloorsForPersonnel(floors);

                // Count personnel left waiting for elevator
                int amountLeft = 0;
                foreach (var floor in floors)
                {
                    amountLeft += floor.Personnel.Count;
                }

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Personnel waiting: " + amountLeft);
                control.elevator.ShowElevatorQueue();

                if (amountLeft == 0 && personnel.Count == numberOfSpawns)
                {
                    runElevator = false;
                }

                Console.WriteLine("--------------- Next loop ---------------");
                Console.ResetColor();
            }

            Console.ForegroundColor = ConsoleColor.Black;
            Console.BackgroundColor = ConsoleColor.White;
            Console.WriteLine($"{personnel.Count} has been spawned, which corresponds to the {numberOfSpawns} number of spawns set up for testing.");
            Console.WriteLine($"Testing of Area 51 elevator security system completed.");
            Console.ResetColor();

            #region Test SpawnedPersonnel

            /*
             * // Create random personnel
             * //SpawnPersonOnRandomFloor(1, floors, numberOfClearanceLevels);
             * int numberofSpawns = 20;
             * for (int i = 0; i < numberofSpawns; i++)
             * {
             *  personnel.Add(SpawnPersonOnRandomFloor(i, floors, numberOfClearanceLevels));
             * }
             *
             * while (true)
             * {
             *  foreach (IPerson person in personnel)
             *  {
             *      // Check if there's af person on floor and the person is first in line
             *      if (person.SpawnFloor.Personnel.Count > 0 && person == person.SpawnFloor.Personnel[0])
             *      {
             *          person.CallElevator(elevator);
             *          person.SpawnFloor.scanner.ScanPerson(person);
             *      }
             *  }
             *
             *  elevator.ShowElevatorQueue();
             *  Floor floorThatCalled = elevator.MoveToNextFloorInQueue();
             *
             *  if (floorThatCalled != null)
             *  {
             *      IPerson caller = floorThatCalled.Personnel[0];
             *      // Make sure that the elevator is in the correct floor
             *      if (caller.HasCalledElevator && elevator.CurrentFloor == caller.SpawnFloor)
             *      {
             *          caller.EnterElevator(elevator);
             *      }
             *
             *      control.RetrieveScanResult(floorThatCalled);
             *  }
             *
             *  // Check floors after elevator movement
             *  CheckFloorsForPersonnel(floors);
             *
             *  // Count personnel left waiting for elevator
             *  int amountLeft = 0;
             *  foreach (var floor in floors)
             *  {
             *      amountLeft += floor.Personnel.Count;
             *  }
             *
             *  // info
             *  Console.ForegroundColor = ConsoleColor.Green;
             *  Console.WriteLine("Personnel waiting: " + amountLeft);
             *  elevator.ShowElevatorQueue();
             *  Console.WriteLine("---------------Next loop---------------press enter");
             *  Console.ResetColor();
             *  Console.ReadKey();
             *
             * }
             */
            #endregion

            #region Test individual types of personnel

            // Create personnel with clearance
            //Personnel spawnedPerson = new Personnel(numberOfClearanceLevels, floors[3], floors[0], 0);
            //floors[0].Personnel.Add(spawnedPerson);

            // Create personnel with insufficient clearance to target floor, and spawned floor is default floor
            //Personnel spawnedPerson = new Personnel(1, floors[3], floors[0], 0);
            //floors[0].Personnel.Add(spawnedPerson);

            // Create personnel with insufficient clearance to target floor, and spawned floor is not default floor
            //Personnel spawnedPerson = new Personnel(2, floors[3], floors[1], 0);
            //floors[1].Personnel.Add(spawnedPerson);

            // Create personnel with insufficient clearance to spawned floor
            //Personnel spawnedPerson = new Personnel(1, floors[3], floors[2], 0);
            //floors[2].Personnel.Add(spawnedPerson);

            // Create intruder on floor 0
            //Personnel spawnedPerson = new Personnel(0, floors[3], floors[0], 0);
            //floors[0].Personnel.Add(spawnedPerson);

            /*
             *
             * // Check all floors for new spawn and let it call elevator
             * for (int i = 0; i < floors.Count; i++)
             * {
             * // A floor can only call elevator once (pushing call button multiple times won't stack in queue)
             * // Only if there's a person on the floor the person can push the floor's call button
             * // TODO: Maybe the one floor one call rule should be removed and add a called field to personnel instead?  If so scanner has to be reworked
             * if (floors[i].CalledElevator == false && floors[i].Personnel.Count != 0)
             * {
             *     // First person in line gets to call elevator
             *     IPerson person = floors[i].Personnel[0];
             *     person.CallElevator(elevator);
             *     floors[i].scanner.ScanPerson(person);
             * }
             * }
             *
             * elevator.ShowElevatorQueue();
             *
             * // Elevator moves
             * Floor currentFloorOfElevator = control.MoveElevator();
             * // Person enters
             * if (currentFloorOfElevator.Personnel[0] == spawnedPerson)
             * {
             * if (spawnedPerson.HasCalledElevator)
             * {
             *     if (spawnedPerson.SpawnFloor == elevator.CurrentFloor)
             *     {
             *         spawnedPerson.EnterElevator(elevator);
             *     }
             * }
             * }
             *
             *
             * // control checks clearance level of person in elevator
             * control.RetrieveScanResult(currentFloorOfElevator);
             *
             *
             * // When arrived to floor let control decide what to do
             *
             * // Check to see if there's any person left
             * CheckFloorsForPersonnel(floors);
             */
            #endregion
        }
Ejemplo n.º 2
0
 public Control(Elevator elevator, Floor defaultFloor)
 {
     this.elevator     = elevator;
     this.defaultFloor = defaultFloor;
 }
Ejemplo n.º 3
0
 public void EnterElevator(Elevator elevator)
 {
     elevator.personInElevator = this;
     Console.WriteLine($"[Personnel]: {Id} has entered elevator");
 }