-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLineManager.cpp
93 lines (79 loc) · 2.96 KB
/
LineManager.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
/* I have done all the coding by myself and only copied the code that my professor provided to complete my workshops and assignments.
Name: Hayaturehman Ahmadzai
Email: [email protected]
Student ID: 122539166
Created on 2021-07-21.*/
#include <fstream>
#include <algorithm>
#include "Utilities.h"
#include "LineManager.h"
namespace sdds {
LineManager::LineManager(const std::string& file, const std::vector<Workstation*>& stations)
{
std::ifstream file_(file);
if (!file_) throw "Failed to open the file";
else {
Utilities utils;
bool more;
size_t npos = 0;
std::string fileLine, currWSname, nxtWSname;
Workstation* nxtWS = nullptr;
Workstation* currWS = nullptr;
Workstation* firstWS = nullptr;
while (std::getline(file_, fileLine))
{
currWSname = utils.extractToken(fileLine, npos, more);
currWS = *std::find_if(stations.begin(), stations.end(), [&](Workstation* ws) {
return ws->getItemName() == currWSname;
});
activeLine.push_back(currWS);
if (more) {
nxtWSname = utils.extractToken(fileLine, npos, more);
nxtWS = *std::find_if(stations.begin(), stations.end(), [&](Workstation* ws) {
return ws->getItemName() == nxtWSname;
});
currWS->setNextStation(nxtWS);
}
}
for_each(stations.begin(), stations.end(), [&](Workstation* tmp) {
firstWS = *find_if(stations.begin(), stations.end(), [&](Workstation* station) {
return station->getNextStation() == firstWS;
});
});
m_firstStation = firstWS;
}
file_.close();
m_cntCustomerOrder = pending.size();
}
void LineManager::linkStations()
{
const Workstation* ws = m_firstStation;
size_t i{0};
do
{
activeLine[i++] = const_cast<Workstation*>(ws);
ws = ws->getNextStation();
} while (ws != nullptr);
}
bool LineManager::run(std::ostream& os){
static size_t iterations = 0;
os << "Line Manager Iteration: " << ++iterations << std::endl;
if (!pending.empty()) {
*m_firstStation += std::move(pending.front());
pending.pop_front();
}
std::for_each(activeLine.begin(),activeLine.end(), [&os](Workstation* ws){
ws->fill(os);
});
std::for_each(activeLine.begin(),activeLine.end(), [](Workstation* ws){
ws->attemptToMoveOrder();
});
return m_cntCustomerOrder == (completed.size() + incomplete.size());
}
void LineManager::display(std::ostream& os) const
{
if (!activeLine.empty())
for (auto& i : activeLine)
i->display(os);
}
}