473,544 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_s tatus = 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(in t 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(i nt n) const
{
return panel[n]->is_off();
}

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

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

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

void Panel::reset(in t 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(i nt) const;
void press(int);
void close_doors();
bool floor_is_valid( int) const;
elevator(const elevator&);
};

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

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

void elevator::promp t()
{
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_acti ve(1) && current_floor != 1)
press(1);
if(button_activ e(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_activ e(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.i s_off(current_f loor))
{
if(button_activ e(current_floor ))
++current_floor ;
else
--current_floor;
if(buttons.is_o ff(current_floo r) &&
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(c urrent_floor);
cout << "Doors are open\n";
sleep(1);
}

bool elevator::butto n_active(int f) const
{
for(int i = f; i <= top_floor; ++i)
if(buttons.is_o n(i))
return true;
return false;
}

bool elevator::floor _is_valid(int f) const
{
return buttons.is_vali d(f);
}

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

Jun 20 '07 #1
12 12010
<wh************ *@gmail.comwrot e:
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.comwrot e:
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.comwrot e:

[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...@com cast.netwrote:
<whitehatmira.. .@gmail.comwrot e:
[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 objektorientier ter Datenverarbeitu ng
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.comwrot e:
>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...@com cast.netwrote:
<whitehatmira.. .@gmail.comwrot e:
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 objektorientier ter Datenverarbeitu ng
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

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

Similar topics

13
2281
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 Response.Write("less than ten") else Response.Write("more than or equal to ten") End if %>
10
5236
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 on how to approach DES. Please help me out. Thanks, Jack
10
15978
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) 0.74
6
8750
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 qualifier 'elevator' must be a struct or clas name
0
2411
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 when I attempt to display the info in main anywhere the info is incorrect. I thought it might be a problem while I was reading the data in, but it...
1
2285
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 fuel pump has a space next to it in which a single vehicle can occupy when it is being fueled. There is also enough space for a queue of 5 vehicles to...
0
7414
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7598
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7757
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5895
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5288
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4905
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3398
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
651
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.