Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 7 Outline 7.1 Introduction 7.2 Overview of Simulation Implementation.

Similar presentations


Presentation on theme: " 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 7 Outline 7.1 Introduction 7.2 Overview of Simulation Implementation."— Presentation transcript:

1  2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 7 Outline 7.1 Introduction 7.2 Overview of Simulation Implementation 7.3 Elevator Simulation Implementation Code (hyperlinked) a Driver Building Clock Scheduler Bell Light Door ElevatorButton FloorButton Elevator Floor Person Sample Simulation 7.4 Conclusion

2  2000 Deitel & Associates, Inc. All rights reserved. 7.1 Introduction Chapter 2 - 5 –Designed simulator Chapter 6 –Began programming simulator Body of Chapter 7 –Discussed dynamic object management ( new and delete ) –Composition - create classes with objects of other classes as data members –static and const class members

3  2000 Deitel & Associates, Inc. All rights reserved. 7.2 Overview of Simulation Implementation Simulation controlled by a Building object –Contains two Floor objects, and an object of classes Elevator, Clock, and Scheduler Clock keeps track of time, incremented by the building every second Scheduler schedules arrival of people to each floor After each clock tick –Building updates scheduler with current time ( processTime ) Scheduler compares this with next arrival time for people on floor –If a person scheduled to arrive Scheduler checks if floor occupied ( isOccupied of class Floor ) If true, scheduler calls delayArrival to delay arrival time by second

4  2000 Deitel & Associates, Inc. All rights reserved. 7.2 Overview of Simulation Implementation (II) If floor empty –Scheduler creates a Person object on appropriate floor –Person invokes pressButton (class FloorButton ) Floor button invokes summonElevator (class Elevator ) Elevator –Building updates elevator with time after each clock tick –Upon getting time, elevator checks state ("moving" or "not moving") –If moving, but not scheduled to arrive Elevator outputs direction of motion –If moving, and time matches arrival time Elevator stops, resets its button, rings bell Notifies floor it has arrived ( elevatorArrived, class Floor ) Floor resets floor button, turns on light Elevator opens door, passenger exits, new can person enter Elevator closes door, determines whether to move to other floor

5  2000 Deitel & Associates, Inc. All rights reserved. 7.2 Overview of Simulation Implementation (III) Elevator (continued) –If elevator not moving when gets updated time Determines which floors need service –If current floor Rings bell, notifies floor it has arrived, and opens door Person on floor enters, presses button to start elevator moving to other floor –If other floor Elevator begins moving to that floor

6  2000 Deitel & Associates, Inc. All rights reserved. 7.3 Elevator Simulation Implementation In previous sections –Gathered information about our system –Created object-oriented design of elevator simulation –Represented design using UML –We have discussed all the C++ needed to implement the simulation Remainder of section –Detailed walkthrough of implementation

7  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Driver for Simulation Prompt user for duration Ignore return character from input stream Create object, invoke member function Print messages when simulation begins and ends 1// Figure 7.11 2// Driver for the simulation. 3#include 4 5using std::cout; 6using std::cin; 7using std::endl; 8 9#include "building.h" 10 11int main() 12{ 13 int duration; // length of simulation in seconds 14 15 cout << "Enter run time: "; 16 cin >> duration; 17 cin.ignore(); // ignore return char 18 19 Building building; // create the building 20 21 cout << endl << "*** ELEVATOR SIMULATION BEGINS ***" 22 << endl << endl; 23 building.runSimulation( duration ); // start simulation 24 cout << "*** ELEVATOR SIMULATION ENDS ***" << endl; 25 26 return 0; 27}

8  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Building Header Load headers of objects in class Floor, Elevator, Clock, and Scheduler objects 28// building.h 29// Definition for class Building. 30#ifndef BUILDING_H 31#define BUILDING_H 32 33#include "elevator.h" 34#include "floor.h" 35#include "clock.h" 36#include "scheduler.h" 37 38class Building { 39 40public: 41 Building(); // constructor 42 ~Building(); // destructor 43 void runSimulation( int ); // run sim for specified time 44 45private: 46 Floor floor1; // floor1 object 47 Floor floor2; // floor2 object 48 Elevator elevator; // elevator object 49 Clock clock; // clock object 50 Scheduler scheduler; // scheduler object 51}; 52 53#endif // BUILDING_H

9  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Building Implementation FLOOR1 and FLOOR2 are constants, defined in class Floor Constructors for objects of Building called with appropriate arguments Loop until specified time has passed. cin.get() : stops text from scrolling; when user presses key, it resumes 54// building.cpp 55// Member function definitions for class Building. 56#include 57 58using std::cout; 59using std::cin; 60using std::endl; 61 62#include "building.h" 63 64Building::Building() // constructor 65 : floor1( Floor::FLOOR1, elevator ), 66 floor2( Floor::FLOOR2, elevator ), 67 elevator( floor1, floor2 ), 68 scheduler( floor1, floor2 ) 69{ cout << "building created" << endl; } 70 71Building::~Building() // destructor 72{ cout << "building destroyed" << endl; } 73 74// control the simulation 75void Building::runSimulation( int totalTime ) 76{ 77 int currentTime = 0; 78 79 while ( currentTime < totalTime ) { 80 clock.tick(); 81 currentTime = clock.getTime(); 82 cout << "TIME: " << currentTime << endl; 83 scheduler.processTime( currentTime ); 84 elevator.processTime( currentTime ); 85 cin.get(); // stop each second for user to view output 86 } 87}

10  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Clock Header Simple class, composed of no other objects Increments time through tick Current time available through getTime Note that getTime is const 88// clock.h 89// Definition for class Clock. 90#ifndef CLOCK_H 91#define CLOCK_H 92 93class Clock { 94 95public: 96 Clock(); // constructor 97 ~Clock(); // destructor 98 void tick(); // increment clock by one second 99 int getTime() const; // returns clock's current time 100 101private: 102 int time; // clock's time 103}; 104 105#endif // CLOCK_H

11  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Clock Implementation Function definitions 106// clock.cpp 107// Member function definitions for class Clock. 108#include 109 110using std::cout; 111using std::endl; 112 113#include "clock.h" 114 115Clock::Clock() // constructor 116 : time( 0 ) 117{ cout << "clock created" << endl; } 118 119Clock::~Clock() // destructor 120{ cout << "clock destroyed" << endl; } 121 122void Clock::tick() // increment time by 1 123{ time++; } 124 125int Clock::getTime() const // return current time 126{ return time; }

12  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Scheduler Header processTime operation Private utility functions (discussed in implementation file) 127// scheduler.h 128// definition for class Scheduler 129#ifndef SCHEDULER_H 130#define SCHEDULER_H 131 132class Floor; // forward declaration 133 134class Scheduler { 135 136public: 137 Scheduler( Floor &, Floor & ); // constructor 138 ~Scheduler(); // destructor 139 void processTime( int ); // set scheduler's time 140 141private: 142 // schedule arrival to a floor 143 void scheduleTime( const Floor & ); 144 145 // delay arrival to a floor 146 void delayTime( const Floor & ); 147 148 // create new person; place on floor 149 void createNewPerson( Floor & ); 150 151 // handle person arrival on a floor 152 void handleArrivals( Floor &, int );

13  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Scheduler Header Member variables 153 154 int currentClockTime; 155 156 Floor &floor1Ref; 157 Floor &floor2Ref; 158 159 int floor1ArrivalTime; 160 int floor2ArrivalTime; 161}; 162 163#endif // SCHEDULER_H

14  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Scheduler Implementation Constructor : seeds pseudo-random arrival time Calls scheduleTime for each floor Destructor 164// scheduler.cpp 165// Member function definitions for class Scheduler. 166#include 167 168using std::cout; 169using std::endl; 170 171#include 172#include 173 174#include "scheduler.h" 175#include "floor.h" 176#include "person.h" 177 178// constructor 179Scheduler::Scheduler( Floor &firstFloor, Floor &secondFloor ) 180 : currentClockTime( 0 ), floor1Ref( firstFloor ), 181 floor2Ref( secondFloor ) 182{ 183 srand( time( 0 ) ); // seed random number generator 184 cout << "scheduler created" << endl; 185 186 // schedule first arrivals for floor 1 and floor 2 187 scheduleTime( floor1Ref ); 188 scheduleTime( floor2Ref ); 189} 190 191Scheduler::~Scheduler() // destructor 192{ cout << "scheduler destroyed" << endl; } 193

15  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Scheduler Implementation scheduleTime : creates pseudorandom arrival time for Person objects on each floor delayTime : delays arrival time for one second 194// schedule arrival on a floor 195void Scheduler::scheduleTime( const Floor &floor ) 196{ 197 int floorNumber = floor.getNumber(); 198 int arrivalTime = currentClockTime + ( 5 + rand() % 16 ); 199 200 floorNumber == Floor::FLOOR1 ? 201 floor1ArrivalTime = arrivalTime : 202 floor2ArrivalTime = arrivalTime; 203 204 cout << "(scheduler schedules next person for floor " 205 << floorNumber << " at time " << arrivalTime << ')' 206 << endl; 207} 208 209// reschedule an arrival on a floor 210void Scheduler::delayTime( const Floor &floor ) 211{ 212 int floorNumber = floor.getNumber(); 213 214 int arrivalTime = ( floorNumber == Floor::FLOOR1 ) ? 215 ++floor1ArrivalTime : ++floor2ArrivalTime; 216 217 cout << "(scheduler delays next person for floor " 218 << floorNumber << " until time " << arrivalTime << ')' 219 << endl; 220} 221

16  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Scheduler Implementation processTime: building updates scheduler 's time using this function Call handleArrivals for each floor createNewPerson: Creates new object of class Person Sends stepOntoFloor message scheduleTime: Calculate arrival time 222// give time to scheduler 223void Scheduler::processTime( int time ) 224{ 225 currentClockTime = time; // record time 226 227 // handle arrivals on floor 1 228 handleArrivals( floor1Ref, currentClockTime ); 229 230 // handle arrivals on floor 2 231 handleArrivals( floor2Ref, currentClockTime ); 232} 233 234// create new person and place it on specified floor 235void Scheduler::createNewPerson( Floor &floor ) 236{ 237 int destinationFloor = 238 floor.getNumber() == Floor::FLOOR1 ? 239 Floor::FLOOR2 : Floor::FLOOR1; 240 241 // create new person 242 Person *newPersonPtr = new Person( destinationFloor ); 243 244 cout << "scheduler creates person " 245 getID() << endl; 246 247 // place person on proper floor 248 newPersonPtr->stepOntoFloor( floor ); 249 250 scheduleTime( floor ); // schedule next arrival 251}

17  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Scheduler Implementation handleArrivals: Compares current time to arrivalTime If times match and floor occupied, delay arrival by one second If unoccupied, create a new person on that floor 252 253// handle arrivals for a specified floor 254void Scheduler::handleArrivals( Floor &floor, int time ) 255{ 256 int floorNumber = floor.getNumber(); 257 258 int arrivalTime = ( floorNumber == Floor::FLOOR1 ) ? 259 floor1ArrivalTime : floor2ArrivalTime; 260 261 if ( arrivalTime == time ) { 262 263 if ( floor.isOccupied() ) // see if floor occupied 264 delayTime( floor ); 265 else 266 createNewPerson( floor ); 267 } 268}

18  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Bell Header Contains no other objects 269// bell.h 270// Definition for class Bell. 271#ifndef BELL_H 272#define BELL_H 273 274class Bell { 275 276public: 277 Bell(); // constructor 278 ~Bell(); // destructor 279 void ringBell() const; // ring the bell 280}; 281 282#endif // BELL_H

19  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Bell Implementation Constructor Destructor ringBell 283// bell.cpp 284// Member function definitions for class Bell. 285#include 286 287using std::cout; 288using std::endl; 289 290#include "bell.h" 291 292Bell::Bell() // constructor 293{ cout << "bell created" << endl; } 294 295Bell::~Bell() // destructor 296{ cout << "bell destroyed" << endl; } 297 298void Bell::ringBell() const // ring bell 299{ cout << "elevator rings its bell" << endl; }

20  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Light Header Member functions Member variables 300// light.h 301// Definition for class Light. 302#ifndef LIGHT_H 303#define LIGHT_H 304 305class Light { 306 307public: 308 Light( const char * ); // constructor 309 ~Light(); // destructor 310 void turnOn(); // turns light on 311 void turnOff(); // turns light off 312 313private: 314 bool on; // true if on; false if off 315 const char *name; // which floor the light is on 316}; 317 318#endif // LIGHT_H

21  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Light Implementation Constructor Destructor turnOn: sets on to true turnOff: sets on to false 319// light.cpp 320// Member function definitions for class Light. 321#include 322 323using std::cout; 324using std::endl; 325 326#include "light.h" 327 328Light::Light( const char *string ) // constructor 329 : on( false ), name( string ) 330{ cout << name << " light created" << endl; } 331 332Light::~Light() // destructor 333{ cout << name << " light destroyed" << endl; } 334 335void Light::turnOn() // turn light on 336{ 337 on = true; 338 cout << name << " turns on its light" << endl; 339} 340 341void Light::turnOff() // turn light off 342{ 343 on = false; 344 cout << name << " turns off its light" << endl; 345}

22  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Door Header In our simulation, door signals passenger to exit elevator, and new person to enter 346// door.h 347// Definition for class Door. 348#ifndef DOOR_H 349#define DOOR_H 350 351class Person; // forward declaration 352class Floor; // forward declaration 353class Elevator; // forward declaration 354 355class Door { 356 357public: 358 Door(); // constructor 359 ~Door(); // destructor 360 361 void openDoor( Person * const, Person * const, 362 Floor &, Elevator & ); 363 void closeDoor( const Floor & ); 364 365private: 366 bool open; // open or closed 367}; 368 369#endif // DOOR_H Door is a composite object in class Elevator ; header for Elevator must contain #include "door.h" Door has a reference to an Elevator object- could put #include "elevator.h" in Door 's header. Causes circular include problem. Avoid this by using forward class declarations (line 353 ) - tells compiler definition of class lies outside of file.

23  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Door Implementation Load headers - correspond to forward declarations Header files have required function prototypes Constructor Destructor 370// door.cpp 371// Member function definitions for class Door. 372#include 373 374using std::cout; 375using std::endl; 376 377#include "door.h" 378#include "person.h" 379#include "floor.h" 380#include "elevator.h" 381 382Door::Door() // constructor 383 : open( false ) 384{ cout << "door created" << endl; } 385 386Door::~Door() // destructor 387{ cout << "door destroyed" << endl; } 388

