Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Elevator problem: How to add a condition to move one elevator at a time

 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[UPDATE]: I think I found the problem. I will post here if I can't figure it out, along with a more specific question.


Hi CR,

I am working on an elevator project where I have to move 8 elevators independently to different floors to pick up and drop off passengers. I have most of the code written, and it works sort of when I am only using one elevator. When I try to move all 8 elevators, they just move to the same floor at the same time.

I think the problem is that because there is no condition to invoke the takeAction method in the elevator class, each time the elevators are looped, they all just go through the takeAction method, which is why they are all moving together. How could I add a condition before the switch statement, so that only the elevators that are NOT in motion goes through the takeAction method?

Any insights would be greatly appreciated!







 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
without looking at a single line of code, my thoughts go like this:

In the real world, what would an actual bank of elevators look like? Would one elevator know anything about the location/movement of any of the others? Or would there be some kind of central controller that knew the current position and destination of every elevator, as well as handling all the requests (i.e. "call to this floor" or "go to that floor").
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:without looking at a single line of code, my thoughts go like this:

In the real world, what would an actual bank of elevators look like? Would one elevator know anything about the location/movement of any of the others? Or would there be some kind of central controller that knew the current position and destination of every elevator, as well as handling all the requests (i.e. "call to this floor" or "go to that floor").


Thanks! Here, I was trying to see whether or not the elevator is stopped (in the takeAction method, the stop time decrements every time it is moving up or down. The problem, though, is that STOP_TIME will always be 3 because it will not decrement until the elevator is moving up or down. So, I thought that this is where the variable that acts as a position holder is used (for the control center you had mentioned). But, I am not sure how to create this variable that holds the positions of the elevator. I tried to create an Building class object, but I keep getting an error that says constructor is not defined. How do I use an array of another class by creating an object to access it, without it giving me this error (I want to use it, not send new arguments. I need to access the elevator array from the Building class so that I could set the elevator array in Elevator class to the one from the Building class)?

This is what I currently have:




EDIT: I just realized that I need to have to for loop keep looping back to 0, or else no elevators will move after i becomes 8. And that FLOOR_TIME == 3 will always be true, since the FLOOR_TIME never gets the chance to decrement, since this for loop happens before the elevators moving up and down(which causes the FLOOR_TIME to decrement and then reset to 3 after). Sigh.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic