-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineManager.cpp
273 lines (209 loc) · 7.95 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Name:Badal Sarkar
// Seneca Student ID:137226189
// Seneca email:[email protected]
// Date of completion:December 1, 2019
//
// I confirm that I am the only author of this file
// and the content was created entirely by me.
#include <fstream>
#include <iostream>
#include <algorithm>
#include "LineManager.h"
/**********************************************************************************************
custom constructor
parameter:1)a file name as string, this contains information about next task to be performed
2) reference to a vector of Task*
3) reference to a vector of CustomerOrder
steps: 1. read one line from the file, extract two tokens.
2. find second token in the 2nd parameter and store the address, find the first token in the 2nd parameter \
and update the m_pNextTak with the address from step 2
3. if there is only one token, put the m_pNextTask of the token as nullptr
4. store the first token in firstTaskInAssmLine. This will be used to identify
the starting point in the AssemblyLine
5. do this for all the records from the file
6. copy each element from 2nd parameter into data member AssemblyLine
7. move each element from 3rd parameter into data member ToBeFilled
8. update the value of m_cntCustomerOrder with the size of ToBeFilled
*************************************************************************************************/
LineManager::LineManager(const std::string& source, std::vector<Task*>& task_list, std::vector<CustomerOrder>& order_list)
{
//step 1-4:
if(source!="")
{
std::ifstream file (source);
if(file)
{
std::string line;
bool firstLine{true};
while (std::getline(file, line))
{
std::string token1{""}, token2{""};
token1=trim(extractToken(line, '|'));
token2=trim(extractToken(line));
//step 2, 3:
if(token2 !=""){
setupNextTask(task_list, token1, token2);
}
//step 4:
if(firstLine)
{
firstTaskInAssmLine=token1;
firstLine=false;
}
line="";
}
file.clear();
file.close();
}
}
//step 5:
//resize may throw exception
AssemblyLine.resize(task_list.size());
std::copy(task_list.begin(), task_list.end(), AssemblyLine.begin());
//step 6:
/*
ToBeFilled.resize(order_list.size());
std::move(order_list.begin(), order_list.end(), ToBeFilled.begin());
*/
for(size_t i=0; i<order_list.size();i++)
{
ToBeFilled.push_front(std::move(order_list[i]));
}
//step 7:
m_cntCustomerOrder=ToBeFilled.size();
}
/**************************************************************************
setupnexttask function
parameter: a vector of pointer to Task, the name of first Task(TaskA),
the name of second Task(TaskB)
step 1: find TaskA and TaskB in the vector
2: call setNextTask method on TaskA- this will store the reference of the
TaskB in the m_pNextTask of the TaskA
***************************************************************************/
void LineManager::setupNextTask(std::vector<Task*>& task_list, std::string& taskA, std::string& taskB)
{
//step 1:
auto taskA_iter=std::find_if(task_list.begin(), task_list.end(), [&taskA](Task* theTask){
return (theTask->getName()==taskA);
});
auto taskB_iter=std::find_if(task_list.begin(), task_list.end(), [&taskB](Task* theTask){
return (theTask->getName()==taskB);
});
//setp2:
if(taskA_iter!=task_list.end() && taskB_iter!=task_list.end())
(*taskA_iter)->setNextTask(*(*taskB_iter));
}
/******************************************************************************
run function
steps: 1. check if ToBeFilled is empty or not
2. if not empty, Identify the starting point of the AssemblyLine
2. move the last element(CustomerOrder) to the starting task
in the AssemblyLine. CustomerOrder will be added to the front of the
m_orders deque, of the starting task, of the AssemblyLine
3. loop through the AssemblyLine and for each element do the following
- call runProcess(),
- check the order fill state, if it is true, move the order to the Completed deque
- if order fill state is false, call moveTask()
4. if the size of Completed deque is equal to m_cntCustomerOrder, it means
all CustomerOrders have been filled. so return true otherwise false
******************************************************************************/
bool LineManager::run(std::ostream& os)
{
bool result {false};
//step 1:
if(!ToBeFilled.empty())
{
auto firstTask=std::find_if(AssemblyLine.begin(), AssemblyLine.end(),[&](Task* theTask){
return (theTask->getName()==firstTaskInAssmLine);
});
//step 2:
(*(*firstTask))+=std::move(ToBeFilled.back());
ToBeFilled.pop_back();
}
//step 3:
std::for_each(AssemblyLine.begin(), AssemblyLine.end(), [&](Task* theTask)
{
theTask->runProcess(os);
CustomerOrder temp;
if(theTask->getCompleted(temp))
{
Completed.push_back(std::move(temp));
}
});
std::for_each(AssemblyLine.begin(), AssemblyLine.end(), [&](Task* theTask)
{
theTask->moveTask();
});
//step : 4
if(Completed.size()==m_cntCustomerOrder)
{
result=true;
}
return result;
}
/**************************************************************************
displaycompleted function
parameter: ostream
steps: 1. loop through Completed deque and call display function
**************************************************************************/
void LineManager::displayCompleted(std::ostream& os)const
{
if(!Completed.empty())
{
std::for_each(Completed.begin(), Completed.end(), [&](const CustomerOrder& theOrder){
theOrder.display(os);
});
}
}
/*********************************************************************
validatetask function
steps: 1. loop through AssemblyLine and call function validate() on each
element
*********************************************************************/
void LineManager::validateTasks()const
{
if(!AssemblyLine.empty())
{
std::for_each(AssemblyLine.begin(), AssemblyLine.end(), [](Task* theTask)
{
theTask->validate(std::cout);
});
}
}
/**************************************************************************
extracttoken function
this free helper function extracts sbustring till the delimiter,
if delimiter is not found whole string is returned
after extraction, it erases the extracted portion from original string
it returns the extracted string
**************************************************************************/
std::string extractToken(std::string& s, char delimiter)
{
size_t delimiterPos=s.find(delimiter);
std::string extractedString=s.substr(0,delimiterPos);
if(delimiterPos==std::string::npos)
{
s.erase(0, delimiterPos);
}
else
{
s.erase(0, delimiterPos+1);
}
return extractedString;
}
/*************************************************************************
trim function
this function removes leading and trailing space
returns the trimmed string
*************************************************************************/
std::string trim(std::string text)
{
try{
size_t startPos=text.find_first_not_of(' ');
size_t endPos=text.find_last_not_of(' ');
text=text.substr(startPos, ((endPos-startPos)+1));
}catch(...){
//empty
};
return text;
}