Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- class Elevator
- {
- public:
- Elevator();
- Elevator( int );
- public:
- void request( int );
- int getCurrentFloor();
- private:
- void goDown( int );
- void goUp( int );
- public:
- static const int BOTTOM_FLOOR;
- static const int TOP_FLOOR;
- int CurrentFloor, lowerFloor, higherFloor, newFloor;
- };
- const int Elevator::BOTTOM_FLOOR = 1;
- const int Elevator::TOP_FLOOR = 15;
- Elevator::Elevator()
- {
- CurrentFloor = BOTTOM_FLOOR;
- }
- Elevator::Elevator( int CurrentFloor )
- {
- if( CurrentFloor > TOP_FLOOR || CurrentFloor < BOTTOM_FLOOR )
- {
- CurrentFloor = BOTTOM_FLOOR;
- }
- else
- CurrentFloor = CurrentFloor;
- }
- int main()
- {
- Elevator ele1;
- Elevator ele2;
- Elevator object1;
- object1.Elevator( 6 );
- }
- void Elevator::request( int newFloor )
- {
- if( newFloor > TOP_FLOOR || newFloor < BOTTOM_FLOOR)
- {
- cout<< "Error: Invalid floor selection. ";
- }
- else if( newFloor > CurrentFloor)
- {
- goUp(higherFloor);
- }
- else if(newFloor < CurrentFloor)
- {
- goDown(lowerFloor);
- }
- }
- int Elevator::getCurrentFloor()
- {
- return CurrentFloor;
- }
- void Elevator::goDown( int lowerFloor )
- {
- cout<< "Starting at floor # " << CurrentFloor;
- while( CurrentFloor > newFloor )
- {
- CurrentFloor--;
- cout<< "Going up -- now at floor # " << CurrentFloor;
- }
- if( CurrentFloor == newFloor )
- {
- cout<< "Welcome to floor # ";
- }
- }
- void Elevator::goUp( int higherFloor )
- {
- cout<< "Starting at floor # " << CurrentFloor;
- while( CurrentFloor < newFloor )
- {
- CurrentFloor++;
- cout<< "Going up -- now at floor # " << CurrentFloor;
- }
- if( CurrentFloor == newFloor )
- {
- cout<< "Welcome to floor # ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement