The classic elevator simulation problem

W

whitehatmiracle

Hello group
I got the code of this classic elevator simulation. Im having some
problem foloowing it.
And i feel it can be simplified... can u people help me?
here's the code:


class button
{
enum button_status {off, on};
public:
button(button_status = off);
void switch_on();
void switch_off();
bool is_off() ; //c
bool is_on() ; //c
private:
button_status state;
};

button::button(button_status s) : state(s) {}

void button::switch_on()
{
if(state == on)
cout << "Please be patient \n"; //it's already pushed
else
state = on;
}

void button::switch_off()
{
state = off;
}

bool button::is_off() //c
{
return state == off;
}

bool button::is_on() //c
{
return state == on;
}

class Panel
{
public:
Panel(int n);
~Panel();
void press(int n);
void reset(int n);
bool is_off(int n) const;
bool is_on(int n) const;
bool is_valid(int n) const;
private:
button** panel;
int number;
Panel(const Panel&);
};

Panel::panel(int n) : panel(0), number(n)
{
panel = new button*[number + 1];
for(int i = 0; i < number + 1; ++i)
panel = new button;
}

Panel::~Panel()
{
for(int i = number; i >= 0; --i)
delete panel;
delete [] panel;
}

bool Panel::is_off(int n) const
{
return panel[n]->is_off();
}

bool Panel::is_on(int n) const
{
return panel[n]->is_on();
}

bool Panel::is_valid(int n) const
{
return n != 13;
}

void Panel::press(int n)
{
if(!is_valid(n))
cout << "No such floor!\n";
else
panel[n]->switch_on();
}

void Panel::reset(int n)
{
panel[n]->switch_off();
}

class elevator
{
public:
elevator(int = 1, int = 0);
~elevator();
void prompt();
private:
Panel buttons;
int current_floor;
const int top_floor;
bool button_active(int) const;
void press(int);
void close_doors();
bool floor_is_valid(int) const;
elevator(const elevator&);
};

elevator::elevator(int n, int w) : buttons(n), current_floor(1),
top_floor(n) {}

elevator::~elevator()
{
cout << "Elevator will self destruct\n";
}

void elevator::prompt()
{
cout << "Key in the floor you would like to visit, from 1 to " <<
top_floor << ", 0 to close doors, EOF to exit: ";
int floor;
while(!(cin >> floor).eof())
{
if(floor == 0)
{
if(!button_active(1) && current_floor != 1)
press(1);
if(button_active(1))
close_doors();
}
else if(floor < 1 || floor > top_floor)
cout << "---Floor " << floor << " is not valid\n";
else if(floor == current_floor)
cout << "---You have reached your floor now\n";
else
press(floor);
cout << "Next floor: ";
}
}

void elevator::press(int n)
{
buttons.press(n);
}

void elevator::close_doors()
{
cout << "Doors are closing\n";
cout << "\a\a\a\a\a";
// If a button is pushed on a floor higher than the current
// floor, then the elevator always moves up.
if(button_active(current_floor))
cout << "Elevator accending\n";
else
cout << "Elevator decending\n";
sleep(1);
// Keep looping as long as the floor buton is off
while(buttons.is_off(current_floor))
{
if(button_active(current_floor))
++current_floor;
else
--current_floor;
if(buttons.is_off(current_floor) &&
floor_is_valid(current_floor))
{
cout << "\tPassing floor " << current_floor << '\a' << '\n';
sleep(1);
}
}
cout << "\tNow on floor " << current_floor << "\a\a\a" << '\n';
sleep(1);
buttons.reset(current_floor);
cout << "Doors are open\n";
sleep(1);
}

bool elevator::button_active(int f) const
{
for(int i = f; i <= top_floor; ++i)
if(buttons.is_on(i))
return true;
return false;
}

bool elevator::floor_is_valid(int f) const
{
return buttons.is_valid(f);
}

int main()
{
const int top = 9;
elevator otis(top);
otis.prompt();
return 0;
}
 
O

osmium

I got the code of this classic elevator simulation. Im having some
problem foloowing it.
And i feel it can be simplified... can u people help me?
here's the code:

<snip>

I will agree that it looks awful bulky, about 205 lines and there are no
people yet. (The classic elevator problem surely has people involved,
right?) It has the look of something where someone has said, "Build me an
industrial strength whatchamcallit", and then walked away. This is what the
engineer produced as a result of that conversation.

