summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/scheduler.cpp
blob: ac9110c9a2e19180117132c2e06d77745f7b5484 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Fig. 7.30: scheduler.cpp

// Member-function definitions for class Scheduler.

#include <iostream>



using std::cout;

using std::endl;



#include <cstdlib>

#include <ctime>



#include "scheduler.h"  // Scheduler class definition

#include "floor.h"      // Floor class definition

#include "person.h"     // Person class definition



// constructor

Scheduler::Scheduler( Floor &firstFloor, Floor &secondFloor )

   : currentClockTime( 0 ),  

     floor1Ref( firstFloor ),

     floor2Ref( secondFloor )

{

   srand( time( 0 ) ); // seed random number generator

   cout << "scheduler created" << endl;



   // schedule first arrivals for floor 1 and floor 2

   scheduleTime( floor1Ref );

   scheduleTime( floor2Ref );



} // end Scheduler constructor



// destructor

Scheduler::~Scheduler() 

{ 

   cout << "scheduler destroyed" << endl; 



} // end Scheduler destructor



// schedule arrival on a floor

void Scheduler::scheduleTime( const Floor &floor )

{

   int floorNumber = floor.getNumber();

   int arrivalTime = currentClockTime + ( 5 + rand() % 16 );



   floorNumber == Floor::FLOOR1 ?

      floor1ArrivalTime = arrivalTime : 

      floor2ArrivalTime = arrivalTime;



   cout << "(scheduler schedules next person for floor " 

        << floorNumber << " at time " << arrivalTime << ')'

        << endl;



} // end function scheduleTime



// reschedule arrival on a floor

void Scheduler::delayTime( const Floor &floor )

{

   int floorNumber = floor.getNumber();



   int arrivalTime = ( floorNumber == Floor::FLOOR1 ) ? 

      ++floor1ArrivalTime : ++floor2ArrivalTime;



   cout << "(scheduler delays next person for floor "

        << floorNumber << " until time " << arrivalTime << ')'

        << endl;



} // end function delayTime



// give time to scheduler

void Scheduler::processTime( int time )

{

   currentClockTime = time;   // record time

   

   // handle arrivals on floor 1

   handleArrivals( floor1Ref, currentClockTime );



   // handle arrivals on floor 2

   handleArrivals( floor2Ref, currentClockTime );

    

} // end function processTime



// create new person and place it on specified floor

void Scheduler::createNewPerson( Floor &floor )

{

   int destinationFloor = 

      floor.getNumber() == Floor::FLOOR1 ?

         Floor::FLOOR2 : Floor::FLOOR1;



   // create new person

   Person *newPersonPtr = new Person( destinationFloor );



   cout << "scheduler creates person " 

        << newPersonPtr->getID() << endl;

   

   // place person on proper floor

   newPersonPtr->stepOntoFloor( floor );

   

   scheduleTime( floor ); // schedule next arrival



} // end function createNewPerson



// handle arrivals for a specified floor

void Scheduler::handleArrivals( Floor &floor, int time )

{

   int floorNumber = floor.getNumber();



   int arrivalTime = ( floorNumber == Floor::FLOOR1 ) ? 

      floor1ArrivalTime : floor2ArrivalTime;



   if ( arrivalTime == time ) {

      

      if ( floor.isOccupied() )     // if floor occupied,

         delayTime( floor );        // delay arrival



      else                          // otherwise, 

         createNewPerson( floor );  // create new person



   } // end outer if



} // end function handleArrivals



/**************************************************************************

 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice      *

 * Hall. All Rights Reserved.                                             *

 *                                                                        *

 * DISCLAIMER: The authors and publisher of this book have used their     *

 * best efforts in preparing the book. These efforts include the          *

 * development, research, and testing of the theories and programs        *

 * to determine their effectiveness. The authors and publisher make       *

 * no warranty of any kind, expressed or implied, with regard to these    *

 * programs or to the documentation contained in these books. The authors *

 * and publisher shall not be liable in any event for incidental or       *

 * consequential damages in connection with, or arising out of, the       *

 * furnishing, performance, or use of these programs.                     *

 *************************************************************************/