By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

The classic elevator simulation problem

P: n/a
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[i] = new button;
}

Panel::~Panel()
{
for(int i = number; i >= 0; --i)
delete panel[i];
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;
}

Jun 20 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
<wh*************@gmail.comwrote:
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.
Jun 20 '07 #2

P: n/a
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

Jun 20 '07 #3

P: n/a
<wh*************@gmail.comwrote:
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
>cables break.

LOL :)
That was in response to your name for the elevator. I think that was my
favorite part of the whole program :)
Jun 20 '07 #4

P: n/a
<wh*************@gmail.comwrote:

[I'll] try and look for some
cloud diagrams too.
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.

http://www.amazon.com/Mastering-Obje...346862&sr=1-14.
Jun 20 '07 #5

P: n/a
On Jun 20, 3:48 pm, "osmium" <r124c4u...@comcast.netwrote:
<whitehatmira...@gmail.comwrote:
[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.
http://www.amazon.com/Mastering-Obje...C%2B%2B-Horstm....
I've not seen the book, but seeing who the author is makes me
want to buy it. Horstman is good.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 20 '07 #6

P: n/a
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?

Jun 22 '07 #7

P: n/a
<wh*************@gmail.comwrote:
>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><codein *that* order. Top down and all that
good stuff.
Jun 22 '07 #8

P: n/a
On Jun 22, 4:04 am, "osmium" <r124c4u...@comcast.netwrote:
<whitehatmira...@gmail.comwrote:
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.
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><codein *that* order. Top down and all that
good stuff.
It's good to hear someone say that.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 22 '07 #9

P: n/a
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..
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 :)

Jun 22 '07 #10

P: n/a
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[i] = new button;
}
Panel::~Panel()
{
for(int i = number; i >= 0; --i)
delete panel[i];
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;
}

Jun 26 '07 #11

P: n/a
<wh*************@gmail.comwrote:
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. :-)

Jun 28 '07 #12

P: n/a
Lets cee ill try coding it again... and get back to you

Jul 2 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.