doodguy
asked on
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!
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();
}
}
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;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So I guess it starts with Move() then goes to the second floor
then goes to stop() here:
if(myFloors[currentFloor]. queuedPass engers > 0){
stop();
but in stop() it chaeck this if:
if (floorRequests[currentFloo r] == 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()
then goes to stop() here:
if(myFloors[currentFloor].
stop();
but in stop() it chaeck this if:
if (floorRequests[currentFloo
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()
by the way you don't need this for boolean:
if (floorRequests[currentFloo
just
if(floorRequests[currentFl
but of course the main thing is that you need to have it true
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.]
I guess you should do it like that:
myFloors[2].queuedPassenge rs = 10;
(I am not sure, if you are counting floors from zero, then your second floor will be myFloors[1].queuedPassenge rs = 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
myFloors[2].queuedPassenge
(I am not sure, if you are counting floors from zero, then your second floor will be myFloors[1].queuedPassenge
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
ASKER
For yan - I put the
myFloors[2].queuedPassenge rs = 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?
myFloors[2].queuedPassenge
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?
No, I don't see anychecks in your stop() method:
You should put something like this:
if (floorRequests[currentFloor] ){
try {
boardPassenger();
if(numPassengers >= capacity){
throw new ElevatorFullException();
}
}
catch (ElevatorFullException e){
System.out.println("ElevatorFullEception");
}
}
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");
}
}
ASKER
Are you suggesting i create a new variable called "countBoarded" ? isnt that the same as numPassengers?
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
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
ASKER
I've requested that this question be deleted for the following reason:
re wrote program
re wrote program
>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.
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.
Thanks, _alias99.
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?