• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2258
  • Last Modified:

Having issues with elevator java program simulation

I'm writing a program for a class that has an elevator going up floor to floor, boarding and unloading passengers and making sure the elevator is not overloaded. There is three classes, elevator which houses most of the function of this program, the floor class that represents each floor function and an exception class. I have it working without errors but its not working as I expected.   Right now this is the only output:

Currently 1 passengers onboard. Current Floor: 1
Going up!. There are currently 1 passengers on board.
Stopping on floor 2

It is supposed to be loading a few passengers per floor but it is not.  Here is the code for the Elevator and Floor class. Thanks for your help!


public class Elevator {


    final static int floors = 7;
    final int capacity = 10;
    
    static int[] destinationRequest = new int[7];  
    public static Floor[] myFloors = new Floor[7];
    public int [] passToDestination ={0,0,0,0,0,0,0,0};
    public boolean [] floorRequests = new boolean[7];	
    int currentFloor;
    int numPassengers;
    int requestFloor;
    int currentDirection;
    int destinationFloor;
    int passengerRequest;
    public int queuedPassengers;
    
    boolean directionUp;
    boolean directionDown;
    
    
   public static void main(String[] args) {
    
       
	   {
           Elevator myElevator = new Elevator();
           
           myElevator.currentFloor = 1;
           myElevator.numPassengers = 1;
           myElevator.directionUp = true;
           myElevator.directionDown = false; 
           
          for (int i = 0; i <= 6; i++)
          		{
             myFloors[i] = new Floor();
           		}
                

           myFloors[1].queuedPassengers = 3;
           myFloors[2].queuedPassengers = 2;
           myFloors[3].queuedPassengers = 1;
          
           
           System.out.println(myElevator);            
           myElevator.move(); 
         
           
	   }  
   }


   /**
    * Directs the elevator to run, checks to see if elevator should stop on a given floor.
    * 
    */
   public void move()
    {
           if(directionUp == true){
               currentFloor = currentFloor +1;
               System.out.println("Going up!. There are currently " + numPassengers + " passengers on board.");
         }
   else {
   currentFloor = currentFloor -1;
   System.out.println("Going down one floor.There are currently " + numPassengers + " passengers on board.");
   }
         if(numPassengers <= capacity){
               if(myFloors[currentFloor].queuedPassengers > 0){
                     stop();
            }
         }
   //reverses dirction at bottom floor
   //if (currentFloor == 0){directionUp = true;}
   //if (currentFloor == 6){directionUp = false;}
   
   }
      
      
 
    
   /**
    * Stops elevator on given floor and reports its state.
    * 
    */
    public void stop()
    {
          System.out.println("Stopping on floor " + currentFloor);
          
          if (passToDestination[currentFloor] >= 1){
        	  numPassengers = numPassengers - 1;
              passToDestination[currentFloor]--;
                }
           if (floorRequests[currentFloor] == true ){
                 try {
                 boardPassenger();
                 if(numPassengers >= capacity){
                       throw new ElevatorFullException();
                       }
                 }
                 catch (ElevatorFullException e){
                       System.out.println("ElevatorFullEception");
                 }
                 
           }
           currentFloor = currentFloor + 1;
      }
          
    
    /**
     * Boards a passenger when called, sets direction variables.
     * @param i 
     * 
     */
    void boardPassenger() throws ElevatorFullException
    {
    	 if(myFloors[currentFloor].queuedPassengers == 0){
             move();
       }
       
       while(numPassengers <= capacity){
             myFloors[currentFloor].queuedPassengers = myFloors[currentFloor].queuedPassengers - 1;
             numPassengers++;
             passToDestination[0]++;
             }
       
       if(numPassengers >= capacity)
       {
            throw new ElevatorFullException();
       }      
 }
    

    /**
     * Subtracts number of passengers destined for current floor and resets destination
     * 
     */
   // public void unloadPassengers(int i)
   //      {
    //          numPassengers = numPassengers - destinationFloor;  
    //		
    //           requestFloor = 0;  
     //    }
   
    /**
     * Overrides the java.lang toString method
     * 
     */
   public String toString()  
    {
         return "Currently " + numPassengers + " passengers onboard. Current Floor: " + currentFloor; 
    }
   
   public void registerRequest(int floor, int passengers)
   {
         requestFloor = floor;
         passengerRequest = passengers;
         myFloors[requestFloor] = new Floor();
   }

}

Open in new window

public class Floor {
	
