473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

The classic elevator simulation problem

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
12 11999
<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
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
<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
<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
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
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
<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
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
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
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
<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
Lets cee ill try coding it again... and get back to you

Jul 2 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Gary | last post by:
I am trying to do a simple "less than" conditional statement, and hitting a brick wall if I use a database element with it. This DOES work: <% if Request.QueryString("PC") < 10 then...
10
by: jack | last post by:
Hi guys, I am working on a project which requires an implementation of discrete event simulation in C using linked lists. I would greatly appreciate if someone could provide with some sources...
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
6
by: whitehatmiracle | last post by:
Hi all... Im getting some errors, and have no clue, as to what could be wrong... SOS... anybody ?? heres the code: // The elevator simulation //4 errors of 2 of same type //error, type...
0
by: Hypnotik | last post by:
My program is to simulate cache memory. I read in the info from 2 external files, 1) access 2) data in memory. When I read the information in I display the info...and it is all correct. However...
1
by: kantai | last post by:
I need a assistance in creating a program to model petrol station.The petrol station has six fuel pumps that are numbered from 1 to 6. Each fuel pump can only vend fuel of a particular type.Each...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.