I would have a queue for each floor representing the number of people
waiting on that floor. So the basic (most interesting) data structure would
be an array of queues. The queue on the first floor has the most people in
the morning and the smallest number in the evening. Assuming a one elevator
building (you don't say what the goal is): The car monitors the queues and
accepts floor selections from the passengers as they get on. If the
building has several elevators, they share the array of queues of people but
have their own data as to the destination of the passengers on *that* car.

There would be a "people generator" object to populate the queues.

I think you have skipped the design phase, and started out by writing code,
a very natural thing to do. Simply writing (or re-writing what you have
been given) the specifications can even be helpful Doing so would have
revealed that you can accept more passengers than the elevator is designed
to carry. Mr. Otis made it safe, but I am sure it is quite scary when the
cables break.

If you can find some nice reference to "cloud" diagrams, they might help
you.
 
W

whitehatmiracle

I will agree that it looks awful bulky, about 205 lines and there are no
people yet. (The classic elevator problem surely has people involved,
right?)

Actually the problem doesnt involve queues, it's much simpler, at
different floors the user on the computer enters the number of the
floor the people want to go to.

I think you have skipped the design phase, and started out by writing code,
a very natural thing to do.

You are absolutely right, there was no design phase, i just got this
code and was trying to follow the logic...ill try and look for some
cloud diagrams too. Do you think ill need the panel class?


Mr. Otis made it safe, but I am sure it is quite scary when the
cables break.

LOL :)
Thank you for your reply
 
O

osmium

You are absolutely right, there was no design phase, i just got this
code and was trying to follow the logic...ill try and look for some
cloud diagrams too. Do you think ill need the panel class?

I know I didn't *like* the panel class. I didn't study it enough to be
sure, but it was a substantial part of what I saw as overkill. That's why I
made a point of "the building has a panel" and "the elevator (car) has a
*different* panel".
Mr. Otis made it safe, but I am sure it is quite scary when the

LOL :)

That was in response to your name for the elevator. I think that was my
favorite part of the whole program :)
 
J

James Kanze

[I'll] try and look for some
cloud diagrams too.

I suspect that you'll find that anything using cloud diagrams is
somewhat dated (not that good design has changed much in this
time). Cloud diagrams are Booch, and it's now over ten years
that he's merged with the other OO experts, and converted to
UML.
If you have access to a University library, they should have this book. I
like it but it never became popular. I have had very good results buying
used books via Amazon if you can wait a few days, four or so in the
continental US. It's faster than an inter-library loan where I live.

I've not seen the book, but seeing who the author is makes me
want to buy it. Horstman is good.
 
W

whitehatmiracle

I do not have access to the book.... and couldnt find any proper cloud
diagrams.
Is it possible to merge the panel class with the other 2 classes?
Can this whole thin be simplified?
 
O

osmium

I do not have access to the book.... and couldnt find any proper cloud
diagrams.
Is it possible to merge the panel class with the other 2 classes?
Can this whole thin be simplified?

There is nothing real special about the cloud diagrams, it was just
something that has virtually a zero learning curve - at least when presented
right - and is an aid to system design. You could look for something on UML
as has been suggested. A friend of mine teaches a one semester course on
UML at a local university which makes my wonder how much you might learn in
an hour or so, I don't know UML myself.

I think there is a good chance it could be simplified. But what are the
specifications for the elevator? "Classic" just doesn't cut it as a spec. I
think you want someone to reverse engineer what you posted and then
reengineer something else, IOW we have to assume there is no redundancy in
the black box action that could be extracted from what you posted. I think
that is, first of all, a bad way to proceed and second of all, I doubt if
anyone here will have the time and patience to even try it..

Post the spec. <spec><design><code> in *that* order. Top down and all that
good stuff.
 
J

James Kanze

There is nothing real special about the cloud diagrams, it was just
something that has virtually a zero learning curve - at least when presented
right - and is an aid to system design. You could look for something on UML
as has been suggested. A friend of mine teaches a one semester course on
UML at a local university which makes my wonder how much you might learn in
an hour or so, I don't know UML myself.

There's not that much difference between Booch and UML, except
that UML uses rectangles instead of clouds.
I think there is a good chance it could be simplified. But what are the
specifications for the elevator? "Classic" just doesn't cut it as a spec. I
think you want someone to reverse engineer what you posted and then
reengineer something else, IOW we have to assume there is no redundancy in
the black box action that could be extracted from what you posted. I think
that is, first of all, a bad way to proceed and second of all, I doubt if
anyone here will have the time and patience to even try it..
Post the spec. <spec><design><code> in *that* order. Top down and all that
good stuff.

It's good to hear someone say that.
 
W

whitehatmiracle

I think there is a good chance it could be simplified. But what are the
Sorry for not giving the complete info.
The output of the program should like this:

Enter the floor from 1 to 9, 0 to close the doors, EOF to quit: 2
Next floor: 5
Next floor: 0
The doors are closing
Elevator is going up!
Now on floor 2
The doors are open
Next floor: 4
Next floor: 0
The doors are clsing
Elevator is going up!
Passing floor 3
Now on floor 4
The doors are open
Next floor: 1
Next floor: 0
The doors are closing
Elevator is going up!
Now on floor 5
The doors are open
Next floor: 0
The doors are closing
Elevator is goign down!
Passing floor 4
Passing floor 3
Passing floor 2
Now on floor 1
The doors are open Next floor ^Z
ELevator being destroyed

And thnx a lot for booch and uml... thats somethign new i learnt :)
 
