Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Having issues with elevator java program simulation

Posted on 2011-09-23
15
Medium Priority
?
2,125 Views
Last Modified: 2012-06-27
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
Comment
Question by:doodguy
  • 10
  • 4
15 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36590261
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
 
LVL 47

Accepted Solution

by:
for_yan earned 1000 total points
ID: 36590314
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36590346
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 47

Expert Comment

by:for_yan
ID: 36590365

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
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1000 total points
ID: 36590373
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
 

Author Comment

by:doodguy
ID: 36602042
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36602087
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
 

Author Comment

by:doodguy
ID: 36602163
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36602234
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
 

Author Comment

by:doodguy
ID: 36602279
Are you suggesting i create a new variable called "countBoarded" ? isnt that the same as numPassengers?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36602301
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
 

Author Comment

by:doodguy
ID: 36926271
I've requested that this question be deleted for the following reason:

re wrote program
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36926272
>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
 
LVL 47

Expert Comment

by:for_yan
ID: 36939055
Thanks,  _alias99.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question