class Car
{
    private int floor;
    private int next_floor;
    private boolean doors_open;
    private boolean going_up;
    private boolean going_down;

    Car()
    {
	//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;
    }

    public int current_floor ()
    {
	return floor;
    }

    public void move( Button b1 ) {

	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(b1);
		}
	    }
	}
	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(b1);
	}
    }
	

    public int next_stop () {
	return next_floor;
    }

    public void pick_next ( Button b1 )
    {
	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++;

	    }
	   
	}
    }
}

class Button
{
    private boolean []buttons;

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

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

    public boolean[] pressed ()
    {
	return buttons;
    }

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

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



public class Elevator
{

    public static void main ( String []args )
    {

	//Test out the button functionality
	Button b1 = new Button(5);
	boolean []b_temp;
	int i;


	b_temp = b1.pressed();
	System.out.print("Pressed [ ");
	for ( i=0; i< b_temp.length; i++) {
	    if ( b_temp[i] == false )
		System.out.print("0, ");
	    else
		System.out.print("1, ");
	}
	b1.press(1);
	b1.press(5);
	b1.press(3);
	System.out.print("]\nPressed after update [ ");
	for ( i=0; i< b_temp.length; i++) {
	    if ( b_temp[i] == false )
		System.out.print("0, ");
	    else
		System.out.print("1, ");
	}
	System.out.print("]\n");

	//Test out the Car functionality
	b1.reset_all();
	b1.press(3);
	System.out.print("]\nPressed after update [ ");
	for ( i=0; i< b_temp.length; i++) {
	    if ( b_temp[i] == false )
		System.out.print("0, ");
	    else
		System.out.print("1, ");
	}
	System.out.print("]\n");


	Car c1 = new Car();

	c1.pick_next(b1);
	System.out.print("Next floor: "+c1.next_stop()+ "\n");
	b1.reset_all();
	b1.press(1);
	c1.pick_next(b1);
	System.out.print("Next floor: "+c1.next_stop()+ "\n");
	b1.reset_all();
	System.out.print("\n-->system reset<--\n");
	b1.press(5);
	c1.pick_next(b1);
	System.out.print("Next floor: "+c1.next_stop()+ "\n");
	b1.press(3);
	c1.pick_next(b1);
	System.out.print("Testing moves, calls to floors 3 & 5 car at 1 ");
	System.out.print("Next floor: "+c1.next_stop()+ "\n");
	c1.move(b1);
        System.out.print("Currnet floor: " + c1.current_floor() + " next stop " + c1.next_stop() + "\n");
	c1.move(b1);
	c1.move(b1);
	System.out.print("Currnet floor: " + c1.current_floor() + " next stop " + c1.next_stop() + "\n");
	c1.move(b1);
	System.out.print("Currnet floor: " + c1.current_floor() + " next stop " + c1.next_stop() + "\n");
	System.out.print("Adding call to floor 1\n");
	b1.press(1);
	c1.move(b1);
	System.out.print("Currnet floor: " + c1.current_floor() + " next stop " + c1.next_stop() + "\n");
	c1.move(b1);
	c1.move(b1);
	c1.move(b1);
	System.out.print("Currnet floor: " + c1.current_floor() + " next stop " + c1.next_stop() + "\n");
	c1.move(b1);
	c1.move(b1);
	c1.move(b1);
	c1.move(b1);
	System.out.print("]\nPressed after update [ ");
	for ( i=0; i< b_temp.length; i++) {
	    if ( b_temp[i] == false )
		System.out.print("0, ");
	    else
		System.out.print("1, ");
	}
	System.out.print("]\n");
    }
}