24  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Door Implementation openDoor : Checks that door is not already open Checks pointer to see if passenger needs to exit - calls exitElevator Passenger leaves simulation Checks for new passenger - calls enterElevator closeDoor: Checks if Door open; if so, closes it 389// open the door 390void Door::openDoor( Person * const passengerPtr, 391 Person * const nextPassengerPtr, 392 Floor &currentFloor, Elevator &elevator ) 393{ 394 if ( !open ) { 395 open = true; 396 397 cout << "elevator opens its door on floor " 398 << currentFloor.getNumber() << endl; 399 400 if ( passengerPtr != 0 ) { 401 passengerPtr->exitElevator( currentFloor, elevator ); 402 delete passengerPtr; // passenger leaves simulation 403 } 404 405 if ( nextPassengerPtr != 0 ) 406 nextPassengerPtr->enterElevator( 407 elevator, currentFloor ); 408 } 409} 410 411// close the door 412void Door::closeDoor( const Floor &currentFloor ) 413{ 414 if ( open ) { 415 open = false; 416 cout << "elevator closes its door on floor " 417 << currentFloor.getNumber() << endl; 418 } 419}

25  2000 Deitel & Associates, Inc. All rights reserved. Home Outline ElevatorButton Header Public operations Member variables 420// elevatorButton.h 421// Definition for class ElevatorButton. 422#ifndef ELEVATORBUTTON_H 423#define ELEVATORBUTTON_H 424 425class Elevator; // forward declaration 426 427class ElevatorButton { 428 429public: 430 ElevatorButton( Elevator & ); // constructor 431 ~ElevatorButton(); // destructor 432 433 void pressButton(); // press the button 434 void resetButton(); // reset the button 435 436private: 437 bool pressed; // state of button 438 Elevator &elevatorRef; // reference to button's elevator 439}; 440 441#endif // ELEVATORBUTTON_H

26  2000 Deitel & Associates, Inc. All rights reserved. Home Outline ElevatorButton Implementation pressButton: Sets pressed to true Sends prepareToLeave message to elevator resetButton: sets pressed to false 442// elevatorButton.cpp: 443// Member function definitions for class ElevatorButton. 444#include 445 446using std::cout; 447using std::endl; 448 449#include "elevatorButton.h" 450#include "elevator.h" 451 452// constructor 453ElevatorButton::ElevatorButton( Elevator &elevatorHandle ) 454 : pressed( false ), elevatorRef( elevatorHandle ) 455{ cout << "elevator button created" << endl; } 456 457ElevatorButton::~ElevatorButton() // destructor 458{ cout << "elevator button destroyed" << endl; } 459 460void ElevatorButton::pressButton() // press the button 461{ 462 pressed = true; 463 cout << "elevator button tells elevator to prepare to leave" 464 << endl; 465 elevatorRef.prepareToLeave( true ); 466} 467 468void ElevatorButton::resetButton() // reset the button 469{ pressed = false; }

27  2000 Deitel & Associates, Inc. All rights reserved. Home Outline FloorButton Header Same member functions as ElevatorButton Member variables 470// floorButton.h 471// Definition for class FloorButton. 472#ifndef FLOORBUTTON_H 473#define FLOORBUTTON_H 474 475class Elevator; // forward declaration 476 477class FloorButton { 478 479public: 480 FloorButton( const int, Elevator & ); // constructor 481 ~FloorButton(); // destructor 482 483 void pressButton(); // press the button 484 void resetButton(); // reset the button 485 486private: 487 const int floorNumber; // number of the button's floor 488 bool pressed; // state of button 489 490 // reference to button's elevator 491 Elevator &elevatorRef; 492}; 493 494#endif // FLOORBUTTON_H

28  2000 Deitel & Associates, Inc. All rights reserved. Home Outline FloorButton Implementation Constructor Destructor 495// floorButton.cpp 496// Member function definitions for class FloorButton. 497#include 498 499using std::cout; 500using std::endl; 501 502#include "floorButton.h" 503#include "elevator.h" 504 505// constructor 506FloorButton::FloorButton( const int number, 507 Elevator &elevatorHandle ) 508 : floorNumber( number ), pressed( false ), 509 elevatorRef( elevatorHandle ) 510{ 511 cout << "floor " << floorNumber << " button created" 512 << endl; 513} 514 515FloorButton::~FloorButton() // destructor 516{ 517 cout << "floor " << floorNumber << " button destroyed" 518 << endl; 519} 520

