////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// // // Your Name : Joe Programmer (joe@andrew.cmu.edu) // Course/Section : 46-699 MSCF- OOP I // Project # : Two // // Description : // // INTRO TO CLASSES - ELEVATOR CONTROL SYSTEM // write your description here // // Last Modified : // // Known Bugs : // ////////////////////////////////////////////////////////////////////////////// // How the current program works. // 1. It creates 3 elevators and put them on random floors // 2. It creates 20 passengers with unique id's and put them on random floors // 3. Program shows the initial status of elevators and passengers // 4. Program moves elevators(in order) to next floor where service was requested // 5. Elevators pick up passengers from those floors (if any) // 6. Program shows the current status of the elevators // TASK ONE : What you need to do // 1. After picking the passengers, move the elevators to the next floor // 2. Drop the passengers off to that floor (if any) // 3. Pick the passengers from that floor (if any) // 4. Do 1,2,3 for all 3 elevators until no more passengers. Program will terminate at // this point. Check project2.out to make sure things worked out OK // 5. Imporve pickPassengers procedure to do the following. If elevator has no passengers // determine the maximum number of passengers waiting on that floor in up or down // direction. Pick up the group with the most number. If there is a tie pick the group // wanting to go up. Note that current pickPassenger algorithm determines(in the case // of no passengers on the elevator) the direction based on the passenger with the // least id waiting on that floor. // EXTRA TASK(optional): Need Extra Credit? // 1. Create passengers dynamically (20 initially and then 2 after every iteration // one iteration means - move(Task One-1), drop(Task One-2), pick(Task One-3) for // all three elevators // 2. Use an internal linked list to keep the current passengers for each elevator // 3. You must destroy the node after passenger gets off // 4. One iteration means - move, drop, pick, for 3 elevators and create 2 random // passengers with unique id's ////////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include using namespace std; const MaxFloors = 8; const int MaxPassengers = 20; bool globalFloorList[MaxFloors]={false}; // global array to hold elevator requests class Elevator { friend class Passenger; enum direction {up,down,idle} ; public : Elevator(); // default constructor Elevator(short id, short floor=0); // specific constructor short getFloor() const; // returns the current floor void showStatus(ofstream &) const; // returns the current elevator floor and direction // to outfile or console void pickPassengers(Passenger []); // pick passengers from the passenger array void moveToNextFloor(); // moves to the next floor (details below) void dropPassengers(Passenger []); // Drop the passengers and change their status private: short currentFloor; // stores current floor number short currentPassengers; // stores current passenger total short id; // stores elevator ID direction direct; // Elevator direction bool floorList[MaxFloors]; // internal floor list array - keeps track of // where people who are on the Elevator want to go bool passengerList[MaxPassengers];// ID's of the passengers who are on the elevator }; class Passenger{ friend class Elevator; enum status {waiting, onBoard, served}; public : Passenger(){}; // default constructor (details below) Passenger(int id, short origin, short destination); // specific constructor void SetPassenger(int id, short origin, short destination); // changes passenger status void callElevator() const; // calls the closest elevator void ShowStatus(ofstream &) const; // shows the status of the passenger (which elevator, floor, direction) void FloorNum() const; // returns current floor number of the passenger private: int id; // passenger ID short elevator; // ID of passenger elevator short origin; // origin floor short destination; // destination floor Elevator::direction direct; status myStatus ; static int passengerCount; }; // A utility function to delay action by n seconds // May be used to create a realistic simulation void waitSeconds(int n){ long then=time(0), now =time(0); while (now-then<=n) now=time(0); return; } //////////////////////////////////////////////////// void main() { srand(time(0)); ofstream outfile("project2.out",ios::out); Elevator ElevatorOne(1, rand()%MaxFloors); Elevator ElevatorTwo(2, rand()%MaxFloors); Elevator ElevatorThree(3,rand()%MaxFloors); Passenger PassengerList[MaxPassengers]; // create an array of passenger objects bool idList[MaxPassengers]={false}; // used to check for duplicate passenger id's //create unique id's for passengers and random origins and destinations for (int i=0; i=0) i--; if (i<0) // no one wants the service, move to the bottom floor and wait {currentFloor=0; direct = up;} else if (floorList[i]==true) {currentFloor = i;floorList[i]=false; direct=down;} else if (globalFloorList[i]== true) {currentFloor = i;} } } // If the elevator is moving down check to see if anyone is waiting below or anyone who // is on the elevator wants to go to a lower floor else if (direct == down) {while (floorList[i]==false && globalFloorList[i]==false && i>=0) i--; if (floorList[i]==true) {currentFloor = i; floorList[i]=false;} if (globalFloorList[i]==true) {currentFloor = i;} // move the elevator to that floor, dont pick up passengers if (i<0) // no one is waiting below check in the up direction { i=currentFloor; while (floorList[i]==false && globalFloorList[i]==false && i0) {list[i].myStatus=Passenger::onBoard; floorList[list[i].destination]=true; currentPassengers++; // increment the number of passengers in this elevator passengerList[list[i].id]=true;//elevator's internal passenger array knows you are in } else // pick up the passengers who want to go down if (direct==down && list[i].destination-list[i].origin<0) {list[i].myStatus=Passenger::onBoard; floorList[list[i].destination]=true; currentPassengers++; passengerList[list[i].id]=true; } else // elevator can go either way. Passenger with the smallest id waiting on // that floor will decide which way to go { if (list[i].destination-list[i].origin>0) direct = up; else direct = down; list[i].myStatus=Passenger::onBoard; floorList[list[i].destination]=true; currentPassengers++; passengerList[list[i].id]=true; } // all passengers from current floor picked up? if so Turn off the lamp globalFloorList[currentFloor]=false; for ( i=0;i0) direct = Elevator::down; else direct = Elevator::up; myStatus = waiting; } /////////////////////////////////////////////////////////////////////////// // Method : setPassenger // Type : Mutator // Class : Passenger // Algorithm : Changes the status of a passenger ///////////////////////////////////////////////////////////////////////////// void Passenger::SetPassenger(int id, short origin, short destination){ Passenger::id = id; Passenger::origin = origin; Passenger::destination = destination; if (origin == 0) direct = Elevator::up; else if (origin==MaxFloors-1) direct = Elevator::down; else if (origin-destination>0) direct = Elevator::down; else direct = Elevator::up; myStatus = waiting; } /////////////////////////////////////////////////////////////////////////// // Method : setPassenger // Type : Accessor // Class : Passenger // Algorithm : Shows the current status of a passenger ///////////////////////////////////////////////////////////////////////////// void Passenger::ShowStatus(ofstream &outfile) const { outfile<<"\nPassenger "< 0) outfile <<" up"; else outfile<<" down"; } else if (myStatus==served) {outfile<<"already being served and is on floor "<