From 8c21925567593249a21992bb1e10d61e71f27739 Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Sat, 6 Oct 2007 11:25:17 -0700 Subject: [PATCH] Add ElevatorController This implements and adds the ElevatorController class to the project. An ElevatorController manages a list of Elevators in a building, deciding which ones to dispatch based on distance. It also can "step the simulation" for all of the Elevators. Signed-off-by: Ira W. Snyder --- Makefile | 2 +- elevatorcontroller.cpp | 105 +++++++++++++++++++++++++++++++++++++++++ elevatorcontroller.hpp | 44 +++++++++++++++++ test.cpp | 24 ++++++++++ 4 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 elevatorcontroller.cpp create mode 100644 elevatorcontroller.hpp diff --git a/Makefile b/Makefile index 8062d9a..116c3ce 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -test: position.o stop.o elevator.o test.o +test: position.o stop.o elevator.o elevatorcontroller.o test.o g++ -o $@ $^ run: test diff --git a/elevatorcontroller.cpp b/elevatorcontroller.cpp new file mode 100644 index 0000000..2baea64 --- /dev/null +++ b/elevatorcontroller.cpp @@ -0,0 +1,105 @@ +#include "elevatorcontroller.hpp" + +ElevatorController::ElevatorController (int floors, int elevators) + : number_of_floors_(floors) + , number_of_elevators_(elevators) + , elevators_() +{ + /* Seed the RNG */ + std::srand ( std::time (NULL) ); + + /* Create and add all of the elevators */ + for (int i=0; i +#include +#include +#include +#include + +typedef std::vector ElevatorList; + +/* Forward-declare Exceptions */ +class bad_elevator { }; +class bad_floor { }; + +class ElevatorController +{ + public: + ElevatorController (int floors, int elevators); + + void call_elevator_to (int floor, Direction direction); + void elevator_request (int elevator_number, int floor); + void move_elevators (); + + private: + int number_of_floors_; + int number_of_elevators_; + + ElevatorList elevators_; + +}; + +/* vim: set ts=4 sts=4 sw=4 noet tw=112: */ + +#endif /* ELEVATORCONTROLLER_HPP */ diff --git a/test.cpp b/test.cpp index 54121a7..6f85ea1 100644 --- a/test.cpp +++ b/test.cpp @@ -6,10 +6,33 @@ using namespace std; #include "stop.hpp" #include "position.hpp" #include "elevator.hpp" +#include "elevatorcontroller.hpp" int main (int argc, char *argv[]) { + const int floors = 10; + const int elevators = 2; + + ElevatorController ec(floors, elevators); + + //ec.elevator_request (0, 2); + ec.call_elevator_to (3, DOWN); + + for (int i=0; i<35; i++) + ec.move_elevators (); + + // Note: without the GUI, this is dependent on choosing the same elevator + // that was randomly chosen by the call_elevator_to() funtion. + // + // This may need to be run a few times to work :) + ec.elevator_request (0, 2); + + for (int i=0; i<35; i++) + ec.move_elevators (); + + +#if TEST_ELEVATOR Elevator e(2); Stop s2(3, DOWN); @@ -29,6 +52,7 @@ int main (int argc, char *argv[]) usleep (500000); e.move (); } +#endif return 0; } -- 2.25.1