Question: Please rewrite cpp and h files as following orders. Thank you. Please use this suggested driver program for V5 testing, ElevatorV5.cpp #include #include #include using

Please rewrite cpp and h files as following orders. Thank you.

Please use this suggested driver program for V5 testing,ElevatorV5.cpp

#include  #include  #include  using namespace std; #include  #include  #include "Rider.h" #include "Building.h" #include "Floor.h" #include "Panel.h" #include "Elevator.h" int main() { cout << "Programmer: Elevator Simulation V5 " << "Programmer's ID: 1234567 " << "File: " << __FILE__; srand(time(0)); rand(); for (int i = 0;; i++) { cout << "----------------------------------------------- "; cout << "-- Time " << i << " --------------------------- "; for (int elevator = 0; elevator < Building::ELEVATORS; elevator++) cout << Building::elevators[elevator] << endl; for (int floor = 0; floor < Building::FLOORS; floor++) cout << Building::floors[floor] << endl; double myArrivalRate = 1; // you choose! Building::action(i < 300 ? myArrivalRate : 0); cin.get(); } }

Progress And Status

Going back to our elevator logic in Module 8 ~ 9, let's look at the progress so far and where we have yet to go. The first five items are done. In this assignment we're adding the ability for elevators to close their doors and start moving.

  1. Open their door if there is a rider to disembark.DONE in v.1 in Module 8
  2. Disembark a rider if the door is open and there's a rider who wants to get off.DONE in v.2 in Module 9
  3. Board a rider if the door is open and a rider wants to board.DONE in v.2 in Module 9
  4. Hold the door open for a few seconds in case more riders show up.DONE in v.3 in Module 13
  5. After waiting a few seconds, and there are riders on the elevator, close the door and get moving.DONE in v.3 in Module 13
  6. If the elevator is already between floors, keep moving -- stop at a floor if its panel button is pressed.
  7. If an elevator is idle and the up/down button is pressed, open the door for the rider to board.
  8. If there's a floor with a lit up/down button, close the door and start moving there.
  9. Close the door and go idle.

Requirements

We've all been working with a building design with 5 floors and 3 identical elevators. Now it's time foryouto customize your building!

  1. InBuilding.cpp, customize6 or more floors, at or above or below ground level. We're not building a doll house, so make sure your elevations (in inches) are reasonable for human beings.
  2. Customize4 or more elevators, so they are not all alike in terms of capacity and speed. Use your imagination -- consider small, fast express elevators and slow, large freight elevators in the mix.
  3. Remove "seeds". There's no more need for riders starting on elevators at time zero. Remove the VisualBuilding's constructor and the console app's code block that puts riders on elevators before the simulation begins.

Console Elevator

If you are running the console version, set the rider arrival rate to a value that "works well" with your building. That is, the rate should not be so small that elevators spend a lot of time idle, and the rate should not be so large that riders accumulate on floors in large crowds. Also, start arrivals at time zero and end after 300 seconds (5 minutes), like this in the main CPP:

 double myArrivalRate = ...; // you choose! Building::action(i < 300 ? myArrivalRate : 0); 

Visual Elevator

It isNOT required that you do a GUI versionof this assignment. But if you do, this is what you should see after the simulation runs for a while and you lower the rider arrival rate to zero and wait for the elevators to stop moving -- something like this, with elevators idle at top and/or bottom floors, and no remaining riders anywhere(click image to download Windows animated EXE).

CPPs and H Files

Source code for Elevator.cpp:

