public void RequestElevator(FloorRequest request) { //if the request is already present in the queue, ignore it if (!_requestQueue.Any(x => x.From == request.From && x.To == request.To)) { _requestQueue.Enqueue(request); } }
private void HandleRequest(FloorRequest floorRequest) { IElevator elevator = GetElevator(floorRequest); if (elevator != null) { elevator.RequestElevator(floorRequest); } else { //no elevators are avaiable at the moment, put the request back in queue. RequestElevator(floorRequest); } }
public void RequestElevator(FloorRequest req) { var stopRequest = StopRequests.Find(sr => sr.FloorNumber == req.To); if (req.GoingDown) { stopRequest.DownRequest = true; } if (req.GoingUp) { stopRequest.UpRequest = true; } if (TargetFloor.FloorNumber == CurrentFloor.FloorNumber) { Land(); return; } if (IsIdle()) { TargetFloor = GetFloor(req, CurrentFloor); if (req.IsGoingDown) { GoDown(); } if (req.IsGoingUp) { GoUp(); } return; } //elevator is going down and the current target is greater than 'To', //new target is 'To' if (IsGoingDown() && TargetFloor.FloorNumber > req.To) { TargetFloor = GetFloor(req, CurrentFloor); } //elevator is going up and the current target is smaller than 'To', //new target is 'To' if (IsGoingUp() && TargetFloor.FloorNumber < req.To) { TargetFloor = GetFloor(req, CurrentFloor); } }
private Floor GetFloor(FloorRequest req, Floor floor) { if (floor.FloorNumber == req.To) { return(floor); } if (floor.FloorNumber > req.To) { return(GetFloor(req, floor.Lower)); } else { return(GetFloor(req, floor.Upper)); } }
public void RequestElevator(int from, int to, bool?up = false, bool?down = false) { FloorRequest request = new FloorRequest { From = from, To = to }; if (up ?? false) { request.GoingUp = true; } if (down ?? false) { request.GoingDown = true; } RequestElevator(request); }
private IElevator GetElevator(FloorRequest floorRequest) { //are there any idle elevators on this floor var idleElevator = _building.Elevators.Find( x => x.CurrentFloor.FloorNumber == floorRequest.From && x.IsIdle() ); if (idleElevator != null) { return(idleElevator); } //get all elevators that are moving in this direction //and are at requested floor var sameDirectionalElevators = _building.Elevators.FindAll(x => ( x.IsGoingDown() && floorRequest.IsGoingDown || x.IsGoingUp() && floorRequest.IsGoingUp ) && x.CurrentFloor.FloorNumber == floorRequest.From ); //there are elevators travelling in this direction if (sameDirectionalElevators.Count > 0) { //probably we can do some optimization here (like open the least weighing elevator // , but we need to capture weight of the elevator including the load) return(sameDirectionalElevators[0]); } //if there are any idle elevators, send one of them idleElevator = _building.Elevators.Find(x => x.IsIdle()); if (idleElevator != null) { return(idleElevator); } //none of the elevators are ready now, lets wait for next round return(null); }