fp-elevators
Elevator.cpp
Overview
Elevator
is a class declared in Elevator.h
and is responsible for representing where an elevator is inside the building along with where it is going. It will service requests that come from the Building
class.
Member Data
servicing
is abool
representing whether the elevator is currently moving to service a request. When an elevator is servicing, it cannot receive another command.currentFloor
is anint
representing on which floor the elevator currently residestargetFloor
is anint
representing which floor an elevator is trying to reach. If an elevator reaches its targetFloor, it will no longer beservicing
and will be eligible to service another request.
Member Functions
Many member functions have been written for you. Please refer to their RME’s for use reference.
You will be writing the implementation for the following functions:
tick
(Difficulty: ★☆☆☆☆)serviceRequest
(Difficulty: ★☆☆☆☆)
tick
/**
* Requires: currentTime is valid
* Modifies: currentFloor, servicing
* Effects: If the currentTime is divisible by TICKS_PER_ELEVATOR_MOVE and
* the elevator is servicing a request:
* Moves the value of current floor by 1 in the direction
* of the target floor
* Also, If the new current floor is the target floor:
* servicing is set to false
*
* Otherwise, does nothing
*
*/
void tick(int currentTime);
tick
is the function that advances the game one turn. In the context of Elevator
, this involves moving it in the correct direction if it is servicing a request. Specifically, we do the following.
- If the
currentTime
is divisible byTICKS_PER_ELEVATOR_MOVE
and it is currently servicing a request, we movecurrentFloor
one floor closer to itstargetFloor
. If the elevator is already at itstargetFloor
, the elevator does not move. - If, after moving the elevator, it has reached its
targetFloor
, we setservicing
to false. It can now service another request.
serviceRequest
/**
* Requires: 0 <= floorNum <= NUM_FLOORS
* Modifies: targetFloor, servicing
* Effects: Sets the targetFloor and marks the Elevator as currently servicing
*/
void serviceRequest(int floorNum);
This is the function that the Building
class will use to control its Elevator
s. It will give the Elevator
a floorNum
which constitutes a request to send the Elevator
to that Floor
. This will update the Elevator
’s targetFloor
, and mark it as servicing
.
Function Table
The table below provides an outline of which other functions each function should call, if any. The functions listed here do not include any library functions you may wish to use. Please look at the starter code section of the spec to see which libraries you are allowed to use.
Function | Other functions it should call |
---|---|
tick() |
This function does not call any other functions |
serviceRequest() |
This function does not call any other functions |