diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Bachelor/Prog1/examples/elevator | |
| download | Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2 | |
Diffstat (limited to 'Bachelor/Prog1/examples/elevator')
23 files changed, 1522 insertions, 0 deletions
diff --git a/Bachelor/Prog1/examples/elevator/ElevatorSimulation.cpp b/Bachelor/Prog1/examples/elevator/ElevatorSimulation.cpp new file mode 100644 index 0000000..6f9e648 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/ElevatorSimulation.cpp @@ -0,0 +1,45 @@ +// Fig. 7.24: elevatorSimulation.cpp
+// Driver for the simulation.
+#include <iostream>
+
+using std::cout;
+using std::cin;
+using std::endl;
+
+#include "building.h" // Building class definition
+
+int main()
+{
+ int duration; // length of simulation in seconds
+
+ cout << "Enter run time: ";
+ cin >> duration;
+ cin.ignore(); // ignore return char
+
+ Building building; // create the building
+
+ cout << endl << "*** ELEVATOR SIMULATION BEGINS ***"
+ << endl << endl;
+
+ building.runSimulation( duration ); // start simulation
+
+ cout << "*** ELEVATOR SIMULATION ENDS ***" << endl;
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/bell.cpp b/Bachelor/Prog1/examples/elevator/bell.cpp new file mode 100644 index 0000000..350b826 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/bell.cpp @@ -0,0 +1,44 @@ +// Fig. 7.32: bell.cpp
+// Member-function definitions for class Bell.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include "bell.h" // Bell class definition
+
+// constructor
+Bell::Bell()
+{
+ cout << "bell created" << endl;
+
+} // end Bell constructor
+
+// destructor
+Bell::~Bell()
+{
+ cout << "bell destructed" << endl;
+
+} // end ~Bell destructor
+
+// ring bell
+void Bell::ringBell() const
+{
+ cout << "elevator rings its bell" << endl;
+
+} // end function ringBell
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/bell.h b/Bachelor/Prog1/examples/elevator/bell.h new file mode 100644 index 0000000..a0ef0bc --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/bell.h @@ -0,0 +1,30 @@ +// Fig. 7.31: bell.h
+// Bell class definition.
+#ifndef BELL_H
+#define BELL_H
+
+class Bell {
+
+public:
+ Bell(); // constructor
+ ~Bell(); // destructor
+ void ringBell() const; // ring the bell
+
+}; // end class Bell
+
+#endif // BELL_H
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/building.cpp b/Bachelor/Prog1/examples/elevator/building.cpp new file mode 100644 index 0000000..b2eaec3 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/building.cpp @@ -0,0 +1,65 @@ +// Fig. 7.26: building.cpp
+// Member-function definitions for class Building.
+#include <iostream>
+
+using std::cout;
+using std::cin;
+using std::endl;
+
+#include "building.h" // Building class definition
+
+// constructor
+Building::Building()
+ : floor1( Floor::FLOOR1, elevator ),
+ floor2( Floor::FLOOR2, elevator ),
+ elevator( floor1, floor2 ),
+ scheduler( floor1, floor2 )
+{
+ cout << "building constructed" << endl;
+
+} // end Building constructor
+
+// destructor
+Building::~Building()
+{
+ cout << "building destructed" << endl;
+
+} // end ~Building destructor
+
+// function to control simulation
+void Building::runSimulation( int totalTime )
+{
+ int currentTime = 0;
+
+ while ( currentTime < totalTime ) {
+ clock.tick(); // increment time
+ currentTime = clock.getTime(); // get new time
+ cout << "TIME: " << currentTime << endl;
+
+ // process person arrivals for currentTime
+ scheduler.processTime( currentTime );
+
+ // process elevator events for currentTime
+ elevator.processTime( currentTime );
+
+ // wait for Enter key press, so user can view output
+ cin.get();
+
+ } // end while
+
+} // end function runSimulation
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/building.h b/Bachelor/Prog1/examples/elevator/building.h new file mode 100644 index 0000000..e45e3cd --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/building.h @@ -0,0 +1,42 @@ +// Fig. 7.25: building.h
+// Building class definition.
+#ifndef BUILDING_H
+#define BUILDING_H
+
+#include "elevator.h" // Elevator class definition
+#include "floor.h" // Floor class definition
+#include "clock.h" // Clock class definition
+#include "scheduler.h" // Scheduler class definition
+
+class Building {
+
+public:
+ Building(); // constructor
+ ~Building(); // destructor
+ void runSimulation( int ); // controls simulation
+
+private:
+ Floor floor1; // floor1 object
+ Floor floor2; // floor2 object
+ Elevator elevator; // elevator object
+ Clock clock; // clock object
+ Scheduler scheduler; // scheduler object
+
+}; // end class Building
+
+#endif // BUILDING_H
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/clock.cpp b/Bachelor/Prog1/examples/elevator/clock.cpp new file mode 100644 index 0000000..466f392 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/clock.cpp @@ -0,0 +1,52 @@ +// Fig. 7.28: clock.cpp
+// Member-function definitions for class Clock.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include "clock.h" // Clock class definition
+
+// constructor
+Clock::Clock()
+ : time( 0 ) // initialize time to 0
+{
+ cout << "clock constructed" << endl;
+
+} // end Clock constructor
+
+// destructor
+Clock::~Clock()
+{
+ cout << "clock destructed" << endl;
+
+} // end ~Clock destructor
+
+// increment time by 1
+void Clock::tick()
+{
+ time++;
+
+} // end function tick
+
+// return current time
+int Clock::getTime() const
+{
+ return time;
+
+} // end function getTime
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/clock.h b/Bachelor/Prog1/examples/elevator/clock.h new file mode 100644 index 0000000..29bb55a --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/clock.h @@ -0,0 +1,34 @@ +// Fig. 7.27: clock.h
+// Clock class definition.
+#ifndef CLOCK_H
+#define CLOCK_H
+
+class Clock {
+
+public:
+ Clock(); // constructor
+ ~Clock(); // destructor
+ void tick(); // increment clock by one second
+ int getTime() const; // returns clock's current time
+
+private:
+ int time; // clock's time
+
+}; // end class Clock
+
+#endif // CLOCK_H
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/door.cpp b/Bachelor/Prog1/examples/elevator/door.cpp new file mode 100644 index 0000000..047fc1b --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/door.cpp @@ -0,0 +1,81 @@ +// Fig. 7.36: door.cpp
+// Member-function definitions for class Door.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include "door.h" // Door class definition
+#include "person.h" // Person class definition
+#include "floor.h" // Floor class definition
+#include "elevator.h" // Elevator class definition
+
+// constructor
+Door::Door()
+ : open( false ) // initialize open to false
+{
+ cout << "door constructed" << endl;
+
+} // end Door constructor
+
+// destructor
+Door::~Door()
+{
+ cout << "door destructed" << endl;
+
+} // end ~Door destructor
+
+// open the door
+void Door::openDoor( Person * const passengerPtr,
+ Person * const nextPassengerPtr, Floor ¤tFloor,
+ Elevator &elevator )
+{
+ if ( !open ) { // if door is not open, open door
+ open = true;
+
+ cout << "elevator opens its door on floor "
+ << currentFloor.getNumber() << endl;
+
+ // if passenger is in elevator, tell person to leave
+ if ( passengerPtr != 0 ) {
+ passengerPtr->exitElevator( currentFloor, elevator );
+ delete passengerPtr; // passenger leaves simulation
+
+ } // end if
+
+ // if passenger waiting to enter elevator,
+ // tell passenger to enter
+ if ( nextPassengerPtr != 0 )
+ nextPassengerPtr->enterElevator(
+ elevator, currentFloor );
+
+ } // end outer if
+
+} // end function openDoor
+
+// close the door
+void Door::closeDoor( const Floor ¤tFloor )
+{
+ if ( open ) { // if door is open, close door
+ open = false;
+ cout << "elevator closes its door on floor "
+ << currentFloor.getNumber() << endl;
+
+ } // end if
+
+} // end function closeDoor
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/door.h b/Bachelor/Prog1/examples/elevator/door.h new file mode 100644 index 0000000..a15a5d1 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/door.h @@ -0,0 +1,40 @@ +// Fig. 7.35: door.h
+// Door class definition.
+#ifndef DOOR_H
+#define DOOR_H
+
+class Person; // forward declaration
+class Floor; // forward declaration
+class Elevator; // forward declaration
+
+class Door {
+
+public:
+ Door(); // constructor
+ ~Door(); // destructor
+
+ void openDoor( Person * const, // opens door
+ Person * const, Floor &, Elevator & );
+ void closeDoor( const Floor & ); // closes door
+
+private:
+ bool open; // open or closed
+
+};
+
+#endif // DOOR_H
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/elevator.cpp b/Bachelor/Prog1/examples/elevator/elevator.cpp new file mode 100644 index 0000000..1f271c1 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/elevator.cpp @@ -0,0 +1,231 @@ +// Fig. 7.42: elevator.cpp
+// Member-function definitions for class Elevator.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include "elevator.h" // Elevator class definition
+#include "person.h" // Person class definition
+#include "floor.h" // Floor class definition
+
+// constants that represent time required to travel
+// between floors and directions of the elevator
+const int Elevator::ELEVATOR_TRAVEL_TIME = 5;
+const int Elevator::UP = 0;
+const int Elevator::DOWN = 1;
+
+// constructor
+Elevator::Elevator( Floor &firstFloor, Floor &secondFloor )
+ : elevatorButton( *this ),
+ currentBuildingClockTime( 0 ),
+ moving( false ),
+ direction( UP ),
+ currentFloor( Floor::FLOOR1 ),
+ arrivalTime( 0 ),
+ floor1NeedsService( false ),
+ floor2NeedsService( false ),
+ floor1Ref( firstFloor ),
+ floor2Ref( secondFloor ),
+ passengerPtr( 0 )
+{
+ cout << "elevator constructed" << endl;
+
+} // end Elevator constructor
+
+// destructor
+Elevator::~Elevator()
+{
+ delete passengerPtr;
+ cout << "elevator destructed" << endl;
+
+} // end ~Elevator destructor
+
+// give time to elevator
+void Elevator::processTime( int time )
+{
+ currentBuildingClockTime = time;
+
+ if ( moving ) // elevator is moving
+ processPossibleArrival();
+
+ else // elevator is not moving
+ processPossibleDeparture();
+
+ if ( !moving )
+ cout << "elevator at rest on floor "
+ << currentFloor << endl;
+
+} // end function processTime
+
+// when elevator is moving, determine if it should stop
+void Elevator::processPossibleArrival()
+{
+ // if elevator arrives at destination floor
+ if ( currentBuildingClockTime == arrivalTime ) {
+
+ currentFloor = // update current floor
+ ( currentFloor == Floor::FLOOR1 ?
+ Floor::FLOOR2 : Floor::FLOOR1 );
+
+ direction = // update direction
+ ( currentFloor == Floor::FLOOR1 ? UP : DOWN );
+
+ cout << "elevator arrives on floor "
+ << currentFloor << endl;
+
+ // process arrival at currentFloor
+ arriveAtFloor( currentFloor == Floor::FLOOR1 ?
+ floor1Ref : floor2Ref );
+
+ return;
+
+ } // end if
+
+ // elevator still moving
+ cout << "elevator moving "
+ << ( direction == UP ? "up" : "down" ) << endl;
+
+} // end function processPossibleArrival
+
+// determine whether elevator should move
+void Elevator::processPossibleDeparture()
+{
+ // this floor needs service?
+ bool currentFloorNeedsService =
+ currentFloor == Floor::FLOOR1 ?
+ floor1NeedsService : floor2NeedsService;
+
+ // other floor needs service?
+ bool otherFloorNeedsService =
+ currentFloor == Floor::FLOOR1 ?
+ floor2NeedsService : floor1NeedsService;
+
+ // service this floor (if needed)
+ if ( currentFloorNeedsService ) {
+ arriveAtFloor( currentFloor == Floor::FLOOR1 ?
+ floor1Ref : floor2Ref );
+
+ return;
+ }
+
+ // service other floor (if needed)
+ if ( otherFloorNeedsService )
+ prepareToLeave( true );
+
+} // end function processPossibleDeparture
+
+// arrive at a particular floor
+void Elevator::arriveAtFloor( Floor& arrivalFloor )
+{
+ moving = false; // reset state
+
+ cout << "elevator resets its button" << endl;
+ elevatorButton.resetButton();
+
+ bell.ringBell();
+
+ // notify floor that elevator has arrived
+ Person *floorPersonPtr = arrivalFloor.elevatorArrived();
+
+ door.openDoor(
+ passengerPtr, floorPersonPtr, arrivalFloor, *this );
+
+ // this floor needs service?
+ bool currentFloorNeedsService =
+ currentFloor == Floor::FLOOR1 ?
+ floor1NeedsService : floor2NeedsService;
+
+ // other floor needs service?
+ bool otherFloorNeedsService =
+ currentFloor == Floor::FLOOR1 ?
+ floor2NeedsService : floor1NeedsService;
+
+ // if this floor does not need service
+ // prepare to leave for the other floor
+ if ( !currentFloorNeedsService )
+ prepareToLeave( otherFloorNeedsService );
+
+ else // otherwise, reset service flag
+ currentFloor == Floor::FLOOR1 ?
+ floor1NeedsService = false: floor2NeedsService = false;
+
+} // end function arriveAtFloor
+
+// request service from elevator
+void Elevator::summonElevator( int floor )
+{
+ // set appropriate servicing flag
+ floor == Floor::FLOOR1 ?
+ floor1NeedsService = true : floor2NeedsService = true;
+
+} // end function summonElevator
+
+// accept a passenger
+void Elevator::passengerEnters( Person * const personPtr )
+{
+ // board passenger
+ passengerPtr = personPtr;
+
+ cout << "person " << passengerPtr->getID()
+ << " enters elevator from floor "
+ << currentFloor << endl;
+
+} // end function passengerEnters
+
+// notify elevator that passenger is exiting
+void Elevator::passengerExits()
+{
+ passengerPtr = 0;
+
+} // end function passengerExits
+
+// prepare to leave a floor
+void Elevator::prepareToLeave( bool leaving )
+{
+ // get reference to current floor
+ Floor &thisFloor =
+ currentFloor == Floor::FLOOR1 ? floor1Ref : floor2Ref;
+
+ // notify floor that elevator may be leaving
+ thisFloor.elevatorLeaving();
+
+ door.closeDoor( thisFloor );
+
+ if ( leaving ) // leave, if necessary
+ move();
+
+} // end function prepareToLeave
+
+// go to other floor
+void Elevator::move()
+{
+ moving = true; // change state
+
+ // schedule arrival time
+ arrivalTime = currentBuildingClockTime +
+ ELEVATOR_TRAVEL_TIME;
+
+ cout << "elevator begins moving "
+ << ( direction == DOWN ? "down " : "up " )
+ << "to floor "
+ << ( direction == DOWN ? '1' : '2' )
+ << " (arrives at time " << arrivalTime << ')'
+ << endl;
+
+} // end function move
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/elevator.h b/Bachelor/Prog1/examples/elevator/elevator.h new file mode 100644 index 0000000..e032607 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/elevator.h @@ -0,0 +1,75 @@ +// Fig. 7.41: elevator.h
+// Elevator class definition.
+#ifndef ELEVATOR_H
+#define ELEVATOR_H
+
+#include "elevatorButton.h"
+#include "door.h"
+#include "bell.h"
+
+class Floor; // forward declaration
+class Person; // forward declaration
+
+class Elevator {
+
+public:
+ Elevator( Floor &, Floor & ); // constructor
+ ~Elevator(); // destructor
+ void summonElevator( int ); // request to service floor
+ void prepareToLeave( bool ); // prepare to leave
+ void processTime( int ); // give current time to elevator
+ void passengerEnters( Person * const ); // board a passenger
+ void passengerExits(); // exit a passenger
+
+ // public object accessible to client code with
+ // access to Elevator object
+ ElevatorButton elevatorButton;
+
+private:
+
+ // utility functions
+ void processPossibleArrival();
+ void processPossibleDeparture();
+ void arriveAtFloor( Floor & );
+ void move();
+
+ // static constants that represent time required to travel
+ // between floors and directions of the elevator
+ static const int ELEVATOR_TRAVEL_TIME;
+ static const int UP;
+ static const int DOWN;
+
+ // data members
+ int currentBuildingClockTime; // current time
+ bool moving; // elevator state
+ int direction; // current direction
+ int currentFloor; // current location
+ int arrivalTime; // time to arrive at a floor
+ bool floor1NeedsService; // floor1 service flag
+ bool floor2NeedsService; // floor2 service flag
+
+ Floor &floor1Ref; // reference to floor1
+ Floor &floor2Ref; // reference to floor2
+ Person *passengerPtr; // pointer to passenger
+
+ Door door; // door object
+ Bell bell; // bell object
+
+}; // end class Elevator
+
+#endif // ELEVATOR_H
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/elevatorButton.cpp b/Bachelor/Prog1/examples/elevator/elevatorButton.cpp new file mode 100644 index 0000000..f1e3a51 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/elevatorButton.cpp @@ -0,0 +1,57 @@ +// Fig. 7.38: elevatorButton.cpp:
+// Member-function definitions for class ElevatorButton.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include "elevatorButton.h" // ElevatorButton class definition
+#include "elevator.h" // Elevator class definition
+
+// constructor
+ElevatorButton::ElevatorButton( Elevator &elevatorHandle )
+ : pressed( false ),
+ elevatorRef( elevatorHandle )
+{
+ cout << "elevator button constructed" << endl;
+
+} // end ElevatorButton constructor
+
+// destructor
+ElevatorButton::~ElevatorButton()
+{
+ cout << "elevator button destructed" << endl;
+
+} // end ~ElevatorButton destructor
+
+// press button and signal elevator to prepare to leave floor
+void ElevatorButton::pressButton()
+{
+ pressed = true;
+ cout << "elevator button tells elevator to prepare to leave"
+ << endl;
+ elevatorRef.prepareToLeave( true );
+
+} // end function pressButton
+
+// reset button
+void ElevatorButton::resetButton()
+{
+ pressed = false;
+
+} // end function resetButton
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/elevatorButton.h b/Bachelor/Prog1/examples/elevator/elevatorButton.h new file mode 100644 index 0000000..7b2c5be --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/elevatorButton.h @@ -0,0 +1,40 @@ +// Fig. 7.37: elevatorButton.h
+// ElevatorButton class definition.
+#ifndef ELEVATORBUTTON_H
+#define ELEVATORBUTTON_H
+
+class Elevator; // forward declaration
+
+class ElevatorButton {
+
+public:
+ ElevatorButton( Elevator & ); // constructor
+ ~ElevatorButton(); // destructor
+
+ void pressButton(); // press the button
+ void resetButton(); // reset the button
+
+private:
+ bool pressed; // state of button
+
+ // reference to elevator containing this button
+ Elevator &elevatorRef;
+
+}; // end class ElevatorButton
+
+#endif // ELEVATORBUTTON_H
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/floor.cpp b/Bachelor/Prog1/examples/elevator/floor.cpp new file mode 100644 index 0000000..c651095 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/floor.cpp @@ -0,0 +1,98 @@ +// Fig. 7.44: floor.cpp
+// Member-function definitions for class Floor.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include "floor.h" // Floor class definition
+#include "person.h" // Person class definition
+#include "elevator.h" // Elevator class definition
+#include "door.h" // Door class definition
+
+// static constants that represent the floor numbers
+const int Floor::FLOOR1 = 1;
+const int Floor::FLOOR2 = 2;
+
+// constructor
+Floor::Floor(int number, Elevator &elevatorHandle )
+ : floorButton( number, elevatorHandle ),
+ floorNumber( number ),
+ elevatorRef( elevatorHandle ),
+ occupantPtr ( 0 ),
+ light( floorNumber )
+{
+ cout << "floor " << floorNumber << " constructed" << endl;
+
+} // end Floor constructor
+
+// destructor
+Floor::~Floor()
+{
+ delete occupantPtr;
+ cout << "floor " << floorNumber << " destructed" << endl;
+
+} // end ~Floor destructor
+
+// determine whether floor is occupied
+bool Floor::isOccupied() const
+{
+ return ( occupantPtr != 0 );
+
+} // end function isOccupied
+
+// return this floor's number
+int Floor::getNumber() const
+{
+ return floorNumber;
+
+} // end function getNumber
+
+// person arrives on floor
+void Floor::personArrives( Person * const personPtr )
+{
+ occupantPtr = personPtr;
+
+} // end function personArrives
+
+// notify floor that elevator has arrived
+Person *Floor::elevatorArrived()
+{
+ cout << "floor " << floorNumber
+ << " resets its button" << endl;
+
+ floorButton.resetButton();
+ light.turnOn();
+
+ return occupantPtr;
+
+} // end function elevatorArrived
+
+// tell floor that elevator is leaving
+void Floor::elevatorLeaving()
+{
+ light.turnOff();
+
+} // end function elevatorLeaving
+
+// notifies floor that person is leaving
+void Floor::personBoardingElevator()
+{
+ occupantPtr = 0; // person no longer on floor
+
+} // end function personBoardingElevator
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/floor.h b/Bachelor/Prog1/examples/elevator/floor.h new file mode 100644 index 0000000..6ead42a --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/floor.h @@ -0,0 +1,63 @@ +// Fig. 7.43: floor.h
+// Floor class definition.
+#ifndef FLOOR_H
+#define FLOOR_H
+
+#include "floorButton.h"
+#include "light.h"
+
+class Elevator; // forward declaration
+class Person; // forward declaration
+
+class Floor {
+
+public:
+ Floor( int, Elevator & ); // constructor
+ ~Floor(); // destructor
+ bool isOccupied() const; // return true if floor occupied
+ int getNumber() const; // return floor's number
+
+ // pass a handle to new person coming on floor
+ void personArrives( Person * const );
+
+ // notify floor that elevator has arrived
+ Person *elevatorArrived();
+
+ // notify floor that elevator is leaving
+ void elevatorLeaving();
+
+ // notify floor that person is leaving floor
+ void personBoardingElevator();
+
+ // static constants representing floor numbers
+ static const int FLOOR1;
+ static const int FLOOR2;
+
+ // public FloorButton object accessible to
+ // any client code with access to a Floor
+ FloorButton floorButton;
+
+private:
+ const int floorNumber; // the floor's number
+ Elevator &elevatorRef; // reference to elevator
+ Person *occupantPtr; // pointer to person on floor
+ Light light; // light object
+
+}; // end class Floor
+
+#endif // FLOOR_H
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/floorButton.cpp b/Bachelor/Prog1/examples/elevator/floorButton.cpp new file mode 100644 index 0000000..6d19769 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/floorButton.cpp @@ -0,0 +1,62 @@ +// Fig. 7.40: floorButton.cpp
+// Member-function definitions for class FloorButton.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include "floorButton.h"
+#include "elevator.h"
+
+// constructor
+FloorButton::FloorButton( int floor, Elevator &elevatorHandle )
+ : floorNumber( floor ),
+ pressed( false ),
+ elevatorRef( elevatorHandle )
+{
+ cout << "floor " << floorNumber << " button constructed"
+ << endl;
+
+} // end FloorButton constructor
+
+// destructor
+FloorButton::~FloorButton()
+{
+ cout << "floor " << floorNumber << " button destructed"
+ << endl;
+
+} // end ~FloorButton destructor
+
+// press the button
+void FloorButton::pressButton()
+{
+ pressed = true;
+ cout << "floor " << floorNumber
+ << " button summons elevator" << endl;
+
+ // call elevator to this floor
+ elevatorRef.summonElevator( floorNumber );
+
+} // end function pressButton
+
+// reset button
+void FloorButton::resetButton()
+{
+ pressed = false;
+
+} // end function resetButton
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/floorButton.h b/Bachelor/Prog1/examples/elevator/floorButton.h new file mode 100644 index 0000000..b09c6e5 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/floorButton.h @@ -0,0 +1,42 @@ +// Fig. 7.39: floorButton.h
+// FloorButton class definition.
+#ifndef FLOORBUTTON_H
+#define FLOORBUTTON_H
+
+class Elevator; // forward declaration
+
+class FloorButton {
+
+public:
+ FloorButton( int, Elevator & ); // constructor
+ ~FloorButton(); // destructor
+
+ void pressButton(); // press the button
+ void resetButton(); // reset the button
+
+private:
+ const int floorNumber; // button's floor number
+ bool pressed; // button state
+
+ // reference to elevator used to summon
+ // elevator to floor
+ Elevator &elevatorRef;
+
+}; // end class FloorButton
+
+#endif // FLOORBUTTON_H
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/light.cpp b/Bachelor/Prog1/examples/elevator/light.cpp new file mode 100644 index 0000000..1a9eb79 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/light.cpp @@ -0,0 +1,65 @@ +// Fig. 7.34: light.cpp
+// Member-function definitions for class Light.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include "light.h" // Light class definition
+
+// constructor
+Light::Light( int number )
+ : on( false ),
+ floorNumber( number )
+{
+ cout << "floor " << floorNumber << " light constructed"
+ << endl;
+
+} // end Light constructor
+
+// destuctor
+Light::~Light()
+{
+ cout << "floor " << floorNumber
+ << " light destructed" << endl;
+
+} // end ~Light destructor
+
+// turn light on
+void Light::turnOn()
+{
+ if ( !on ) { // if light not on, turn it on
+ on = true;
+ cout << "floor " << floorNumber
+ << " light turns on" << endl;
+
+ } // end if
+
+} // end function turnOn
+
+// turn light off
+void Light::turnOff()
+{
+ if ( on ) { // if light is on, turn it off
+ on = false;
+ cout << "floor " << floorNumber
+ << " light turns off" << endl;
+
+ } // end if
+
+} // end function turnOff
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/light.h b/Bachelor/Prog1/examples/elevator/light.h new file mode 100644 index 0000000..88e920d --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/light.h @@ -0,0 +1,36 @@ +// Fig. 7.33: light.h
+// Light class definition.
+#ifndef LIGHT_H
+#define LIGHT_H
+
+class Light {
+
+public:
+ Light( int ); // constructor
+ ~Light(); // destructor
+
+ void turnOn(); // turns light on
+ void turnOff(); // turns light off
+
+private:
+ bool on; // true if on; false if off
+ const int floorNumber; // floor number that contains light
+
+}; // end class Light
+
+#endif // LIGHT_H
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/person.cpp b/Bachelor/Prog1/examples/elevator/person.cpp new file mode 100644 index 0000000..72e070d --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/person.cpp @@ -0,0 +1,91 @@ +// Fig. 7.46: person.cpp
+// Member-function definitions for class Person.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include "person.h" // Person class definition
+#include "floor.h" // Floor class definition
+#include "elevator.h" // Elevator class definition
+
+// initialize static member personCount
+int Person::personCount = 0;
+
+// constructor
+Person::Person( int destFloor )
+ : ID( ++personCount ),
+ destinationFloor( destFloor )
+{
+ cout << "person " << ID << " constructed" << endl;
+
+} // end Person constructor
+
+// destructor
+Person::~Person()
+{
+ cout << "(person " << ID << " destructor invoked)" << endl;
+
+} // end ~Person destructor
+
+// return person's ID number
+int Person::getID() const
+{
+ return ID;
+
+} // end function getID
+
+// person walks onto a floor
+void Person::stepOntoFloor( Floor& floor )
+{
+ // notify floor person is coming
+ cout << "person " << ID << " steps onto floor "
+ << floor.getNumber() << endl;
+ floor.personArrives( this );
+
+ // press button on floor
+ cout << "person " << ID
+ << " presses floor button on floor "
+ << floor.getNumber() << endl;
+ floor.floorButton.pressButton();
+
+} // end function stepOntoFloor
+
+// person enters elevator
+void Person::enterElevator( Elevator &elevator, Floor &floor )
+{
+ floor.personBoardingElevator(); // person leaves floor
+
+ elevator.passengerEnters( this ); // person enters elevator
+
+ // press button on elevator
+ cout << "person " << ID
+ << " presses elevator button" << endl;
+ elevator.elevatorButton.pressButton();
+
+} // end function enterElevator
+
+// person exits elevator
+void Person::exitElevator(
+ const Floor &floor, Elevator &elevator ) const
+{
+ cout << "person " << ID << " exits elevator on floor "
+ << floor.getNumber() << endl;
+ elevator.passengerExits();
+
+} // end function exitElevator
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/person.h b/Bachelor/Prog1/examples/elevator/person.h new file mode 100644 index 0000000..7a6f10c --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/person.h @@ -0,0 +1,42 @@ +// Fig. 7.45: person.h
+// Person class definition.
+#ifndef PERSON_H
+#define PERSON_H
+
+class Floor; // forward declaration
+class Elevator; // forward declaration
+
+class Person {
+
+public:
+ Person( int ); // constructor
+ ~Person(); // destructor
+ int getID() const; // returns person's ID
+
+ void stepOntoFloor( Floor & );
+ void enterElevator( Elevator &, Floor & );
+ void exitElevator( const Floor &, Elevator & ) const;
+
+private:
+ static int personCount; // total number of people
+ const int ID; // person's unique ID #
+ const int destinationFloor; // destination floor #
+
+}; // end class Person
+
+#endif // PERSON_H
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/scheduler.cpp b/Bachelor/Prog1/examples/elevator/scheduler.cpp new file mode 100644 index 0000000..73bdebb --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/scheduler.cpp @@ -0,0 +1,134 @@ +// Fig. 7.30: scheduler.cpp
+// Member-function definitions for class Scheduler.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <new>
+#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 constructed" << endl;
+
+ // schedule first arrivals for floor 1 and floor 2
+ scheduleTime( floor1Ref );
+ scheduleTime( floor2Ref );
+
+} // end Scheduler constructor
+
+// destructor
+Scheduler::~Scheduler()
+{
+ cout << "scheduler destructed" << 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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/elevator/scheduler.h b/Bachelor/Prog1/examples/elevator/scheduler.h new file mode 100644 index 0000000..a035d08 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/scheduler.h @@ -0,0 +1,53 @@ +// Fig. 7.29: scheduler.h
+// Scheduler class definition.
+#ifndef SCHEDULER_H
+#define SCHEDULER_H
+
+class Floor; // forward declaration
+
+class Scheduler {
+
+public:
+ Scheduler( Floor &, Floor & ); // constructor
+ ~Scheduler(); // destructor
+ void processTime( int ); // set scheduler's time
+
+private:
+ // schedule arrival to a floor
+ void scheduleTime( const Floor & );
+
+ // delay arrival to a floor
+ void delayTime( const Floor & );
+
+ // create new person; place on floor
+ void createNewPerson( Floor & );
+
+ // handle person arrival on a floor
+ void handleArrivals( Floor &, int );
+
+ int currentClockTime;
+
+ Floor &floor1Ref;
+ Floor &floor2Ref;
+
+ int floor1ArrivalTime;
+ int floor2ArrivalTime;
+
+}; // end class Scheduler
+
+#endif // SCHEDULER_H
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/
|
