Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <fstream>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8. class Elevator
  9. {
  10.     public:
  11.     Elevator();
  12.     Elevator( int );
  13.    
  14.     public:
  15.     void request( int );
  16.     int getCurrentFloor();
  17.    
  18.     private:
  19.     void goDown( int );
  20.     void goUp( int );
  21.    
  22.     public:
  23.     static const int BOTTOM_FLOOR;
  24.     static const int TOP_FLOOR;
  25.     int CurrentFloor, lowerFloor, higherFloor, newFloor;
  26. };
  27.  
  28. const int Elevator::BOTTOM_FLOOR = 1;
  29. const int Elevator::TOP_FLOOR = 15;
  30.  
  31. Elevator::Elevator()
  32. {
  33.     CurrentFloor = BOTTOM_FLOOR;
  34. }
  35.  
  36. Elevator::Elevator( int CurrentFloor )
  37. {
  38.     if( CurrentFloor > TOP_FLOOR || CurrentFloor < BOTTOM_FLOOR )
  39.     {
  40.         CurrentFloor = BOTTOM_FLOOR;
  41.     }
  42.     else
  43.         CurrentFloor = CurrentFloor;
  44. }
  45.  
  46.  
  47. int main()
  48. {
  49.     Elevator ele1;
  50.     Elevator ele2;
  51.    
  52.     Elevator object1;
  53.     object1.Elevator( 6 );
  54.  
  55.  
  56. }
  57.  
  58. void Elevator::request( int newFloor )
  59. {
  60.     if( newFloor > TOP_FLOOR || newFloor < BOTTOM_FLOOR)
  61.     {
  62.         cout<< "Error: Invalid floor selection. ";
  63.     }
  64.     else if( newFloor > CurrentFloor)
  65.     {
  66.         goUp(higherFloor);
  67.     }
  68.     else if(newFloor < CurrentFloor)
  69.     {
  70.         goDown(lowerFloor);
  71.     }
  72. }
  73.  
  74. int Elevator::getCurrentFloor()
  75. {
  76.     return CurrentFloor;
  77. }
  78.  
  79. void Elevator::goDown( int lowerFloor )
  80. {
  81.     cout<< "Starting at floor #  " << CurrentFloor;
  82.     while( CurrentFloor > newFloor )
  83.     {
  84.         CurrentFloor--;
  85.         cout<< "Going up -- now at floor #  " << CurrentFloor;
  86.     }
  87.     if( CurrentFloor == newFloor )
  88.     {
  89.         cout<< "Welcome to floor #  ";
  90.     }
  91. }
  92.  
  93. void Elevator::goUp( int higherFloor )
  94. {
  95.     cout<< "Starting at floor #  " << CurrentFloor;
  96.     while( CurrentFloor < newFloor )
  97.     {
  98.         CurrentFloor++;
  99.         cout<< "Going up -- now at floor #  " << CurrentFloor;
  100.     }
  101.     if( CurrentFloor == newFloor )
  102.     {
  103.         cout<< "Welcome to floor #  ";
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement