Hello Guys,
I am writing an elevator problem and am stuck at the movement of the elevator and also stopping the elevator. The elevator is suppose to state the current floor is starting and the number of passengers that are in the elevator.
I am able to let it go up one floor and finding it difficult going to the highest floor and also for the elevator to go down.
Here is my code:

public class Elevator {

    private static int Level = 7;
    private static int Passengers = 10;
    static int cPassengers = 3;
    static int cfloor = 3;


    public static void main(String[] args)
    {
        System.out.println("Currently " + cPassengers + " passenger on board\n" + "Current floor " + cfloor);
        System.out.println("................................................................................");
        Elevator move = new Elevator();

        move.Stop();
        move.boardPassenger(cfloor);




    }



        public void moveUp()
        {
            cfloor++;
        }
        public void moveDown()
        {
            cfloor--;
        }

        public void boardPassenger(int floor)
        {
            cfloor = floor;
            cPassengers--;
            System.out.println("Currently " + --cPassengers + " on borad\n" + "Current floor: " + cfloor);
        }

        //Elevator stopping at a specfic floor
        public void Stop()
        {
            cfloor++;
            System.out.println("Stopping at floor: " + cfloor);
        }

}

The requirement is for the elevator to go up and down and also to load and unload passengers.

Thanks in advance for your reply.

You didn't explain exactly what your problem is...
Please explain exactly how you are testing your code, exactly what output you expect, and exactly how the acual output is different from what you expect.

The output that am expecting is;

Currently (x number of) passengers on board.

Current floor: x

Stopping on floor x. // The destination of the elevator
Currently (x number of) passengers on board.

Current floor: x

Stopping on floor x. // The destination of the elevator
Currently (x number of) passengers on board.
Current floor: x

The elevator is also expected to load passengers and unload passengers that are waiting at every floor that it reaches. Also have the ability to go up and down.
The problem I have facing is how to to add passengers to the elevator at it reaches every floor.

Thanks

private static int Level = 7;
    private static int Passengers = 10;
    static int cPassengers = 3;
    static int cfloor = 3;

I see a bunch of static variables that seem like they should be non-static members of the Elevator class. Why create objects if all you do is manipulate static variables. What if you have TWO elevators? If there isn't a reason to make them static (and I see no reason at all and every reason to make them non-static), make them non-static. You need to redesign everything.

An elevator has a current floor and a collection of passengers at any given time. That's the state of the elevator. Two variables. You have two PAIRS of variables. I see no reason to have the pairing.

Think of a real world elevator. You have people waiting and they need to go up or down and they are on a certain floor and they have a certain floor as a destination. That reminds me of the third data member that an elevator has. Direction. I imagine you need to add that one.

You might want to create a Passenger class. A passenger has a current state (either ON the elevator or waiting for the elevator, as well as a destination). An elevator has a bunch of Passengers. It stops on a floor if and only if there is a passenger waiting that is going the same direction or there is a passenger on the elevator that needs to get off.

Therefore I think a complete redesign is in order. No static variables anywhere as far as I can tell.

Edited 4 Years Ago by VernonDozier

The boardPassenger method is suppose to add passengers to the elevator on every floor it stops.

This article has been dead for over six months. Start a new discussion instead.