-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineManager.cpp
105 lines (93 loc) · 2.66 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
94
95
96
97
98
99
100
101
102
103
104
105
// Name: Jagbir Singh
// Seneca Student ID: 144019221
// Seneca email: [email protected]
// Date of completion: 05/04/2024
//
// I confirm that I am the only author of this file
// and the content was created entirely by me.
#include <vector>
#include <algorithm>
#include <fstream>
#include "Station.h"
#include "Utilities.h"
#include "LineManager.h"
using namespace std;
namespace seneca
{
LineManager::LineManager(const string& file, const vector<Workstation*>& stations)
{
try
{
ifstream in(file);
m_cntCustomerOrder = g_pending.size();
while (in)
{
Utilities ut;
bool more{ true };
size_t pos{};
string str{};
string first{};
string next{};
getline(in, str);
first = ut.extractToken(str, pos, more);
if (more)
next = ut.extractToken(str, pos, more);
for_each(stations.begin(), stations.end(), [&](Workstation* firstWorkstation)
{
if (firstWorkstation->getItemName() == first)
{
for_each(stations.begin(), stations.end(), [&](Workstation* nextWorkstation)
{
if (nextWorkstation->getItemName() == next)
firstWorkstation->setNextStation(nextWorkstation);
});
m_activeLine.push_back(firstWorkstation);
} });
m_firstStation = m_activeLine.front();
}
}
catch (...)
{
throw "ERROR: LineManager Constructor";
}
}
void LineManager::reorderStations()
{
vector<Workstation*> tempVec;
Workstation* tempWorkstation{ nullptr };
size_t cnt{};
while (cnt < m_activeLine.size())
{
for_each(m_activeLine.begin(), m_activeLine.end(), [&](Workstation* workstation)
{ if (workstation->getNextStation() == tempWorkstation)
{
tempVec.push_back(workstation);
tempWorkstation = workstation;
cnt++;
} });
}
reverse(tempVec.begin(), tempVec.end());
m_firstStation = tempVec[0];
m_activeLine = tempVec;
}
bool LineManager::run(ostream& os)
{
static size_t cnt{};
os << "Line Manager Iteration: " << ++cnt << endl;
if (!g_pending.empty())
{
*m_firstStation += move(g_pending.front());
g_pending.pop_front();
}
for_each(m_activeLine.begin(), m_activeLine.end(), [&](Workstation* workstation)
{ workstation->fill(os); });
for_each(m_activeLine.begin(), m_activeLine.end(), [&](Workstation* workstation)
{ workstation->attemptToMoveOrder(); });
return g_completed.size() + g_incomplete.size() == m_cntCustomerOrder;
}
void LineManager::display(ostream& os) const
{
for_each(m_activeLine.begin(), m_activeLine.end(), [&](Workstation* workstation)
{ workstation->display(os); });
}
}