#include "Building.h" #include "Elevator.h" #include "Panel.h" #include "Rider.h" #include  #include  using namespace std; #include  Elevator::Elevator(unsigned int capacity, int speed, int start) :speed(speed), CAPACITY(capacity), location(Building::floors[start]) { start = Building::floors[start]; direction = IDLE; timer = 0; atFloorIndex = -1; for(int i = 0; i < Building::FLOORS; i++) { panel.addButton(Building::floors[i].label); } } ostream& operator<<(ostream& out, const Elevator& e) { cout << "Elevator at "; cout.width(5); cout << e.location; cout.width(11); switch(e.direction) { case 1: cout << "going UP"; break; case -1: cout << "going DOWN"; break; case 0: cout << "IDLE"; break; } cout.width(3); cout << e.riders.size() << " riders"; cout.width(16); if(e.atFloorIndex != -1) cout << " door is OPEN|" << e.timer; else { cout.width(15); cout << " "; } cout << " " << e.panel; return out; } void Elevator::openDoorTo(int floorindex) { this->panel.clear(Building::floors[floorindex].label); this->atFloorIndex = floorindex; this->resetTimer(); } void Elevator::board(const Rider& r) { this->riders.push_back(r); // add rider //string to = Building::floors[r.to].label; this->panel.press(Building::floors[r.to].label); // press button if(r.goingUp) // set direction of elevator this->direction = this->UP; else if(r.goingDown) this->direction = this->DOWN; else throw "BOARD ERROR!"; this->resetTimer(); // reset close door timer } bool Elevator::hasRiderForFloor() const { if(atFloorIndex == -1) return false; for(int i = 0; i < riders.size(); i++) if(riders[i].to == atFloorIndex) return true; // find rider for floor return false; // end of function } void Elevator::removeRider() { vector:: iterator it; for(it = riders.begin(); it != riders.end(); it++) { if(it->to == atFloorIndex) { this->panel.clear(Building::floors[it->to].label); it = this->riders.erase(it); this->resetTimer(); break; } } } bool Elevator::closeDoor() { if(atFloorIndex == -1) return false; else { if(this->goingUp() && !Building::floors[atFloorIndex].hasUpRider()) Building::floors[atFloorIndex].panel.clear(Building::floors[atFloorIndex].UP); else if(this->goingDown() && !Building::floors[atFloorIndex].hasDownRider()) Building::floors[atFloorIndex].panel.clear(Building::floors[atFloorIndex].DOWN); } this->atFloorIndex = -1; return true; } bool Elevator::move() { if(this->direction == this->UP) { for(int i = 0; i < Building::FLOORS; i++) { if(Building::floors[i].elevation < this->location || Building::floors[i].elevation > this->location + speed) continue; else if((Building::floors[i].panel.isLit(Floor::UP) || Building::floors[i].panel.isLit(Building::floors[i].label)) && this->location != Building::floors[i].elevation) { this->location = Building::floors[i].elevation; this->openDoorTo(i); return true; } } if(Building::floors[Building::FLOORS - 1].elevation <= this->location + speed) { this->location = Building::floors[Building::FLOORS - 1].elevation; this->atFloorIndex = Building::FLOORS - 1; return false; } else { this->location += speed; return true; } } else if(this->direction == this->DOWN) { for(int i = 0; i < Building::FLOORS; i++) { if(Building::floors[i].elevation > this->location || Building::floors[i].elevation < this->location - speed) continue; else if((Building::floors[i].panel.isLit(Floor::DOWN) || Building::floors[i].panel.isLit((Building::floors[i].label))) && this->location != Building::floors[i].elevation) { this->location = Building::floors[i].elevation; this->openDoorTo(i); return true; } } if(Building::floors[0].elevation >= this->location - speed) { this->location = Building::floors[0].elevation; this->atFloorIndex = 0; return false; } else { this->location -= speed; return true; } } return false; }

Source code for Elevator.h:

