//***ELEVATOR_MODEL.DEV***
//BUILDING.H
//The function-elements are in BUILDING.CPP
#ifndef BUILDING_H
#define BUILDING_H
#include <iostream.h>
#include <stdlib.h>
#include "elevator.h"
class building
{
public:
building(int=2);
int GetMaxLevel() const;
void SetMaxLevel(int);
elevator e1;
private:
int MaxLevel;
};
#endif
//BIULDING.CPP
//Function-elements of BUILDING class
#include <iostream.h>
#include <stdlib.h>
#include "building.h"
building::building(int l)
{
SetMaxLevel(l);
}
int building::GetMaxLevel() const
{
return MaxLevel;
}
void building::SetMaxLevel(int level)
{
if(level>1) MaxLevel=level; else MaxLevel=2;
}
//ELEVATOR.H
#ifndef ELEVATOR_H
#define ELEVATOR_H
#include <iostream.h>
#include <stdlib.h>
class elevator
{
public:
elevator();
void MoveTo(int);
int GetPosition() const;
void OpenDoor();
void CloseDoor();
private:
int position;
bool is_door_opened;
};
#endif
//ELEVATOR.CPP
#include <iostream.h>
#include <stdlib.h>
#include "elevator.h"
elevator::elevator()
{
position=1;
is_door_opened=false;
}
void elevator::MoveTo(int l)
{
if(l!=position)
{
if(is_door_opened==true) CloseDoor();
cout<<"Elevator has moved to level "<<l
<<" from level "<<position<<endl;
position=l;
OpenDoor();
}
else cout<<"!!Trying to set current level:no reaction"<<endl;
}
void elevator::OpenDoor()
{
is_door_opened=true;
cout<<"The Doors are opened"<<endl;
}
void elevator::CloseDoor()
{
is_door_opened=false;
cout<<"The Doors are closed"<<endl;
}
int elevator::GetPosition() const
{
return position;
}
//PASSENGER.H
#ifndef PASSENGER_H
#define PASSENGER_H
#include <iostream.h>
#include <stdlib.h>
#include "building.h"
#include "elevator.h"
class passenger
{
public:
passenger();
void SetBuilding(building &);
void CallElevator(); //Button
void ChoseLevel(int); //Button
void SetLocation(int);
private:
building b1;
int current_level;
bool allowed;
};
#endif
//PASSENGER.CPP
#include <iostream.h>
#include <stdlib.h>
#include "building.h"
#include "elevator.h"
#include "passenger.h"
passenger::passenger()
{
allowed=false;
}
//-------------------------------------------------------------------
void passenger::SetBuilding(building &house)
{
b1=house;
allowed=true;
current_level=1;
cout<<"The passenger is in the building"<<endl;
}
//---------------------------------------------------------------------
void passenger::SetLocation(int level)
{
if(allowed==true)
{
if( level<=( b1.GetMaxLevel() ) )
{
current_level=level;
cout<<"The passenger is on the "<<current_level<<" level"<<endl;
}
else cout<<"Passenger is Out of MaxLevel!"<<endl;
}
else cout<<"Set the building location of passenger first!"<<endl;
}
//-----------------------------------------------------------------------
void passenger::CallElevator()
{
if(allowed==true)
{
cout<<"The passenger has called the elevator"<<endl;
if(b1.e1.GetPosition()!=current_level) b1.e1.MoveTo(current_level);
else b1.e1.OpenDoor();
}
else cout<<"Set the building location of passenger first!"<<endl;
}
//-----------------------------------------------------------------------
void passenger::ChoseLevel(int l)
{
if(allowed==true)
{
if( l<=(b1.GetMaxLevel() ) )
{
cout<<"The passenger pushed button "<<l<<" in the elevator"<<endl;
b1.e1.MoveTo(l);
current_level=l;
}
else cout<<"Level is Out of MaxLevel!"<<endl;
}
else cout<<"Set the building location of passenger first!"<<endl;
}
//ELEVATOR_MODEL_DRIVER.CPP
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include "building.h"
#include "elevator.h"
#include "passenger.h"
using namespace std;
main()
{
const int i=5;
building office(i); //5-leveled building has been created
passenger worker1;
int start, finish, n; //the passenger will appear on level "start"
//and go to level "finish"
srand(time(NULL));
cin>>n; //n times passenger will appear on different levels
worker1.SetBuilding(office); //the passenger now is in a building "office"
for(int j=0; j<n; j++)
{
start=finish=(rand() % i) + 1;
while(start==finish){ finish=(rand() % i) + 1; }
cout<<endl;
worker1.SetLocation(start);
worker1.CallElevator();
worker1.ChoseLevel(finish);
}
system("PAUSE");
return 0;
}