29  2000 Deitel & Associates, Inc. All rights reserved. Home Outline FloorButton Implementation pressButton : Set pressed to true, call summonElevator resetButton: Set pressed to false 521// press the button 522void FloorButton::pressButton() 523{ 524 pressed = true; 525 cout << "floor " << floorNumber 526 << " button summons elevator" << endl; 527 elevatorRef.summonElevator( floorNumber ); 528} 529 530// reset the button 531void FloorButton::resetButton() 532{ pressed = false; }

30  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Elevator Header Load headers and forward declarations public member functions 533// elevator.h 534// Definition for class Elevator. 535#ifndef ELEVATOR_H 536#define ELEVATOR_H 537 538#include "elevatorButton.h" 539#include "door.h" 540#include "bell.h" 541 542class Floor; // forward declaration 543class Person; // forward declaration 544 545class Elevator { 546 547public: 548 Elevator( Floor &, Floor & ); // constructor 549 ~Elevator(); // destructor 550 void summonElevator( int ); // request to service a floor 551 void prepareToLeave( bool ); // prepare to leave 552 void processTime( int ); // give time to elevator 553 void passengerEnters( Person * const ); // board a passenger 554 void passengerExits(); // exit a passenger 555 ElevatorButton elevatorButton; // note public object 556 elevatorButton public so all Person objects can use it.

31  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Elevator Header private utility functions static const data members available to all objects of class Elevator Data members and handles Need pointer (not reference) for Person object because passenger changes 557private: 558 void processPossibleArrival(); 559 void processPossibleDeparture(); 560 void arriveAtFloor( Floor & ); 561 void move(); 562 563 // time to move between floors 564 static const int ELEVATOR_TRAVEL_TIME; 565 static const int UP; // UP direction 566 static const int DOWN; // DOWN direction 567 568 int currentBuildingClockTime; // current time 569 bool moving; // elevator state 570 int direction; // current direction 571 int currentFloor; // current location 572 int arrivalTime; // time to arrive at a floor 573 bool floor1NeedsService; // floor1 service flag 574 bool floor2NeedsService; // floor2 service flag 575 576 Floor &floor1Ref; // reference to floor1 577 Floor &floor2Ref; // reference to floor2 578 Person *passengerPtr; // pointer to current passenger 579 580 Door door; // door object 581 Bell bell; // bell object 582}; 583 584#endif // ELEVATOR_H bell and door are private because people do not usually interface with those objects.

32  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Elevator Implementation Constructor (note large member- initializer list) Destructor 585// elevator.cpp 586// Member function definitions for class Elevator. 587#include 588 589using std::cout; 590using std::endl; 591 592#include "elevator.h" 593#include "person.h" 594#include "floor.h" 595 596const int Elevator::ELEVATOR_TRAVEL_TIME = 5; 597const int Elevator::UP = 0; 598const int Elevator::DOWN = 1; 599 600// constructor 601Elevator::Elevator( Floor &firstFloor, Floor &secondFloor ) 602 : elevatorButton( *this ), currentBuildingClockTime( 0 ), 603 moving( false ), direction( UP ), 604 currentFloor( Floor::FLOOR1 ), arrivalTime( 0 ), 605 floor1NeedsService( false ), floor2NeedsService( false ), 606 floor1Ref( firstFloor ), floor2Ref( secondFloor ), 607 passengerPtr( 0 ) 608{ cout << "elevator created" << endl; } 609 610Elevator::~Elevator() // destructor 611{ cout << "elevator destroyed" << endl; } 612

33  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Elevator Implementation processTime: Updates time (called by building ) Check possible departure or arrival and determine if elevator moving or not processPossible Arrival: Compare time to arrival time If match, update floor, direction, call arriveAtFloor 613// give time to elevator 614void Elevator::processTime( int time ) 615{ 616 currentBuildingClockTime = time; 617 618 if ( moving ) 619 processPossibleArrival(); 620 else 621 processPossibleDeparture(); 622 623 if ( !moving ) 624 cout << "elevator at rest on floor " 625 << currentFloor << endl; 626} 627 628// when elevator is moving, determine if it should stop 629void Elevator::processPossibleArrival() 630{ 631 // if elevator arrives at destination floor 632 if ( currentBuildingClockTime == arrivalTime ) { 633 634 currentFloor = // update current floor 635 ( currentFloor == Floor::FLOOR1 ? 636 Floor::FLOOR2 : Floor::FLOOR1 ); 637 638 direction = // update direction 639 ( currentFloor == Floor::FLOOR1 ? UP : DOWN ); 640 641 cout << "elevator arrives on floor " 642 << currentFloor << endl; 643 644 arriveAtFloor( currentFloor == Floor::FLOOR1 ? 645 floor1Ref : floor2Ref );