	 public int queuedPassengers;
     
     
     public void unloadPassenger(Elevator myElevator){
           int i = myElevator.passToDestination[myElevator.currentFloor];
           if (i > 0){
                 myElevator.numPassengers = myElevator.numPassengers - 1;
                 System.out.println("One passenger unloaded on floor." + queuedPassengers + " are left on floor" + myElevator.currentFloor);
                 }
           return;
     }
}

Open in new window

0
doodguy
Asked:
doodguy
  • 10
  • 4
2 Solutions
 
for_yanCommented:
I think it does what you requested.
What do you want to happen?
You may probaly want to add a question to the user, so that
the user could give a command - say this is a new passesnger with desination thsi or that floor
Then  you'll report that command is executed ask for next command something like that.
Otherwise how youexpect it to function?
0
 
for_yanCommented:
floorRequests array isi initialized as boolen - means all are false

then you check if it is true a,and then you board , but t is false
0
 
for_yanCommented:
So I guess it starts with Move() then goes to the second floor
then goes to stop() here:
 if(myFloors[currentFloor].queuedPassengers > 0){
                     stop();

but in stop() it chaeck this if:

if (floorRequests[currentFloor] == true ){
                 try {
                 boardPassenger();
                 if(numPassengers >= capacity){
                       throw new ElevatorFullException();
                       }
                 }

and I can find only how you initialize floorRequersts,
public boolean [] floorRequests = new boolean[7];      
but I don;'t see anywahere where you change these fvalues to true,
so it does not go to boardPassenger()


0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
for_yanCommented:

by the way you don't need this for boolean:
    if (floorRequests[currentFloor] == true )

just

if(floorRequests[currentFloor]) would be fine

but of course the main thing is that you need to have it true

0
 
for_yanCommented:
I changed it this way (changed all requests to true
and added printout in the boarding method
public class Elevator {


    final static int floors = 7;
    final int capacity = 10;

    static int[] destinationRequest = new int[7];
    public static Floor[] myFloors = new Floor[7];
    public int [] passToDestination ={0,0,0,0,0,0,0,0};
    public static boolean [] floorRequests = new boolean[7];
    int currentFloor;
    int numPassengers;
    int requestFloor;
    int currentDirection;
    int destinationFloor;
    int passengerRequest;
    public int queuedPassengers;

    boolean directionUp;
    boolean directionDown;


   public static void main(String[] args) {


	   {
           Elevator myElevator = new Elevator();

           myElevator.currentFloor = 1;
           myElevator.numPassengers = 1;
           myElevator.directionUp = true;
           myElevator.directionDown = false;

          for (int i = 0; i <= 6; i++)
          		{
             myFloors[i] = new Floor();
                      floorRequests [i] = true;
           		}


           myFloors[1].queuedPassengers = 3;
           myFloors[2].queuedPassengers = 2;
           myFloors[3].queuedPassengers = 1;


           System.out.println(myElevator);
           myElevator.move();


	   }
   }


   /**
    * Directs the elevator to run, checks to see if elevator should stop on a given floor.
    *
    */
   public void move()
    {
           if(directionUp == true){
               currentFloor = currentFloor +1;
               System.out.println("Going up!. There are currently " + numPassengers + " passengers on board.");
         }
   else {
   currentFloor = currentFloor -1;
   System.out.println("Going down one floor.There are currently " + numPassengers + " passengers on board.");
   }
         if(numPassengers <= capacity){
               if(myFloors[currentFloor].queuedPassengers > 0){
                     stop();
            }
         }
   //reverses dirction at bottom floor
   //if (currentFloor == 0){directionUp = true;}
   //if (currentFloor == 6){directionUp = false;}

   }




   /**
    * Stops elevator on given floor and reports its state.
    *
    */
    public void stop()
    {
          System.out.println("Stopping on floor " + currentFloor);

          if (passToDestination[currentFloor] >= 1){
        	  numPassengers = numPassengers - 1;
              passToDestination[currentFloor]--;
                }
           if (floorRequests[currentFloor] ){
                 try {
                 boardPassenger();
                 if(numPassengers >= capacity){
                       throw new ElevatorFullException();
                       }
                 }
                 catch (ElevatorFullException e){
                       System.out.println("ElevatorFullEception");
                 }

           }
           currentFloor = currentFloor + 1;
      }


    /**
     * Boards a passenger when called, sets direction variables.
     * 
     *
     */
    void boardPassenger() throws ElevatorFullException
    {
    	 if(myFloors[currentFloor].queuedPassengers == 0){
             move();
       }

       while(numPassengers <= capacity){
             myFloors[currentFloor].queuedPassengers = myFloors[currentFloor].queuedPassengers - 1;
             numPassengers++;
             passToDestination[0]++;
           System.out.println("boarding");
             }

       if(numPassengers >= capacity)
       {
            throw new ElevatorFullException();
       }
 }


    /**
     * Subtracts number of passengers destined for current floor and resets destination
     *
     */
   // public void unloadPassengers(int i)
   //      {
    //          numPassengers = numPassengers - destinationFloor;
    //
    //           requestFloor = 0;
     //    }

    /**
     * Overrides the java.lang toString method
     *
     */
   public String toString()
    {
         return "Currently " + numPassengers + " passengers onboard. Current Floor: " + currentFloor;
    }

   public void registerRequest(int floor, int passengers)
   {
         requestFloor = floor;
         passengerRequest = passengers;
         myFloors[requestFloor] = new Floor();
   }

}

class ElevatorFullException extends Exception {

}

Open in new window


and got this output:

Currently 1 passengers onboard. Current Floor: 1
Going up!. There are currently 1 passengers on board.
Stopping on floor 2
boarding
boarding
boarding
boarding
boarding
boarding
boarding
boarding
boarding
boarding
ElevatorFullEception

Open in new window

0
 
doodguyAuthor Commented:
For_yan - thanks for your input.    The problem is, i should specify how many are loading and queued per floor in the main.   Right now i want it looks like it goes to floor 2 and boards 10 people.. but where in the code is that specified?

Here are my requirements more specifically.

-Creates an Elevator with its attendant array of Floor objects.
-Load the Elevator with some passengers going to different floors.
-Initialize some (but not all) Floors with some arbitrary number of passengers waiting for the Elevator. Make sure there are enough passengers waiting so that at some point the Elevator will reach capacity and overflow to demonstrate your new exception. This will mean that the Elevator needs to make more than one pass through the buildling to get all the passengers down to the ground floor.
-Run the Elevator up and down collecting passengers and delivering them to the ground floor.
-Show the Elevator and its Floors successfully collaborating. In other words as the program runs it should show the Elevator unloading passengers to the Floor and the Floor boarding passengers onto the Elevator. [Here, "showing" means nothing more than simple print statements on standard output.]
0
 
for_yanCommented:
I guess you should do it like that:
myFloors[2].queuedPassengers = 10;

(I am not sure, if you are counting floors from zero, then your second floor will be myFloors[1].queuedPassengers = 10)

I guess then in the stop() method you should check how many passengers you have in the queue on the floor where you mad a stop -
and make a loop through boradPassenger() as many times as you need - something like that
 
0
 
doodguyAuthor Commented:
For yan - I put the
myFloors[2].queuedPassengers = 10;
 in my program and it ran no differently.  I thought in my stop() method I had a check built in to check the amount of passengers?
0
 
for_yanCommented:
No, I don't see anychecks in your stop() method:

  if (floorRequests[currentFloor] ){
                 try {
                 boardPassenger();
                 if(numPassengers >= capacity){
                       throw new ElevatorFullException();
                       }
                 }
                 catch (ElevatorFullException e){
                       System.out.println("ElevatorFullEception");
                 }

           }

Open in new window


You should put something like this:

  if (floorRequests[currentFloor]   &&  myFloor[currentFloor].queuedPassengers > 0){

                 try {
  countBoarded = 0;
     for(int j=0; j<myFloor[currentFloor].queuedPassengers; j++){ 
                 boardPassenger();
 countBoarded++;

                 if(numPassengers >= capacity){
                       throw new ElevatorFullException();
                       }

          }
myFloor[currentFloor].queuedPassengers = myFloor[currentFloor].queuedPassengers - countBoarded;



                 }
                 catch (ElevatorFullException e){
                       System.out.println("ElevatorFullEception");
                 }

           }

Open in new window




0
 
doodguyAuthor Commented:
Are you suggesting i create a new variable called "countBoarded" ? isnt that the same as numPassengers?
0
 
for_yanCommented:
No, numPassenger is instance variable - keeping the value for the whole class - don't knwio where
you will be using also

countBoarded is local counter neede aonly for the duration of boarding on this floor in order to update the
queue on this floor (we could get along without it at all, but in case we
jump out of the loop before the expected number of boarding becuase of capacity overflow -
we'd need this counter to update the queue correctly
0
 
doodguyAuthor Commented:
I've requested that this question be deleted for the following reason:

re wrote program
0
 
for_yanCommented:
>It is supposed to be loading a few passengers per floor but it is not

This reason of this issue was found and the code was recompilesd and executed
and  it was booarding  (see ID:36590373, ID:36590314).
in addition some other points were addrressed.

Believe that this issue was resolved, even though the authtor chose to re-write the program.
0
 
for_yanCommented:
Thanks,  _alias99.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now