• No results found

Optional Case Study - Chapter 7

N/A
N/A
Protected

Academic year: 2021

Share "Optional Case Study - Chapter 7"

Copied!
51
0
0

Loading.... (view fulltext now)

Full text

(1)

2000 Deitel & Associates, Inc. All rights reserved.

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.

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.

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.

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.

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.

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.

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 <iostream>

4

5 using std::cout;

6 using std::cin;

7 using std::endl;

8

9 #include "building.h"

10

11 int 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.

2000 Deitel & Associates, Inc. All rights reserved.

Home

Outline

Building Header

Load headers of objects in class

Create 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

38 class Building { 39

40 public:

41 Building(); // constructor 42 ~Building(); // destructor

43 void runSimulation( int ); // run sim for specified time 44

45 private:

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.

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 <iostream>

57

58 using std::cout;

59 using std::cin;

60 using std::endl;

61

62 #include "building.h"

63

64 Building::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

71 Building::~Building() // destructor

72 { cout << "building destroyed" << endl; } 73

74 // control the simulation

75 void 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.

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

93 class Clock { 94

95 public:

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.

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 <iostream>

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.

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.

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.

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 <iostream>

167

168using std::cout;

169using std::endl;

170

171#include <cstdlib>

172#include <ctime>

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.

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.

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 << newPersonPtr->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.

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.

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.

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 <iostream>

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.

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.

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 <iostream>

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.

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_HDoor 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.

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 <iostream>

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.

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.

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.

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 <iostream>

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.

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.

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 <iostream>

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.

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.

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.

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 (unless they are a technician).

(32)

2000 Deitel & Associates, Inc. All rights reserved.

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 <iostream>

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.

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.

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

References

Related documents

simulator floor2 floor's elevator arrival light clock floor button person waiting on a floor time elevator door elevator's passenger.. All

On the Destination Complete COP, there are no exposed floor buttons inside the elevator car because floor destinations are selected at the kiosk. Destination Complete

 represent turn toward authentic, risk-taking spirit of early rock n roll and away from the pomposity/self-conscious artistry of album oriented rock.  About the sound, not

(2) Where, pursuant to subsection (1), an elevator licence has been suspended or cancelled or an elevator has been sealed and its use prohibited, no person shall operate the elevator

Serves 6–8 225 g (8 oz) Pâte Brisée 200 ml (7 fl oz) whipping cream (35% fat) 200 ml (7 fl oz) full-cream milk 4 eggs ½ teaspoon freshly grated nutmeg 1 teaspoon ground black

This report highlights 2 cases of choroidal melanoma with vision loss mimicking neurological diagnoses.. The first patient is a 41-year-old white male with a

Department Head, the Dean, and at least two members of the department. Formal review at the end of one year is required for reappointment. Renewal of all research, non-chaired

Elevator Call Button Access - Configuration Diagram Basement (2) Basement (1) Ground Floor RS485 communication line PC Communicator HyperAccess Access Control System