#ifndef Elevator_H #define Elevator_H #include  using namespace std; #include "Rider.h" #include "Floor.h" #include "Panel.h" class Elevator { static const int IDLE = 0; static const int UP = 1; static const int DOWN = -1; // elevator metrics int location; // inches above ground level int direction; // equal to IDLE, UP, DOWN int atFloorIndex; // -1 if door is closed int timer; vector riders; public: const int speed; // inches per second, up or down const unsigned int CAPACITY; Elevator(unsigned int, int, int); operator int() const {return location;} bool isOpen() const {return atFloorIndex >= 0;} void openDoorTo(int); // parameter is index in Building::floors array void board(const Rider&); // adds a Rider to the Elevator bool hasRiderForFloor() const; void removeRider(); bool isFull() const {return CAPACITY == riders.size();} bool goingUp() const {return direction == UP;} bool goingDown() const {return direction == DOWN;} int getFloorIndex() const {return atFloorIndex;} unsigned int getNumberOfRiders() const {return riders.size();} bool isIdle() const { return direction == 0 ? true : false;} bool closeDoor(); bool move(); void goIdle() {direction = IDLE; closeDoor();} void setDirectionUp() {direction = UP;} void setDirectionDown() {direction = DOWN;} // count-down timer before closing door void resetTimer() {timer = 3;} void tickTimer() {timer--;} bool timedOut() const {return timer == 0;} Panel panel; // friend function friend ostream& operator<<(ostream&, const Elevator&); }; #endif

Source code for Building.cpp:

#include "Building.h" #include "Floor.h" #include "Elevator.h" #include  using namespace std; #include  #include  Floor Building::floors[] = { Floor(0, "First Floor", "1"), Floor(100, "Second Floor", "2"), Floor(200, "Third Floor", "3"), Floor(305, "Fourth Floor", "4"), Floor(410, "Fifth Floor", "5"), Floor(515, "Sixth Floor", "6") }; const int Building::FLOORS = sizeof(floors) / sizeof(Floor); Elevator Building::elevators[] = { Elevator(12, 5, 0), Elevator(12, 5, 1), Elevator(12, 5, 2), Elevator(12, 5, 2) }; const int Building::ELEVATORS = sizeof(elevators) / sizeof(Elevator); void Building::getDifferentInts(int& a, int& b) { do { a = rand() % FLOORS; // range is 0 to (FLOORS-1) b = rand() % FLOORS; // range is 0 to (FLOORS-1) } while (a == b); // try again if they are the same } int Building::getPoisson(double avg) { int arrivals = 0; double probOfnArrivals = exp(-avg); // requires cmath for(double randomValue = (rand() % 1000) / 1000.0; // requires cstdlib (randomValue -= probOfnArrivals) > 0.0; probOfnArrivals *= avg / ++arrivals); return arrivals; } void Building::placeNewRiders(double arrivalsPerSecond) { int n = getPoisson(arrivalsPerSecond); for (int i = 0; i < n; i++) { int from, to; getDifferentInts(from, to); Rider rider(from, to); floors[from].addRider(rider); } } bool Building::openDoorToDisembarkRider(int e) { // if closed and floor button is lit... //...and there are riders to disembark, open door if (!elevators[e].isOpen()) for (int f = 0; f < FLOORS; f++) if (floors[f] == elevators[e] && elevators[e].panel.isLit(floors[f].label)) { elevators[e].openDoorTo(f); return true; } return false; } void Building::action(double arrivalsPerSecond) { placeNewRiders(arrivalsPerSecond); // one action per elevator for (int e = 0; e < ELEVATORS; e++) { if (openDoorToDisembarkRider(e)) continue; // lab 8 if (disembarkRider(e)) continue; // lab 9 if (boardRider(e)) continue; // lab 9 if (waitingForMoreRiders(e)) continue; // lab 12 if (doneWaitingMove(e)) continue; // lab 12 if (moveableMove(e)) continue; // lab 14 if (setIdleElevatorInMotion(e)) continue; // lab 15 if (sendIdleElevatorToCallingFloor(e)) continue; // lab 15 elevators[e].goIdle(); // nothing to do } } bool Building::disembarkRider(int e) // lab 9 { // if open and rider to disembark, do that if (elevators[e].isOpen() && elevators[e].hasRiderForFloor()) { elevators[e].removeRider(); return true; } return false; } bool Building::boardRider(int e) // lab 9 { // if door is open and not full and rider to load, load if (elevators[e].isOpen() && !elevators[e].isFull()) { Floor& floor = floors[elevators[e].getFloorIndex()]; if (elevators[e].goingUp() && floor.hasUpRider()) { Rider rider = floor.removeUpRider(); elevators[e].board(rider); return true; } else if (elevators[e].goingDown() && floor.hasDownRider()) { Rider rider = floor.removeDownRider(); elevators[e].board(rider); return true; } } return false; } bool Building::waitingForMoreRiders(int e) // lab 12 { if(elevators[e].isOpen() && !elevators[e].isIdle() && !elevators[e].timedOut()) { elevators[e].tickTimer(); return true; } return false; } bool Building::doneWaitingMove(int e) // lab 12 { if(!elevators[e].isOpen() || elevators[e].isIdle() || !elevators[e].timedOut()) return false; else { elevators[e].closeDoor(); elevators[e].move(); return true; } } bool Building::moveableMove(int e) // lab 14 { if(elevators[e].isOpen() || elevators[e].isIdle()) return false; else return elevators[e].move(); } bool Building::setIdleElevatorInMotion(int e) { if(elevators[e].isIdle()) { for(int i = 0; i < FLOORS; i++) { if(elevators[e] == floors[i]) { if(floors[i].panel.getFirstLit() == Floor::UP) { elevators[e].openDoorTo(i); elevators[e].setDirectionUp(); return true; } else if(floors[i].panel.getFirstLit() == Floor::DOWN) { elevators[e].openDoorTo(i); elevators[e].setDirectionDown(); return true; } } } } return false; } bool Building::sendIdleElevatorToCallingFloor(int e) { if(elevators[e].isIdle()) { for(int i = 0; i < FLOORS; i++) { if(floors[i].hasDownRider() || floors[i].hasUpRider()) { if(floors[i] > elevators[e]) { elevators[e].setDirectionUp(); elevators[e].move(); return true; } else if(floors[i] < elevators[e]) { elevators[e].setDirectionDown(); elevators[e].move(); return true; } } } } return false; }

