Exemple #1
0
        private void MoveElevator()
        {
            // The elevator is moving in a direction
            if ((CurrentDirection == Direction.up) || (CurrentDirection == Direction.down))
            {
                int destination = -1;

                // Are there more items on the CurrentList - which are eligible for servicing?
                int found = CurrentList.FindIndex((n) =>
                {
                    if (CurrentDirection == Direction.up)
                    {
                        return(n > CurrentFloor);
                    }
                    else if (CurrentDirection == Direction.down)
                    {
                        return(n < CurrentFloor);
                    }
                    return(false);
                });

                // If not, it is time to reverse direction
                if (found == -1)
                {
                    List <int> otherList = (CurrentList == upList) ? downList : upList;
                    if (otherList.Count == 0)
                    {
                        CurrentDirection = Direction.idle;
                        return;
                    }
                    CurrentList = otherList;
                    SetDirection();
                    destination = CurrentList[0];

                    // We have switched lists.  Are we on a floor on the new list?
                    if (CurrentFloor == destination)
                    {
                        CurrentList.Remove(destination);
                        return;
                    }
                }
                else
                {
                    destination = CurrentList[found];
                }

                Console.WriteLine("The elevator is moving past floor {0} transitioning to {1}", CurrentFloor, destination);
                CurrentFloor = (CurrentDirection == Direction.up) ? CurrentFloor + 1 : CurrentFloor - 1;
                //FutureDirection = OppositeDirection;
                return;
            }
        }