-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineManager.cpp
130 lines (108 loc) · 2.89 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Author: Kathleen Monks
// Purpose: Final Project for Seneca's OOP345
// Date of completion: 2021-11-27
// ==========================================
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include "LineManager.h"
#include "Workstation.h"
#include "Utilities.h"
using namespace std;
namespace sdds
{
LineManager::LineManager(const std::string& file, const std::vector<Workstation*>& stations)
{
ifstream ifstr;
ifstr.open(file);
if (!ifstr.is_open())
{
throw "Error: Failure to open " + file;
}
else
{
string record{ "" };
string stationName{ "" };
string nextStationName{ "" };
Utilities util; // to extract
size_t next_pos{0u};
bool more;
while (!ifstr.eof())
{
getline(ifstr, record); // stationName|nextStationName
next_pos = 0u;
more = true;
stationName = util.extractToken(record, next_pos, more);
auto station = *find_if(stations.begin(), stations.end(), [stationName](Workstation* ws) // find matching station
{
return ws->getItemName() == stationName;
});
if (more) // ensure next station exists
{
nextStationName = util.extractToken(record, next_pos, more);
// find matching next station
auto nextStation = *find_if(stations.begin(), stations.end(), [nextStationName](Workstation* ws)
{
return ws->getItemName() == nextStationName;
});
station->setNextStation(nextStation);
}
activeLine.push_back(station); // set instance vector
}
ifstr.close();
auto firstActive = *std::find_if(activeLine.begin(), activeLine.end(), [&](Workstation* ws)
{
return *std::find_if(activeLine.begin(), activeLine.end(), [&ws](Workstation* next)
{
return (next->getNextStation() && next->getNextStation()->getItemName() == ws->getItemName()) ? false : true;
});
});
m_firstStation = firstActive; // set instance vars
m_cntCustomerOrder = pending.size();
}
}
void LineManager::linkStations()
{
vector<Workstation*> sortedLine;
auto it = m_firstStation;
while (it)
{
sortedLine.push_back(it);
it = it->getNextStation();
}
activeLine = sortedLine;
}
bool LineManager::run(std::ostream& os)
{
static size_t count{0u};
bool isFilled{false};
count++;
os << "Line Manager Iteration: " << count << endl;
if (!pending.empty()) // has a CustomerOrder object
{
*m_firstStation += move(pending.front()); // place front of pending line into first station
pending.pop_front(); // clean up
}
for (auto& s : activeLine)
{
s->fill(os);
}
for (auto& s : activeLine)
{
s->attemptToMoveOrder();
}
if (completed.size() + incomplete.size() == m_cntCustomerOrder)
{
isFilled = true;
}
return isFilled;
}
void LineManager::display(std::ostream& os)const
{
for_each(activeLine.begin(), activeLine.end(), [&os](Workstation* ws)
{
ws->display(os);
});
}
}