Source code for Building.h:

#ifndef BUILDING_H #define BUILDING_H #include "Floor.h" #include "Elevator.h" struct Building { static Floor floors[]; static Elevator elevators[]; static const int FLOORS; static const int ELEVATORS; static void getDifferentInts(int&, int&); static int getPoisson(double); static void action(double); // actions static void placeNewRiders(double); static bool openDoorToDisembarkRider(int); static bool disembarkRider(int); // lab 9 static bool boardRider(int); // lab 9 static bool waitingForMoreRiders(int); // lab 12 static bool doneWaitingMove(int); // lab 12 static bool moveableMove(int); // lab 14 static bool setIdleElevatorInMotion(int); // lab 14 static bool sendIdleElevatorToCallingFloor(int); // lab 15 }; #endif

Source code for Floor.cpp:

#include "Building.h" #include "Floor.h" #include "Rider.h" #include "Panel.h" #include  #include  #include  #include  using namespace std; #include  const char* const Floor::UP = "Up"; const char* const Floor::DOWN = "Down"; int Floor::TOP = INT_MIN; // defined in climits int Floor::BOTTOM = INT_MAX; // also in climits Floor::Floor(const int elevation, const char* const name, const char* const label) :name(name), label(label), elevation(elevation) { panel.addButton(UP); panel.addButton(DOWN); if (TOP < elevation) TOP = elevation; if (elevation < BOTTOM) BOTTOM = elevation; } ostream& operator<<(ostream& out, const Floor& floor) { out.width(3); out << floor.label; out.width(15); out << floor.name << " at"; out.width(6); out << floor.elevation; out << "\" Up/Down:"; out.width(4); out << floor.upRiders.size() << "/" << floor.downRiders.size() << " Button: " << floor.panel; return out; } void Floor::addRider(const Rider& r) { if(r.goingUp) { this->upRiders.push(r); panel.press(UP); } else if(r.goingDown) { this->downRiders.push(r); panel.press(DOWN); } else throw "ADD_RIDER ERROR!"; } Rider Floor::removeUpRider() { if(this->upRiders.empty()) throw "UP_RIDER EMPTY!"; Rider r = this->upRiders.front(); this->upRiders.pop(); return r; } Rider Floor::removeDownRider() { if(this->downRiders.empty()) throw "DOWN_RIDER EMPTY!"; Rider r = this->downRiders.front(); this->downRiders.pop(); return r; }

