The output has to be like above. my code: //elevator.h //header file //*** #incl
ID: 3644951 • Letter: T
Question
The output has to be like above.
my code:
//elevator.h
//header file
//***
#include <string>
using namespace std;
class Elevator //Elevator class definition
{
public:
Elevator( string name, int floor); //constructor initializes Elevator name and floor number
void setElevatorName( string name); //setting elevator name
void setElevatorFloor( int floor); //setting floor number
void addPassengers(int dest); //holding the passengers information
string getElevatorName(); //getting the elevator name
int getElevatorFloor(); //getting floor number
int findNextFloor();
int findNextPassenger(int floor);
void displayInfo(); //display message of the elevator name and current floor
void runElevator();
private:
string elevatorName; //elevator name
int elevatorFloor; //elevator floor
int passenger[5]; //array to hold 5 passengers
int numCurrentPassengers; //current passengers in the elevator
};
--------------------------------------------------------------------------------------------------
//elevator.cpp
#include "stdafx.h"
#include "elevator.h"
#include <string>
#include <iostream>
Elevator::Elevator( string name, int floor ) //initializes the elevator name and floor number
{
setElevatorName( name ); //validate and stores elevator name
setElevatorFloor( floor ); //validate and stores floor number
for ( int i=0; i<5; i++) //starting for-loop
{
passenger[i] = -1; //using -1 to see if there is any passengers in the elevator
}
numCurrentPassengers = 0;
}
void Elevator::setElevatorName( string name) //setting elevator name
{
elevatorName = name;
}
void Elevator::setElevatorFloor(int floor) //setting floor number
{
if( floor <=50 && floor >= 1) //if statement to check that the value between 1-50
{
elevatorFloor = floor;
}
else
{
cout << "invalid floor selection, try again..." << endl << endl; //error message if the value less than 1 or more than 50
}
}
string Elevator::getElevatorName() //get the elevator name
{
return elevatorName;
}
int Elevator::getElevatorFloor() //get floor number
{
return elevatorFloor;
}
void Elevator::displayInfo() //display message for the elevator name and current floor number
{
cout << "" << getElevatorName() << " - Current Floor - " << getElevatorFloor() << endl;
for ( int i=0; i<5; i++)
{
if (passenger[i] == -1) //displays the current passengers destination
{
cout << "Spot " << i+1 << " is empty" << endl;
}
else
{
cout <<"Passenger " << i+1 << "`s destination is " << passenger[i] << endl;
}
}
}
void Elevator::addPassengers( int dest) //adding passengers to the elevator
{
if (dest<=50 && dest>=1) //set passengers destination between 1-50
{
if (numCurrentPassengers > 4) //check if passengers more than 4 to display error message
{
cout << " Elevator is full. please wait..." << endl << endl;
system("pause");
}
else
{
passenger[numCurrentPassengers] = dest;
numCurrentPassengers++;}
}
else
{
cout << "invalid destination selection, try again..." << endl << endl;
system("pause");
}
}
--------------------------------------------------------------------------------------------------
// lab2-code.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "elevator.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[]) //main function
{
int floor = 1; //decleration of variables
int add ;
int number;
int dest;
char ch;
Elevator myElevator( "South Elevator", floor); //pass elevator name and floor number to contructor
while ( 1 ) //while-loop
{
myElevator.setElevatorFloor(floor); //resetting the floor number to new value
myElevator.displayInfo(); //display the elevator name and passengers destination with the new floor number
cout << " "; //printing new lines
cout << "Elevator Menu" << endl; //prints "elevator menu
cout << "1. Change floor" << endl; //prints first choice
cout << "2. Add passenger " << endl; //prints second choice
cout << "what you would like to do ? "; //asking the user to choose from the list
cin >> ch; //getting user choice
switch (ch) // switch the user choice to the following
{
case '1': //when choosing 1, it will ask for new floor
cout << "Which floor do you want to go?"; //asking for floor number
cin >> number; //get number from user
floor=number; //assign number to floor
break;
case '2': //when choosing 2, it will add new passenger with the destination
cout << "What is the passenger destination?"; //ask for destination
cin >> add; //get destination
dest = add; //assign destination number to the variable dest
myElevator.addPassengers( add ); //add new passenger with the destination
break;
}
system("cls"); //clear screen
}
return 0;
}
--------------------------------------------------------------------------------------------------------
the instruction which I did not understand it:
Before writing the actual ?run? function of the elevator, write two functions for the elevator class that will be necessary for the run function to operate correctly. First, you will need to be able to search the array of passenger?s destinations and return the next floor the elevator needs to stop at. Next, you will need to be able to search the passenger array and see which passenger(s) need to exit at the current stop.
1. Clean up the code from last time by creating a #define for the maximum number or passengers. This helps by allowing you to change a number in one location, rather than all over your code to modify the maximum number of passengers. In ?Elevator.h? after the #include, add #define MAX_PASSENGERS 5. Then, everywhere that the number 5 was used in loops and if statements, change it to MAX_PASSENGERS.
2. Add a function to the Elevator class definition called ?findNextFloor?. This function does not need any input arguments, but should return the integer value of the next floor, based on the passenger destinations.
3. Add a function to the Elevator class definition called ?findNextPassenger?. This function requires an input argument of the current floor of the elevator. It should return the index of the passenger array of a passenger that needs to get off at that floor passed as the input argument. If no passengers are getting off at this floor, it should return the value -1.
4. Next, switch to the ?Elevator.cpp? file to add the implementation of these two functions.
5. The function ?findNextFloor? needs to search through the passenger[ ] array and find which passenger has the lowest destination floor. This value needs to be returned from the function. This can be accomplished with a FOR-LOOP incrementing through each index of the array, keeping track of the smallest value.
6. The function ?findNextPassenger? needs to have an input argument of a certain floor. The function will search through the passenger array, and when it finds a passenger whose destination matches the input floor, it will return that passenger?s index in the array. This can be accomplished with a FOR-LOOP also that increments through each index of the array until it finds a match and returns that index. If no match is found the function should return -1, signaling that no more passengers are getting off at this floor.
7. Now, go back to the Elevator class definition and add a function called ?runElevator?. This function does not need any input arguments or return value.
8. Switch back to the Elevator class implementation to implement the ?runElevator? function. This is the function that will deliver all the passengers to their desired floors. Take a look at the pseudo code below to help visualize the approach to doing this
Ifthere are passengers
While there are passengers
-find the next floor to stop at
-move the elevator to that floor
-display a message saying the next stop
While there are passengers that need to exit at this floor
-find the next passenger that needs to exit
-display a message saying which person exited
-mark that spot in the elevator as empty
-decrement the number of passengers
-display the current elevator information
-reset elevator to first floor
Else
Display an error message saying there are no passengers
9. After creating the ?runElevator? function, go to the main function and add a third menu option for running the elevator. This is as simple as calling your Elevator class function ?runElevator? after the user selects option 3. Also, test when you change MAX_PASSENGERS to a different number.
Explanation / Answer
/ Building class definition. #ifndef BUILDING_H #define BUILDING_H #include "elevator.h" // Elevator class definition #include "floor.h" // Floor class definition #include "clock.h" // Clock class definition #include "scheduler.h" // Scheduler class definition class Building { public: Building(); // constructor ~Building(); // destructor void runSimulation( int ); // controls simulation private: Floor floor1; // floor1 object Floor floor2; // floor2 object Elevator elevator; // elevator object Clock clock; // clock object Scheduler scheduler; // scheduler object }; // end class Building #endif // BUILDING_H
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.