W

whitehatmiracle

Can i rip down panel and replace
void switch_on();
void switch_off();
bool is_off() ; //c
bool is_on() ; //c
by just two functions get_state and set_state.
I still couldnt figure out the cloud diagram....

class button
{
enum button_status {off, on};
public:
button(button_status = off);
void switch_on();
void switch_off();
bool is_off() ; //c
bool is_on() ; //c
private:
button_status state;



};


button::button(button_status s) : state(s) {}

void button::switch_on()
{
if(state == on)
cout << "Please be patient \n"; //it's already pushed
else
state = on;



}


void button::switch_off()
{
state = off;


}


bool button::is_off() //c
{
return state == off;


}


bool button::is_on() //c
{
return state == on;


}


class Panel
{
public:
Panel(int n);
~Panel();
void press(int n);
void reset(int n);
bool is_off(int n) const;
bool is_on(int n) const;
bool is_valid(int n) const;
private:
button** panel;
int number;
Panel(const Panel&);


};


Panel::panel(int n) : panel(0), number(n)
{
panel = new button*[number + 1];
for(int i = 0; i < number + 1; ++i)
panel = new button;


}


Panel::~Panel()
{
for(int i = number; i >= 0; --i)
delete panel;
delete [] panel;


}


bool Panel::is_off(int n) const
{
return panel[n]->is_off();


}


bool Panel::is_on(int n) const
{
return panel[n]->is_on();


}


bool Panel::is_valid(int n) const
{
return n != 13;


}


void Panel::press(int n)
{
if(!is_valid(n))
cout << "No such floor!\n";
else
panel[n]->switch_on();


}


void Panel::reset(int n)
{
panel[n]->switch_off();


}


class elevator
{
public:
elevator(int = 1, int = 0);
~elevator();
void prompt();
private:
Panel buttons;
int current_floor;
const int top_floor;
bool button_active(int) const;
void press(int);
void close_doors();
bool floor_is_valid(int) const;
elevator(const elevator&);


};


elevator::elevator(int n, int w) : buttons(n), current_floor(1),
top_floor(n) {}

elevator::~elevator()
{
cout << "Elevator will self destruct\n";



}


void elevator::prompt()
{
cout << "Key in the floor you would like to visit, from 1 to " <<
top_floor << ", 0 to close doors, EOF to exit: ";
int floor;
while(!(cin >> floor).eof())
{
if(floor == 0)
{
if(!button_active(1) && current_floor != 1)
press(1);
if(button_active(1))
close_doors();
}
else if(floor < 1 || floor > top_floor)
cout << "---Floor " << floor << " is not valid\n";
else if(floor == current_floor)
cout << "---You have reached your floor now\n";
else
press(floor);
cout << "Next floor: ";
}


}


void elevator::press(int n)
{
buttons.press(n);


}


void elevator::close_doors()
{
cout << "Doors are closing\n";
cout << "\a\a\a\a\a";
// If a button is pushed on a floor higher than the current
// floor, then the elevator always moves up.
if(button_active(current_floor))
cout << "Elevator accending\n";
else
cout << "Elevator decending\n";
sleep(1);
// Keep looping as long as the floor buton is off
while(buttons.is_off(current_floor))
{
if(button_active(current_floor))
++current_floor;
else
--current_floor;
if(buttons.is_off(current_floor) &&
floor_is_valid(current_floor))
{
cout << "\tPassing floor " << current_floor << '\a' << '\n';
sleep(1);
}
}
cout << "\tNow on floor " << current_floor << "\a\a\a" << '\n';
sleep(1);
buttons.reset(current_floor);
cout << "Doors are open\n";
sleep(1);


}


bool elevator::button_active(int f) const
{
for(int i = f; i <= top_floor; ++i)
if(buttons.is_on(i))
return true;
return false;


}


bool elevator::floor_is_valid(int f) const
{
return buttons.is_valid(f);


}


int main()
{
const int top = 9;
elevator otis(top);
otis.prompt();
return 0;
}
 
O

osmium

