-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathElevator.cpp
113 lines (98 loc) · 3.36 KB
/
Elevator.cpp
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
#include "Elevator.hpp"
#include <iostream>
Elevator::Elevator(int id, int maxFloor, int minFloor)
: id(id), currentFloor(minFloor), direction(Direction::IDLE),
status(Status::STOPPED), maxFloor(maxFloor), minFloor(minFloor) {
floorsToVisit.resize(maxFloor + 1, false);
}
int Elevator::getId() const { return id; }
int Elevator::getCurrentFloor() const { return currentFloor; }
Direction Elevator::getDirection() const { return direction; }
Status Elevator::getStatus() const { return status; }
void Elevator::setDirection(Direction direction) {
this->direction = direction;
}
void Elevator::setStatus(Status status) {
this->status = status;
}
void Elevator::addRequest(const Request& request) {
requests.push(request);
floorsToVisit[request.getDestinationFloor()] = true;
if (direction == Direction::IDLE) {
if (request.getDestinationFloor() > currentFloor) {
direction = Direction::UP;
} else if (request.getDestinationFloor() < currentFloor) {
direction = Direction::DOWN;
}
}
}
void Elevator::moveToNextFloor() {
if (status != Status::MOVING) return;
if (direction == Direction::UP && currentFloor < maxFloor) {
currentFloor++;
} else if (direction == Direction::DOWN && currentFloor > minFloor) {
currentFloor--;
}
updateDirection();
}
bool Elevator::hasStopRequest() const {
return floorsToVisit[currentFloor];
}
void Elevator::processFloor() {
if (floorsToVisit[currentFloor]) {
floorsToVisit[currentFloor] = false;
status = Status::STOPPED;
std::cout << "Elevator " << id << " stopped at floor " << currentFloor << std::endl;
// Remove processed requests
while (!requests.empty() && requests.front().getDestinationFloor() == currentFloor) {
requests.pop();
}
}
}
void Elevator::updateDirection() {
if (direction == Direction::UP) {
bool hasHigherFloorRequest = false;
for (int i = currentFloor + 1; i <= maxFloor; i++) {
if (floorsToVisit[i]) {
hasHigherFloorRequest = true;
break;
}
}
if (!hasHigherFloorRequest) {
direction = Direction::DOWN;
}
} else if (direction == Direction::DOWN) {
bool hasLowerFloorRequest = false;
for (int i = currentFloor - 1; i >= minFloor; i--) {
if (floorsToVisit[i]) {
hasLowerFloorRequest = true;
break;
}
}
if (!hasLowerFloorRequest) {
direction = Direction::UP;
}
}
if (!requests.empty()) {
status = Status::MOVING;
} else {
direction = Direction::IDLE;
status = Status::STOPPED;
}
}
void Elevator::displayInfo() const {
std::cout << "Elevator " << id << " - Floor: " << currentFloor;
std::cout << " - Direction: ";
switch (direction) {
case Direction::UP: std::cout << "UP"; break;
case Direction::DOWN: std::cout << "DOWN"; break;
case Direction::IDLE: std::cout << "IDLE"; break;
}
std::cout << " - Status: ";
switch (status) {
case Status::MOVING: std::cout << "MOVING"; break;
case Status::STOPPED: std::cout << "STOPPED"; break;
case Status::MAINTENANCE: std::cout << "MAINTENANCE"; break;
}
std::cout << std::endl;
}