简体   繁体   中英

Flawed logic in elevator simulation for finding floors

So I'm trying to work out how the elevator will work in a single instance. It gets a persons arrival floor and its destination, and moves successfully to the arrival floor. However I'm not too sure how to make this work properly. It's meant to wait for another Person to be added to the Call queue, but it just doesn't work right.

Here is the run() method in the elevator class

@Override
public synchronized void run()
{
    while(true)
    {


        List<ElevatorCall> callsWaiting = new ArrayList<ElevatorCall>();

        queue.drainTo(callsWaiting);

        for(int i = 0; i < callsWaiting.size(); i++)
        {   
            boolean directionToGo = getDirection(callsWaiting.get(i).getDestinationFloor(), currentPosition);
            int floorsToGoForCollection = getFloorsToGo(callsWaiting.get(i).getCollectionFloor());
            int floorsToGoForDestination = getFloorsToGo(callsWaiting.get(i).getDestinationFloor());

            //System.out.println("Elevator is moving to floor #" + callsWaiting.get(i).getCollectionFloor());

            System.out.println("Elevator is on floor " + currentPosition);

            for(int j = 0; j < floorsToGoForCollection + 1; j++)
            {
                boolean wasPersonOnFloor = checkIfPersonIsOnFloor(currentPosition, callsWaiting);
                boolean isDestinationFloor = checkIfDestination(currentPosition, currentElevatorCalls);

                if(wasPersonOnFloor)
                {
                    currentElevatorCalls.add(callsWaiting.get(floorsToGoForCollection - j));
                    System.out.println("A person on this floor called the elevator");

                    //floorsToGoForCollection = getFloorsToGo(callsWaiting.get(i).getDestinationFloor());
                    //j = 0;

                    directionToGo = getDirection(callsWaiting.get(i).getDestinationFloor(), currentPosition);
                    break;
                }

                if(isDestinationFloor)
                    callsWaiting = removeDestinationFromQueue(currentPosition, callsWaiting);

                if(callsWaiting.size() == 0)
                {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                //floorsToGoForCollection = getFloorsToGo(callsWaiting.get(i).getCollectionFloor());
                //floorsToGoForDestination = getFloorsToGo(callsWaiting.get(i).getDestinationFloor());

                wasPersonOnFloor = false;
                moveFloor(directionToGo);

            }   
        }       
    }
}

And in my main class I have this code to simulate it. I'm fairly sure my logic in the run() isn't correct but I can't seem to figure out what aspect of it is.

    run();

    Elevator elevator = new Elevator(queue);
    elevator.run();
}

public synchronized static void run()
{
    Random rand = new Random();
    int waitTime = rand.nextInt((1000 - 100) + 1) + 100;
    int startingID = 1;

    List<Person> people = new ArrayList<Person>(startingID);

    people.add(new Person(queue));

    for(int i = 0; i < people.size(); i++)
    {
        people.get(i).run();
    }

    try {
        Thread.sleep(waitTime);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Thanks for any help with the logic.

Does your Elevator class or Person extend Thread ? Remember that to start a Thread you must call start() as opposed to run() . If it is instead a Runnable , you must pass it to a new Thread object and start() that new thread. Maybe your code isn't advancing because it is stuck in a loop since it's handled in a single thread?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM