class Elevator {

    private class Car
    {
	private class Button
	{
	    private boolean []buttons;

	    Button( int n )
	    {
		int i;
		buttons = new boolean[n];
		for ( i=0; i< n; i++ ) 
		    buttons[i] = false;
	    }

	    void press ( int n )
	    {
		if ( n <= buttons.length && n > 0 )
		    buttons[n-1] = true;
	    }

	    boolean[] pressed ()
	    {
		return buttons;
	    }

	    void reset_1 ( int n )
	    {
		if ( n <= buttons.length && n > 0 )
		    buttons[n-1] = false;
	    }

	    void reset_all()
	    {
		int i;
		for (i=0; i< buttons.length; i++)
		    buttons[i] = false;
	    }
	}

	private Button b1;
	private int floor;
	private int next_floor;
	private boolean doors_open;
	private boolean going_up;
	private boolean going_down;

	Car( int n )
	{
	    //place the car on the first floor with the doors open going nowhere
	    floor = 1;
	    next_floor = 1;
	    doors_open = true;
	    going_up = false;
	    going_down = false;
	    b1 = new Button(n);
	}

	int current_floor ()
	{
	    return floor;
	}

	void call_car( int n )
	{
	    b1.press(n);
	}

	void move() {

	    boolean []calls;
	    calls = b1.pressed();

	    if ( calls[floor-1] ) { 
		if ( !doors_open ) {
		    doors_open = true;
		    System.out.print("stopping on floor: " + floor + "...open doors\n");
		}
		else {
		    doors_open = false;
		    System.out.print("closing doors on floor " + floor + "\n");
		    b1.reset_1(floor);
		    if ( next_floor == floor ) {
			going_up = false;
			going_down = false;
			pick_next();
		    }
		}
	    }
	    if ( next_floor != floor ) {
		doors_open = false;
		if ( next_floor > floor ) {
		    going_up = true;
		    going_down = false;
		    floor++;
		}
		else if ( next_floor < floor ) {
		    going_down = true;
		    going_up = false;
		    floor--;
		}
	    
	    } else {
		going_up = false;
		going_down = false;
		doors_open = true;
		pick_next();
	    }
	}
	

	int next_stop () {
	    return next_floor;
	}

	void pick_next ()
	{
	    int i, j;
	    boolean done;
	    boolean []calls;

	    calls = b1.pressed();

	    if ( !going_up && !going_down ) {
		i = floor-2; 
		j = floor;
		//bit odd here current floor in button array is floor-1

		if ( i < 1 )
		    i = 0;
	    
		if ( j > calls.length-1 )
		    j = calls.length-1;
	    
		done = false;
		while ( !done ) {
		    if ( calls[i] ) {
			next_floor = i+1;
			done = true;
		    }
		    if ( calls[j] ) {
			next_floor = j+1;
			done = true;
		    }
		    if ( i==0 && j==calls.length-1 && !done ) {
			done = true;
			next_floor = floor;
		    }
		    if ( i > 0 )
			i--;
		    if ( j < calls.length-1 )
			j++;

		}
	   
	    }
	}
    }


    private Car car;

    Elevator ( int n )
    {
	car = new Car(n);
    }

    public void move()
    {
	car.move();
    }

    public void call_car ( int n )
    {
	car.call_car( n );
	car.pick_next();
	car.move();
    }

    public void send_car ( int n )
    {
	call_car( n );
    }

    public void print_status ()
    {
	System.out.print("Current floor: " + car.current_floor() + "\n");
	System.out.print("Next stop: " + car.next_stop() + "\n");
    }
}

public class Elevator_2
{

    public static void main ( String []args )
    {
	Elevator e1 = new Elevator(10);
	e1.call_car ( 7 );
	e1.print_status ();
	e1.move();
	e1.move();
	e1.call_car ( 9 );
	e1.print_status();
	e1.move();
	e1.move();
	e1.move();
	e1.print_status();
	e1.move();
    }
}













