Complete Elevator Simulation Program In C++

==================================
//Header File
/*
 * elevator.h
 * class declaration file for Elevator
 */

class Elevator
{
public:
    // Constructors

    Elevator::Elevator();
    Elevator::Elevator(int min, int max);

    // Access Member Function (4)

    int  Elevator::getCurrentFloor() const;
    int  Elevator::getMaxFloor() const;
    int  Elevator::getMinFloor() const;
    bool Elevator::getDoorStatus() const;

    // Set Member functions (4)

    void Elevator::setCurrentFloor(int currentFloor);
    void Elevator::setMinFloor(int minFloor);
    void Elevator::setMaxFloor(int maxFloor);
    void Elevator::setDoorStatus(bool doorStatus);

    void Elevator::setMinMaxFloors(int minFloor, int maxFloor);

    // Other Member Functions

    void Elevator::move(int destinationFloor);

    void Elevator::printDoorStatus();


=================================
//CPP File

/*
 * elevator.cpp
 * class implemenation file for Elevator class
 */

#include <iostream>

using namespace std

#include "elevator.h"

// Constructors

Elevator::Elevator()
{
    _min = 1;
    _max = 20;
}

Elevator::Elevator(int min, int max)
{
    _min = min;
    _max = max;
}

// Access Member Function (4)

int  Elevator::getCurrentFloor() const
{
    return(_CurrentFloor);
}

int  Elevator::getMaxFloor() const
{
    return(_MaxFloor);
}

int  Elevator::getMinFloor() const
{
    return(_MinFloor)
}

bool Elevator::getDoorStatus() const
{
    _DoorStatus = false;
    _DoorStatus = true;
}

// Set Member functions (4)

void Elevator::setCurrentFloor(int currentFloor)
{
    _CurrentFloor = currentFloor;
}

void Elevator::setMinFloor(int minFloor)
{
    _MinFloor = minFloor;
}

void Elevator::setMaxFloor(int maxFloor)
{
    _MaxFloor = maxFloor;
}

void Elevator::setDoorStatus(bool doorStatus)
{
    _DoorStatus = doorStatus;
}

void Elevator::setMinMaxFloors(int minFloor, int maxFloor)
{

}

// Other Member Functions

void Elevator::move(int destinationFloor)
{
    // check for valiation of the destinationFloor
    // call validateFloor(destination)



    if(destinationFloor > _currentFloor)
        goUp(destinationFloor);
    else if(destinationFloor < _currentFloor)
        goDown(destinationFloor);
    else
        printDestinationFloor();

    printDoorStatus();

    // print the door status
}

void Elevator::goUp(int destinationFloor)
{
    // for loop that goes up


}

void Elevator::goDown(int destinationFloor)
{
    // for loop that goes down
}

void Elevator::printDoorStatus()
{
    if(_doorOpen)
    {
        cout << "Open at floor " << _currentFloor << endl;
        _doorOpen = false;
    }
    else
    {
        cout << "Doors are Closing" << endl;
        _doorOpen = true;
    }
}

void Elevator::printDestinationFloor()
{
    decimals(cout, 2);
    cout << "The elevator is on floor " << DestinationFloor << "." << endl;
}

bool Elevator::validateFloor(int destinationFloor)
{
    _validateFloor = destinationFloor;
}

    void Elevator::printDestinationFloor();

    bool Elevator::validateFloor(int destinationFloor);

private:
    int  _currentFloor;
    int  _maxFloor;
    int  _minFloor;
    bool _doorOpen;

    void Elevator::goUp(int destinationFloor);
    void Elevator::goDown(int destinationFloor);
};





====================================================

Another Elevator Simulation Program In C Language


#include < stdio.h >
#include < conio.h >
#include < stdlib.h >
#include < conio.h >
#include < dos.h >
#include < ctype.h >
#include < math.h >

void showstatus( ) ;
void allotlift ( int *, int, int, char ) ;

/* show current status, i.e. the floor no. on which
the lifts are standing */
void showstatus ( int *lf )
{
int i ;

for ( i = 0 ; i < 5 ; i++ )
printf ( "Lift : %d is on %d floor\n", i, lf[i] ) ;
}

/* allot the lift to the user */
void allotlift ( int *lf, int flnos, int flnot, char d )
{
/* struct p to store the difference between 2 lifts */
struct p
{
int lfno[5] ;
int pos[5] ;
int diff[5] ;
} arr ;

int lno = -1 ; /* to store the lift no. that can be made available */
int i, j, k, t1, t2, t3 ;

/* initialize array */
for ( i = 0; i < 5; i++ )
{
arr.lfno[i] = i ;
arr.pos[i] = 0 ;
arr.diff[i] = 0 ;
}

while ( lno == -1 )
{
/* get the current position of a lift */
for ( i = 0 ; i < 5; i++ )
arr.pos[i] = lf[i] ;

for ( i = 0; i < 5; i++ )
{
/* get the diff. between floor on which lift is standing
and the floor on which the user is standing */
arr.diff[i] = abs ( arr.pos[i] - flnos ) ;
}

/* sort array */
for ( i = 0; i < 5; i++ )
{
for ( j = 0; j < 4; j++ )
{
if ( arr.diff[i] < arr.diff[j] )
{

t1 = arr.lfno[i] ;
t2 = arr.pos[i] ;
t3 = arr.diff[i] ;
arr.lfno[i] = arr.lfno[j] ;
arr.pos[i] = arr.pos[j] ;
arr.diff[i] = arr.diff[j] ;
arr.lfno[j] = t1 ;
arr.pos[j] = t2 ;
arr.diff[j] = t3 ;
}
}
}

/* check for the lift that can be alloted
depending on the direction */
for ( i = 0, k = 0; i < 5; i++ )
{
k = arr.lfno[i] ;

if ( ( d == 'u' ) && (
( lf[k] < flnos ) ||
( lf[k] == 0 ) ||
( lf[k] == flnos ) ) )
{
lno = k ;
break ;
}

if ( ( d == 'd' ) && (
( lf[k] > flnos ) ||
( lf[k] == 0 ) ||
( lf[k] == flnos ) ) )
{
lno = k ;
break ;
}
}

/* if none of the lift has been alloted */
j = 30 ;
if ( lno == -1 )
{
for ( i = 0; i < 5; i++ )
{
if ( j > arr.diff[i] )
{
j = arr.diff[i] ;
lno = i ;
}
}
}
}

printf ( “\nThe lift available to you would be: %d\n\n”, lno ) ;

lf[lno] = flnot ;
}