Can i rip down panel and replace
void switch_on();
void switch_off();
bool is_off() ; //c
bool is_on() ; //c
by just two functions get_state and set_state.

I am still waiting for a specification. The thing you posted only contained
sample messages printed by some elevator driven by stimuli I was not
entitled to see.

I have looked through your various posts trying to form a hypothetical
specification from what I see. This is what I come up with:

The elevator simulator simulates, in real time, [ I note the sleep(1)] the
operation of the only elevator in a nine story building. The building is
poorly designed and has only a single call button on each floor, rather than
the conventional "up" and "down" buttons. Furthermore, the elevator has a
prehistoric design in which the passengers, if any, must tell it to close
the door and do it's levitation thing. The simulator assumes one person
who frantically runs up and down the steps pressing or not pressing call
buttons at his whim. And so on.

Because of the real time thing and typing/thinking speed, I would prefer a
simulation in which the events are scheduled ahead of time.

Each entry would consist of a passenger id, a time stamp, current floor,
destination floor. And anything I forgot. The entries (ten or so
stuctures) would be in an stl::deque or perhaps read as needed from the
file. Would that be permissible? There would be no buttons and no panels.
This data would be entered into a file by a text editor and the file would
then be operated on by the program. Even better, would be a passenger
generator as I mentioned in an earlier post.

You are using the word "classical" too loosely. A classical elevator
simulator is used to determine service times and whether the building needs
more elevators and that kind of thing. Yours is more of a "toy elevator".
Not that there's anything wrong with that.

Quit stewing about the cloud diagrams. Some hugely complicated programs
were written well before cloud diagrams even existed.
 
W

whitehatmiracle

I am still waiting for a specification. The thing you posted only contained
sample messages printed by some elevator driven by stimuli I was not
entitled to see.
I have looked through your various posts trying to form a hypothetical
specification from what I see. This is what I come up with:

Ok sorry again here are the specifications:

The program, should simulate the movement of an elevator. There should
be a button class that abstracts a push button located inside the
levator. Use an enumerated type to represent its state - either
pressed or not pressed. Write implmentor function to change its state,
and an accessor function to retrieve the state.
There should be an elevator class that contains the following data
members:
A pointer to an array of button objects
AN integer representing which floor the elevator is currently on.
A const integer representing the top floor for the elevator. Its value
comes from the formal argument in the levator constructor finction.
Assume that the bottom floor is always 1.
Elevator class should contain member functions that allow the user to
press any number of valid buttons. 0 to close the elevator doors. Next
the elevator moves the the corresponding floor of whihc the button has
been pressed. When floor is reached, stop and prompt for more buttons.
while detreming which floor to go next, give priority to any floor
that is higher than the floor the elevator is currently on. EOF to
quit.

It can deviate a little here and there... nothing to strict.

I think the panel class is not necessary... how could i strip it down
and simplify the existing program.. cause it looks close to the
description.
Yours is more of a "toy elevator".

I agree.. a very very simple...very unrealistic model :)

SOS
 
O

osmium

Ok sorry again here are the specifications:

The program, should simulate the movement of an elevator. There should
be a button class that abstracts a push button located inside the
levator. Use an enumerated type to represent its state - either
pressed or not pressed. Write implmentor function to change its state,
and an accessor function to retrieve the state.
There should be an elevator class that contains the following data
members:
A pointer to an array of button objects
AN integer representing which floor the elevator is currently on.
A const integer representing the top floor for the elevator. Its value
comes from the formal argument in the levator constructor finction.
Assume that the bottom floor is always 1.
Elevator class should contain member functions that allow the user to
press any number of valid buttons. 0 to close the elevator doors. Next
the elevator moves the the corresponding floor of whihc the button has
been pressed. When floor is reached, stop and prompt for more buttons.
while detreming which floor to go next, give priority to any floor
that is higher than the floor the elevator is currently on. EOF to
quit.

It can deviate a little here and there... nothing to strict.

I think the panel class is not necessary... how could i strip it down
and simplify the existing program.. cause it looks close to the
description.

Now we're getting somewhere! I see no need for a panel Focus on this:
A pointer to an array of button objects

I think that's where you came up with the notion of a panel.

To implement that I would use something along these lines.
in elevator class, private,

*Button bpa;
..
In the ctor for Elevator, use new to set up an array that holds (nine?)
Buttons.

I think that you think (or something) of bpa as a "panel".

Firm up the practice of capitalizing the first letter of a class. Elevator,
Button, Panel. You are inconsistent.

Our communication problem was that I kept visualizing buttons that belong to
the *building*. There are none. A person just stands idly ion the floor
hoping that an elevator will come by before the he dies. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top