Solved

Having issues with elevator java program simulation

Posted on 2011-09-23
15
1,716 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 250 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
 
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 250 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now