public void move(Elevator elevator)
        {
            Console.WriteLine("column {0} elevator {1} is on floor : {2}", name, elevator.name, elevator.floorNumber);
            if (elevator.floorNumber == elevator.destinationFloor)
            //elevator is at it's current destination, needs to check if it has other destinations lined up.
            {
                //if all calls or requests have not been fullfilled this will catch them
                if (elevator.requestList.Count != 0)
                {
                    elevator.destinationFloor = elevator.requestList[0];
                }
                else if (callList.Count != 0)
                {
                    elevator.destinationFloor = callList[0];
                }

                //must open doors for potential passengers
                elevator.Doors.action();
            }

            while (elevator.floorNumber != elevator.destinationFloor)

            {
                if (!elevator.isElevatorFull())
                {
                    //sets movement direction of the elevator
                    if (elevator.destinationFloor > elevator.floorNumber)
                    {
                        elevator.movement = "UP";

                        elevator.floorNumber++;
                        if (!floorsServed.Contains(0) && elevator.floorNumber == 0)
                        {
                            elevator.floorNumber++;
                        }


                        //Console.WriteLine("column {0} elevator {1} moved to {2}", name, elevator.name, elevator.floorNumber);
                    }
                    else
                    {
                        elevator.movement = "DOWN";

                        elevator.floorNumber--;
                        if (!floorsServed.Contains(0) && elevator.floorNumber == 0)
                        {
                            elevator.floorNumber--;
                        }

                        //Console.WriteLine("column {0} elevator {1} moved to {2}", name, elevator.name, elevator.floorNumber);
                    }

                    foreach (int floor in floorsServed)
                    {
                        if (floor == elevator.floorNumber)
                        {
                            Thread.Sleep(500);
                        }
                    }
                    elevator.FloorDisplayUpdate(elevator.floorNumber, elevator.movement);


                    bool floorIsInCallList    = callList.Contains(elevator.floorNumber);
                    bool floorIsInRequestList = elevator.requestList.Contains(elevator.floorNumber);

                    //if elevator crosses a floor it should stop at (on a call or request list)
                    if ((floorIsInCallList && elevator.movement == elevator.toBase))
                    //|| (floorIsInRequestList && elevator.movement != elevator.toBase))
                    {
                        elevator.Doors.action();
                        int indexOfCall = callbuttonList.IndexOf(callbuttonList.Where(call => call.nameint == elevator.floorNumber).FirstOrDefault());
                        callbuttonList[indexOfCall].isPressed = false;
                        callList.Remove(elevator.floorNumber);
                        elevator.requestList.Remove(elevator.floorNumber);
                    }
                    if (elevator.floorNumber == elevator.destinationFloor)
                    //elevator has reached it's destination
                    {
                        elevator.Doors.action();
                    }

                    int tmpFloor = elevator.floorNumber;
                    while (elevator.floorNumber == elevator.destinationFloor)
                    {
                        //if all calls or requests have not been fullfilled this will catch them
                        if (callList.Count != 0)
                        {
                            elevator.destinationFloor = callList[0];
                        }
                        else if (elevator.requestList.Count != 0)
                        {
                            elevator.destinationFloor = elevator.requestList[0];
                        }
                        if (elevator.floorNumber == elevator.destinationFloor)
                        {
                            if (elevator.requestList.Count != 0)
                            {
                                elevator.requestList.RemoveAt(0);
                            }
                            else
                            {
                                elevator.floorNumber = 0;
                            }
                        }
                    }
                    elevator.floorNumber = tmpFloor;
                }
                else
                {
                    elevator.FloorDisplay.message = "FULL!";
                    elevator.FloorDisplay.messageDisplay();
                    Console.WriteLine("enter a value lower than {0}", Elevator.MAXLOAD);
                    string newLoad = Console.ReadLine();
                    elevator.load = float.Parse(newLoad);
                    elevator.FloorDisplayUpdate(elevator.floorNumber, elevator.movement);
                }
            }
            if (elevator.floorNumber == elevator.destinationFloor)
            {
                elevator.movement = "IDLE";
                //elevator.Doors.action();
                int indexOfCall = callbuttonList.IndexOf(callbuttonList.Where(call => call.nameint == elevator.floorNumber).FirstOrDefault());
                callbuttonList[indexOfCall].isPressed = false;
                goToIdle();
            }
        }
Beispiel #2
0
        public void AssignElevator(int RequestedFloor)

        {
            Elevator selectedElevator      = null;
            int      indexOfRequestedFloor = FloorRequestButtonList.IndexOf(FloorRequestButtonList.Where(button => button.nameint == RequestedFloor).FirstOrDefault());

            FloorRequestButtonList[indexOfRequestedFloor].isPressed = true;

            Column selectedColumn = null;

            selectedColumn = selectColumn(RequestedFloor);

            if (selectedColumn.online)
            {
                selectedColumn.createOnlineElevatorList();

                selectedColumn.sortElevatorByDistance(baseFloor, selectedColumn.onlineElevatorList);

                foreach (Elevator elevator in selectedColumn.onlineElevatorList)
                {
                    if (elevator.distance == 0 && elevator.movement == "IDLE")
                    {
                        selectedElevator = elevator;
                    }
                }
                if (selectedColumn.checkIfElevatorOnItsWay(baseFloor))
                {
                    selectedElevator = selectedColumn.selectedElevator;
                }
                foreach (Elevator elevator in selectedColumn.onlineElevatorList)
                {
                    if (elevator.movement == "IDLE")
                    {
                        selectedElevator = elevator;
                    }
                }

                if (selectedElevator == null)//all elevators are moving away from basefloor we will selectthe one whose last request is the smallest value.
                {
                    selectedColumn.onlineElevatorList.Sort((x, y) => x.requestList[x.requestList.Count - 1].CompareTo(y.requestList[y.requestList.Count - 1]));
                    selectedElevator = selectedColumn.onlineElevatorList[0];

                    interfaceDisplay.gotoColumn   = selectedColumn.nameLetter;
                    interfaceDisplay.gotoElevator = selectedElevator.name;
                    interfaceDisplay.gotoRequest  = RequestedFloor;

                    interfaceDisplay.displaygoto();

                    selectedColumn.move(selectedElevator);
                }
                else
                {
                    selectedElevator.requestList.Add(RequestedFloor);

                    if (selectedElevator.toBase == "UP")
                    {
                        selectedElevator.requestList.Sort((a, b) => b.CompareTo(a));// sortdescending
                    }
                    else
                    {
                        selectedElevator.requestList.Sort((a, b) => a.CompareTo(b));//sort ascending
                    }

                    interfaceDisplay.gotoColumn   = selectedColumn.nameLetter;
                    interfaceDisplay.gotoElevator = selectedElevator.name;
                    interfaceDisplay.gotoRequest  = RequestedFloor;

                    interfaceDisplay.displaygoto();
                }
                selectedElevator.destinationFloor = baseFloor;



                //selectedElevator.requestList.Add(baseFloor);

                selectedElevator.requestList.Add(RequestedFloor);
                selectedColumn.move(selectedElevator);



                //selectedElevator.destinationFloor = selectedElevator.requestList[selectedElevator.requestList.Count - 1];
                //selectedColumn.move(selectedElevator);


                SelectedElevator = selectedElevator;
                SelectedColumn   = selectedColumn;
            }
            else
            {
                interfaceDisplay.display("SORRY the column to your desired floor is offline.");
            }
        }