-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElevatorSimulator.cpp
85 lines (63 loc) · 1.52 KB
/
ElevatorSimulator.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
#include "ElevatorSimulator.hpp"
ElevatorSimulator::ElevatorSimulator() {
m_elevatorCount = 2;
for (int i = 0; i < m_elevatorCount; i++) {
m_pElevatorList.push_back(nullptr);
}
m_pController = nullptr;
}
ElevatorSimulator::ElevatorSimulator(int elevatorCount) {
m_elevatorCount = elevatorCount;
for (int i = 0; i < m_elevatorCount; i++) {
m_pElevatorList.push_back(nullptr);
}
m_pController = nullptr;
}
void ElevatorSimulator::start() {
startController(m_elevatorCount);
for (int i = 0; i < m_elevatorCount; i++) {
startElevator(i);
}
}
void ElevatorSimulator::stop() {
for (int i = 0; i < m_elevatorCount; i++) {
stopElevator(i);
}
stopController();
}
void ElevatorSimulator::startElevator(int id) {
if (m_pElevatorList[id] == nullptr) {
m_pElevatorList[id] = std::make_unique<elevator::Elevator>(std::to_string(id));
}
if (m_pElevatorList[id]->hasStarted()) {
return;
}
m_pElevatorList[id]->start();
}
void ElevatorSimulator::stopElevator(int id) {
if (m_pElevatorList[id] == nullptr) {
return;
}
if (!m_pElevatorList[id]->hasStarted()) {
return;
}
m_pElevatorList[id]->stop();
}
void ElevatorSimulator::startController(int numElevators) {
if (m_pController == nullptr) {
m_pController = std::make_unique<controller::Controller>(numElevators);
}
if (m_pController->hasStarted()) {
return;
}
m_pController->start();
}
void ElevatorSimulator::stopController() {
if (m_pController == nullptr) {
return;
}
if (!m_pController->hasStarted()) {
return;
}
m_pController->stop();
}