34  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Elevator Implementation processPossible Arrival (cont'd) : Exit function (if times match) If times do not match, print message processPossible Departure: If needs to service current floor, call arriveAtFloor If needs to service other floor, call prepareToLeave (which will leave if argument true ) 646 647 return; 648 } 649 650 // elevator is moving 651 cout << "elevator moving " 652 << ( direction == UP ? "up" : "down" ) << endl; 653} 654 655// determine if elevator should move 656void Elevator::processPossibleDeparture() 657{ 658 // this floor needs service? 659 bool currentFloorNeedsService = 660 currentFloor == Floor::FLOOR1 ? 661 floor1NeedsService : floor2NeedsService; 662 663 // other floor needs service? 664 bool otherFloorNeedsService = 665 currentFloor == Floor::FLOOR1 ? 666 floor2NeedsService : floor1NeedsService; 667 668 // service this floor (if needed) 669 if ( currentFloorNeedsService ) { 670 arriveAtFloor( currentFloor == Floor::FLOOR1 ? 671 floor1Ref : floor2Ref ); 672 673 return; 674 } 675 676 // service other floor (if needed) 677 else prepareToLeave( otherFloorNeedsService ); 678} 679

35  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Elevator Implementation arriveAtFloor: Stops elevator Resets button, rings bell elevatorArrived returns pointer to Person object waiting on floor Open door Check if floors need service If not, reset service flags 680// arrive at a particular floor 681void Elevator::arriveAtFloor( Floor& arrivalFloor ) 682{ 683 moving = false; // reset state 684 685 cout << "elevator resets its button" << endl; 686 elevatorButton.resetButton(); 687 688 bell.ringBell(); 689 690 // notify floor that elevator has arrived 691 Person *floorPersonPtr = arrivalFloor.elevatorArrived(); 692 693 door.openDoor( passengerPtr, floorPersonPtr, 694 arrivalFloor, *this ); 695 696 // this floor needs service? 697 bool currentFloorNeedsService = 698 currentFloor == Floor::FLOOR1 ? 699 floor1NeedsService : floor2NeedsService; 700 701 // other floor needs service? 702 bool otherFloorNeedsService = 703 currentFloor == Floor::FLOOR1 ? 704 floor2NeedsService : floor1NeedsService; 705 706 // if this floor does not need service 707 // prepare to leave for the other floor 708 if ( !currentFloorNeedsService ) 709 prepareToLeave( otherFloorNeedsService ); 710 else // otherwise, reset service flag 711 currentFloor == Floor::FLOOR1 ? 712 floor1NeedsService = false: floor2NeedsService = false; 713}

36  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Elevator Implementation summonElevator: Allows other objects to request service, sets service flag passengerEnters: Update passengerPtr handle passengerExits: Set passengerPtr handle to zero (no passenger) 714 715// request service from elevator 716void Elevator::summonElevator( int floor ) 717{ 718 // set appropriate servicing flag 719 floor == Floor::FLOOR1 ? 720 floor1NeedsService = true : floor2NeedsService = true; 721} 722 723// accept a passenger 724void Elevator::passengerEnters( Person * const personPtr ) 725{ 726 // board passenger 727 passengerPtr = personPtr; 728 729 cout getID() 730 << " enters elevator from floor " 731 << currentFloor << endl; 732} 733 734// notify elevator that passenger is exiting 735void Elevator::passengerExits() { passengerPtr = 0; } 736

37  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Elevator Implementation prepareToLeave: Elevator notifies floor it may leave, closes door Elevator decides if it must leave move: Set moving to true Schedule arrival time Print message 737// prepare to leave a floor 738void Elevator::prepareToLeave( bool leaving ) 739{ 740 Floor &thisFloor = 741 currentFloor == Floor::FLOOR1 ? floor1Ref : floor2Ref; 742 743 // notify floor that elevator may be leaving 744 thisFloor.elevatorLeaving(); 745 746 door.closeDoor( thisFloor ); 747 748 if ( leaving ) // leave, if necessary 749 move(); 750} 751 752void Elevator::move() // go to a particular floor 753{ 754 moving = true; // change state 755 756 // schedule arrival time 757 arrivalTime = currentBuildingClockTime + 758 ELEVATOR_TRAVEL_TIME; 759 760 cout << "elevator begins moving " 761 << ( direction == DOWN ? "down " : "up ") 762 << "to floor " 763 << ( direction == DOWN ? '1' : '2' ) 764 << " (arrives at time " << arrivalTime << ')' 765 << endl; 766}

38  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Floor Header Member functions 767// floor.h 768// Definition for class Floor. 769#ifndef FLOOR_H 770#define FLOOR_H 771 772#include "floorButton.h" 773#include "light.h" 774 775class Elevator; // forward declaration 776class Person; // forward declaration 777 778class Floor { 779 780public: 781 Floor( int, Elevator & ); // constructor 782 ~Floor(); // destructor 783 bool isOccupied() const; // return true if floor occupied 784 int getNumber() const; // return floor's number 785 786 // pass a handle to new person coming on floor 787 void personArrives( Person * const ); 788 789 // notify floor that elevator has arrived 790 Person *elevatorArrived(); 791 792 // notify floor that elevator is leaving 793 void elevatorLeaving(); 794 795 // notify floor that person is leaving floor 796 void personBoardingElevator();

39  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Floor Header public const members, initialized in implementation private data member Reference to Elevator object (does not change over time) Pointer to Person object (occupant changes over time) 797 798 static const int FLOOR1; 799 static const int FLOOR2; 800 FloorButton floorButton; // floorButton object 801 802private: 803 const int floorNumber; // the floor's number 804 Elevator &elevatorRef; // pointer to elevator 805 Person *occupantPtr; // pointer to person on floor 806 Light light; // light object 807}; 808 809#endif // FLOOR_H floorButton is public so all Person objects can interface with it. Light is private, because people do not interface with it.

40  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Floor Implementation Constructor Destructor isOccupied: Returns true if occupantPtr not 0 810// floor.cpp 811// Member function definitions for class Floor. 812#include 813 814using std::cout; 815using std::endl; 816 817#include "floor.h" 818#include "person.h" 819#include "elevator.h" 820 821const int Floor::FLOOR1 = 1; 822const int Floor::FLOOR2 = 2; 823 824// constructor 825Floor::Floor(int number, Elevator &elevatorHandle ) 826 : floorButton( number, elevatorHandle ), 827 floorNumber( number ), elevatorRef( elevatorHandle ), 828 occupantPtr ( 0 ), 829 light( floorNumber == 1 ? "floor 1" : "floor 2" ) 830{ cout << "floor " << floorNumber << " created" << endl; } 831 832// destructor 833Floor::~Floor() 834{ cout << "floor " << floorNumber << " destroyed" << endl; } 835 836// determine if floor is occupied 837bool Floor::isOccupied() const 838{ return ( occupantPtr != 0 ); } 839

41  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Floor Implementation getNumber: Returns floorNumber personArrives: Set occupantPtr to point to new Person object elevatorArrived: Print message Reset button Turn on light Return pointer elevatorLeaving: Turn off light personBoarding Elevator: Set occupantPtr to 0 840// return this floor's number 841int Floor::getNumber() const { return floorNumber; } 842 843// pass person to floor 844void Floor::personArrives( Person * const personPtr ) 845{ occupantPtr = personPtr; } 846 847// notify floor that elevator has arrived 848Person *Floor::elevatorArrived() 849{ 850 // reset the button on floor, if necessary 851 cout << "floor " << floorNumber 852 << " resets its button" << endl; 853 floorButton.resetButton(); 854 855 light.turnOn(); 856 857 return occupantPtr; 858} 859 860// tell floor that the elevator is leaving 861void Floor::elevatorLeaving() { light.turnOff(); } 862 863// notifies floor that a person is leaving it 864void Floor::personBoardingElevator() { occupantPtr = 0; }

42  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Person Header Forward declarations, public member functions, private data 865// person.h 866// definition of class Person 867#ifndef PERSON_H 868#define PERSON_H 869 870class Floor; // forward declaration 871class Elevator; // forward declaration 872 873class Person { 874 875public: 876 Person( const int ); // constructor 877 ~Person(); // destructor 878 int getID() const; // returns person's ID 879 880 void stepOntoFloor( Floor & ); 881 void enterElevator( Elevator &, Floor & ); 882 void exitElevator( const Floor &, Elevator & ) const; 883 884private: 885 static int personCount; // total number of persons 886 const int ID; // person's unique ID # 887 const int destinationFloor; // destination floor # 888}; 889 890#endif // PERSON_H

43  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Person Implementation Initialize personCount Constructor Destructor getID: Returns ID 891// person.cpp 892// Member function definitions for class Person. 893#include 894 895using std::cout; 896using std::endl; 897 898#include "person.h" 899#include "floor.h" 900#include "elevator.h" 901 902// initialize static member personCount 903int Person::personCount = 0; 904 905Person::Person( const int destFloor ) // constructor 906 : ID( ++personCount ), destinationFloor( destFloor ) 907{} 908 909Person::~Person() // destructor 910{ 911 cout << "person " << ID << " exits simulation on floor " 912 << destinationFloor << " (person destructor invoked)" 913 << endl; 914} 915 916int Person::getID() const { return ID; } // get the ID 917

44  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Person Implementation stepOntoFloor: Notify floor ( personArrives ) Summon elevator ( pressButton ) enterElevator: Notify floor ( personBoarding Elevator ) Enter elevator Press button 918// person walks onto a floor 919void Person::stepOntoFloor( Floor& floor ) 920{ 921 // notify floor a person is coming 922 cout << "person " << ID << " steps onto floor " 923 << floor.getNumber() << endl; 924 floor.personArrives( this ); 925 926 // press button on the floor 927 cout << "person " << ID 928 << " presses floor button on floor " 929 << floor.getNumber() << endl; 930 floor.floorButton.pressButton(); 931} 932 933// person enters elevator 934void Person::enterElevator( Elevator &elevator, Floor &floor ) 935{ 936 floor.personBoardingElevator(); // person leaves floor 937 938 elevator.passengerEnters( this ); // person enters elevator 939 940 // press button on elevator 941 cout << "person " << ID 942 << " presses elevator button" << endl; 943 elevator.elevatorButton.pressButton(); 944}

45  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Person Implementation exitElevator: Print message Send exit message ( passengerExits ) 945 946// person exits elevator 947void Person::exitElevator( 948 const Floor &floor, Elevator &elevator ) const 949{ 950 cout << "person " << ID << " exits elevator on floor " 951 << floor.getNumber() << endl; 952 elevator.passengerExits(); 953}

46  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Sample Simulation Enter run time: 30 (scheduler schedules next person for floor 1 at time 5) (scheduler schedules next person for floor 2 at time 17) *** ELEVATOR SIMULATION BEGINS *** TIME: 1 elevator at rest on floor 1 TIME: 2 elevator at rest on floor 1 TIME: 3 elevator at rest on floor 1 TIME: 4 elevator at rest on floor 1 TIME: 5 scheduler creates person 1 person 1 steps onto floor 1 person 1 presses floor button on floor 1 floor 1 button summons elevator (scheduler schedules next person for floor 1 at time 20) elevator resets its button elevator rings its bell floor 1 resets its button floor 1 turns on its light elevator opens its door on floor 1 person 1 enters elevator from floor 1 person 1 presses elevator button elevator button tells elevator to prepare to leave floor 1 turns off its light elevator closes its door on floor 1 elevator begins moving up to floor 2 (arrives at time 10)

47  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Sample Simulation TIME: 6 elevator moving up TIME: 7 elevator moving up TIME: 8 elevator moving up TIME: 9 elevator moving up TIME: 10 elevator arrives on floor 2 elevator resets its button elevator rings its bell floor 2 resets its button floor 2 turns on its light elevator opens its door on floor 2 person 1 exits elevator on floor 2 floor 2 turns off its light elevator closes its door on floor 2 elevator at rest on floor 2 TIME: 11 elevator at rest on floor 2 TIME: 12 elevator at rest on floor 2 TIME: 13 elevator at rest on floor 2

48  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Sample Simulation TIME: 14 elevator at rest on floor 2 TIME: 15 elevator at rest on floor 2 TIME: 16 elevator at rest on floor 2 TIME: 17 scheduler creates person 2 person 2 steps onto floor 2 person 2 presses floor button on floor 2 floor 2 button summons elevator (scheduler schedules next person for floor 2 at time 34) elevator resets its button elevator rings its bell floor 2 resets its button floor 2 turns on its light elevator opens its door on floor 2 person 2 enters elevator from floor 2 person 2 presses elevator button elevator button tells elevator to prepare to leave floor 2 turns off its light elevator closes its door on floor 2 elevator begins moving down to floor 1 (arrives at time 22) TIME: 18 elevator moving down TIME: 19 elevator moving down

49  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Sample Simulation TIME: 20 scheduler creates person 3 person 3 steps onto floor 1 person 3 presses floor button on floor 1 floor 1 button summons elevator (scheduler schedules next person for floor 1 at time 26) elevator moving down TIME: 21 elevator moving down TIME: 22 elevator arrives on floor 1 elevator resets its button elevator rings its bell floor 1 resets its button floor 1 turns on its light elevator opens its door on floor 1 person 2 exits elevator on floor 1 person 3 enters elevator from floor 1 person 3 presses elevator button elevator button tells elevator to prepare to leave floor 1 turns off its light elevator closes its door on floor 1 elevator begins moving up to floor 2 (arrives at time 27) TIME: 23 elevator moving up TIME: 24 elevator moving up TIME: 25 elevator moving up

50  2000 Deitel & Associates, Inc. All rights reserved. Home Outline Sample Simulation TIME: 26 scheduler creates person 4 person 4 steps onto floor 1 person 4 presses floor button on floor 1 floor 1 button summons elevator (scheduler schedules next person for floor 1 at time 35) elevator moving up TIME: 27 elevator arrives on floor 2 elevator resets its button elevator rings its bell floor 2 resets its button floor 2 turns on its light elevator opens its door on floor 2 person 3 exits elevator on floor 2 floor 2 turns off its light elevator closes its door on floor 2 elevator begins moving down to floor 1 (arrives at time 32) TIME: 28 elevator moving down TIME: 29 elevator moving down TIME: 30 elevator moving down *** ELEVATOR SIMULATION ENDS ***

51  2000 Deitel & Associates, Inc. All rights reserved. 7.4 Conclusion Congratulations! –We have made a working simulator Chapter 9 –Discuss inheritance and apply it to our elevator


Download ppt " 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 7 Outline 7.1 Introduction 7.2 Overview of Simulation Implementation."

Similar presentations


Ads by Google