Source code for Floor.h:

 #ifndef FLOOR_H #define FLOOR_H #include  #include  #include  using namespace std; #include "Panel.h" #include "Rider.h" class Floor { queue upRiders, downRiders; public: Floor(const int, const char* const, const char* const); // elevation (inches above ground) of floor, name and label operator int() const {return elevation;} bool hasUpRider() const {return !upRiders.empty() ? true : false;} bool hasDownRider() const {return !downRiders.empty() ? true : false;} void addRider(const Rider&); Rider removeUpRider(); Rider removeDownRider(); unsigned int getNumberOfUpRiders() const {return upRiders.size();} unsigned int getNumberOfDownRiders() const { return downRiders.size();} static const char* const UP; static const char* const DOWN; static int TOP, BOTTOM; Panel panel; const string name; // name of floor, for example: Mezzanine const string label; // as it appears on the button panel const int elevation; // inches above ground level // for external reporting friend ostream& operator<<(ostream&, const Floor&); // say name, location, #'s of up/down riders waiting }; #endif

Source code for Panel.cpp:

#include  #include  using namespace std; #include "Panel.h" int Panel::Button::count = 0; void Panel::addButton(string label) { Button temp; temp.label = label; temp.lit = false; buttons.push_back(temp); } void Panel::press(string label) { for(int i = 0; i < buttons.size(); i++) { if(this->buttons[i].label == label) { this->buttons[i].lit = true; Button::count++; this->buttons[i].sequence = Button::count; } } } void Panel::clear(string label) { for(int i = 0; i < this->buttons.size(); i++) { if(this->buttons[i].label == label) this->buttons[i].lit = false; } } string Panel::getFirstLit() const { string firstLit = ""; int sequence = 0; for(int i = 0; i < this->buttons.size(); i++) { if(this->buttons[i].lit) { if(sequence == 0 ) { sequence = buttons[i].sequence; firstLit = buttons[i].label; } else if(sequence != 0 && sequence > this->buttons[i].sequence) { sequence = this->buttons[i].sequence; firstLit = this->buttons[i].label; } } } return firstLit; } bool Panel::isLit(string label) const { for(int i = 0; i < buttons.size(); i++) { if(label == buttons[i].label) return buttons[i].lit ? true : false; } return false; } bool Panel::areAnyLit() const { for(int i = 0; i < this->buttons.size(); i++) { if(this->buttons[i].lit) return true; } return false; } ostream& operator<<(ostream& out, const Panel& p) { for(int i = 0; i < p.buttons.size(); i++) { if(p.buttons[i].lit) { out << "[" << p.buttons[i].label << "]"; } } return out; }

Source code for Panel.h:

#ifndef Panel_h #define Panel_h #include  #include  #include  using namespace std; class Panel { struct Button { string label; bool lit; int sequence; static int count; }; vector

Source code for Rider.cpp:

#include "Rider.h" #include "Building.h" Rider::Rider(int from, int to) :from(from), to(to), goingUp(Building::floors[to] > Building::floors[from] ? true : false), goingDown(Building::floors[to] < Building::floors[from] ? true : false) { } Rider& Rider::operator=(const Rider& original) { if (this != &original) { const_cast(this->from) = original.from; const_cast(this->to) = original.to; const_cast(this->goingUp) = original.goingUp; const_cast(this->goingDown) = original.goingDown; } return *this; }

Source code for Rider.h:

#ifndef Rider_h #define Rider_h struct Rider { const int from, to; const bool goingUp, goingDown; Rider(int,int); Rider& operator=(const Rider&); }; #endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!