/// <summary> /// Method for checking if our elevator and all of our floors are empty or not. /// </summary> /// <returns>Returns true of false based on of there are people left</returns> public bool CheckIfPeopleWaiting(Elevator elevat) { int peopleLeft = 0; foreach (Floor f in allFloors) { if (f.StillPeopleWaiting() == true) { peopleLeft++; } } if (elevat.GetCurrentPassagers().Count > 0) { foreach (Person p in elevat.GetCurrentPassagers()) { peopleLeft++; } } if (peopleLeft > 0) { return(true); } return(false); }
/// <summary> /// Main method used for running the simulation. /// The algorithm decides whether to go up or down based on where people currently standing in the elevator needs to get off. /// As long as there either are people waiting on any floor, or people waiting inside the elevator, the returned bool is true. /// </summary> /// <param name="elevator">Uses an elevator object in order to /// access it's methods and use the same object, instead of creating a new one.</param> /// <param name="numFloors">An integer representing the number of floors in our building, it is possible to change this variable</param> public void StartElevator(Elevator elevator, int numFloors = 10) { simulationComplete = false; int minFloorNum = 0; int maxFloorNum = numFloors - 1; if (elevator.GetCurrentPassagers().Count > 0) { elevator.RemovePersonFromElevator(); int goingUp = 0; int goingDown = 0; foreach (Person p in elevator.GetCurrentPassagers().ToList()) { if (p.End_floor > elevator.GetCurrentFloor()) { goingUp++; } else { goingDown++; } } if (goingUp > goingDown) { elevator.SetCurrentDirection(Direction.DirectionEnum.Up); elevator.MoveElevator(Direction.DirectionEnum.Up); } else { elevator.SetCurrentDirection(Direction.DirectionEnum.Down); elevator.MoveElevator(Direction.DirectionEnum.Down); } elevator.IncreaseSystemTime(); IncreaseWaitTime(); } else if (CheckIfPeopleWaiting(elevator) == true) { int currFloorNum = elevator.GetCurrentFloor(); if (currFloorNum == minFloorNum) { goUp = true; elevator.AddPersonToElevator(); elevator.RemovePeopleFromFloor(); elevator.SetCurrentDirection(Direction.DirectionEnum.Up); elevator.MoveElevator(Direction.DirectionEnum.Up); } else if (currFloorNum == maxFloorNum) { goUp = false; elevator.AddPersonToElevator(); elevator.RemovePeopleFromFloor(); elevator.SetCurrentDirection(Direction.DirectionEnum.Down); elevator.MoveElevator(Direction.DirectionEnum.Down); } else if (goUp == true) { elevator.AddPersonToElevator(); elevator.RemovePeopleFromFloor(); elevator.SetCurrentDirection(Direction.DirectionEnum.Up); elevator.MoveElevator(Direction.DirectionEnum.Up); } else if (goUp == false) { elevator.AddPersonToElevator(); elevator.RemovePeopleFromFloor(); elevator.SetCurrentDirection(Direction.DirectionEnum.Down); elevator.MoveElevator(Direction.DirectionEnum.Down); } elevator.IncreaseSystemTime(); IncreaseWaitTime(); } else if (CheckIfPeopleWaiting(elevator) == false) { simulationComplete = true; } }