In: Computer Science
Elevator (C++)
Following the diagram shown below, create the class Elevator. An Elevator represents a moveable carriage that lifts passengers between floors. As an elevator operates, its sequence of operations are to open its doors, let off passengers, accept new passengers, handle a floor request, close its doors and move to another floor where this sequence repeats over and over while there are people onboard. A sample driver for this class is shown below. Each elevator request translates into just a single line of output shown below. You should probably make a more thorough driver to test your class better.
|
|
|||||
|
Elevator.cpp ------
#include<iostream>
using namespace std;
class Elevator
{
private:
int my_Floor;
int my_NumberOfPassengers;
bool my_DoorsOpen;
public:
Elevator();//constructor
void openDoors();//this function is
used to open the door of the elevator
void closeDoors();//this function
is used to close the door of the elevator
void letOffPassengers(int);//this
function is used to let off passengers form the lift
void requestFloor(int);//this
function is used to go to a floor
bool isOnFloor(int);//this function
checks whether the lift is in specified floor
//and if it is in the
floor then it returns true else returns false
int getFloor();//this function
returns the current floor number
int getPassengers();//this function
returns total number of passengers currently is in the
elevator
void
acceptPassengers(int);//accepts passengers in the elevator
};
void Elevator::requestFloor(int floor)
{
if(floor<0)
{
cout<<"Invalid
Floor\n";
return;
}
cout<<"Passengers want floor
"<<floor<<endl<<"Elevator moving to floor
"<<floor<<endl;
}
void Elevator::letOffPassengers(int amount)
{
my_NumberOfPassengers-=amount;
cout<<"Elevator has
"<<my_NumberOfPassengers<<" passengers\n";
}
void Elevator::acceptPassengers(int amount)
{
my_NumberOfPassengers+=amount;
cout<<"Elevator has
"<<my_NumberOfPassengers<<" passengers\n";
}
void Elevator::closeDoors()
{
my_DoorsOpen=false;
}
void Elevator::openDoors()
{
cout<<"Elevator Door's Open\n";
my_DoorsOpen=true;
}
Elevator::Elevator()
{
cout<<"\nElevator on Floor 1 with 0
passengers\n";
my_Floor=1;//initally the elevator is in the floor
1
my_NumberOfPassengers=0;//initially the elevator is
empty
my_DoorsOpen=false;//initiall the door of the elevator
is closed
}
int Elevator::getFloor()
{
return my_Floor;
}
int Elevator::getPassengers()
{
return my_NumberOfPassengers;
}
bool Elevator::isOnFloor(int floor)
{
if(floor==my_Floor)
return true;
else
return false;
}
int main()
{
Elevator e;
e.openDoors();
e.acceptPassengers( 5 );
e.requestFloor( 3 );
e.closeDoors();
e.openDoors();
e.letOffPassengers( 1 );
e.acceptPassengers( 2 );
e.requestFloor( 4 );
e.closeDoors();
e.openDoors();
e.letOffPassengers( 3 );
return 0;
}