/* the main menu */
int showmenu( )
{
int c ;

printf ( ” ——————– Lift Program ——————– \n” ) ;
printf ( ” There are 5 lifts available for your service. \n” ) ;
printf ( ” Select 1 if you want to use a lift. \n” ) ;
printf ( ” Selecting 2 would show, for each of the 5 lifts, \n” ) ;
printf ( ” the floor no. on which the lift is currently standing. \n” ) ;
printf ( ” Select 3 to quit the program \n” ) ;
printf ( ” —————————————————— \n” ) ;
printf ( ” 1. Do you wish to use lift?\n” ) ;
printf ( ” 2. Show status of lift\n” ) ;
printf ( ” 3. Exit\n” ) ;
printf ( ” —————————————————— \n” ) ;
scanf ( “%d”, &c ) ;

return c ;
}

void validate ( int *fs, int *ft, char *d )
{
int flnos, flnot ;
char dir ;
int flag ;
unsigned char chr ;

flnos = flnot = -1 ;
flag = 0 ;

while ( flnos < 0 || flnos > 29 )
{
printf ( “Enter the floor no. where you are standing : ” ) ;
scanf ( “%d”, &flnos ) ;

if ( flnos < 0 || flnos > 29 )
{
printf ( “\nIncorrect floor no.! Enter floor no. in a range of 0 – 29 \n\n” ) ;
getch( ) ;
}
}

while ( flag == 0 )
{
dir = ‘j’ ;
while ( tolower ( dir ) != ‘u’ && tolower ( dir ) != ‘d’ )
{
printf ( “Do you wish to go up or down (up/down): ” ) ;
fflush ( stdin ) ;
scanf ( “%c”, &dir ) ;
printf ( “\n” ) ;

if ( dir != ‘u’ && dir != ‘d’ )
printf ( “Incorrect direction! Enter again\n” ) ;
}

if ( flnos == 0 && dir == ‘d’ )
{
printf ( “\nYou are already standing on ground floor.\n” ) ;
printf ( “Cannot go further down.\n” ) ;
getch( ) ;
flag = 0 ;
}
else
flag = 1 ;
}
flag = 0 ;

while ( flag == 0 )
{
printf ( “\nEnter the floor no. where you want to go: ” ) ;
scanf ( “%d”, &flnot ) ;

if ( ( flnos == 0 && flnot == 0 ) ||
( flnot < 0 || flnot > 29 ) ||
( flnos == flnot ) ||
( ( flnos != 0 ) && ( dir == ‘d’ && ( flnos – flnot < 0 ) ) ) ||
( ( flnos != 0 ) && ( dir == 'u' && ( flnot < flnos ) ) ) )
{
printf ( "\nIncorrect floor no. or trying to move to the same \n" ) ;
printf ( "floor where you are standing, or either the \n" ) ;
printf ( " direction given is incorrect or the floor number \n" ) ;
printf ( " is incorrect. Try again \n") ;
flag = 0 ;
}
else
flag = 1 ;
}

*fs = flnos ;
*d = dir ;
*ft = flnot ;
}

void main( )
{
int ch ; /* stores the no. entered as a choice for the menu */
int flnos ; /* to store floor no. where the user stands */
int flnot ; /* to store the target floor no. */
char dir ; /* to store the direction */
char chr[20] ; /* a temporary variable */
int lf[5] = { 0, 0, 0, 0, 0 } ;

void validate ( int *, int *, char * ) ;

flnos = flnot = -1 ;

clrscr( ) ;

while ( 1 )
{
/* display main menu */
ch = showmenu( ) ;

/* switch-case for menu options to be handled */
switch ( ch )
{
case 1 :

/* actual program logic */
printf ( “Allot lift: \n” ) ;
validate ( &flnos, &flnot, &dir ) ;
allotlift ( lf, flnos, flnot, dir ) ;
break ;

case 2 :

/* getstaus */
printf ( “Current status of lifts: \n” ) ;
showstatus ( lf ) ;
break ;

case 3 :

/* exit program */
exit ( 0 ) ;
break ;

default :

/* wrong choice */
printf ( “Incorrect choice! try again!” ) ;
}

printf ( “\n\n” ) ;
printf ( “Press key to continue…” ) ;
fflush ( stdin ) ;
scanf ( “%c”, &chr ) ;
}
}