-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskUnit.java
More file actions
406 lines (318 loc) · 10.4 KB
/
Copy pathTaskUnit.java
File metadata and controls
406 lines (318 loc) · 10.4 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
public class TaskUnit {
private ArrayList<TaskSubUnit> subUnits;
private ArrayList<Integer> schedules;
private String unitCode;
private TaskModel parent;
private String unitTitle;
private float pretUnitar;
private String unitateMetric;
private float cantitate;
private float ore;
private float pretTotal;
private float material;
private float manopera;
private float utilaj;
private float transport;
public TaskUnit(TaskModel parent) {
schedules = new ArrayList<Integer>();
subUnits = new ArrayList<>();
this.parent = parent;
this.unitCode = "new";
initialiseSubUnits();
}
public TaskUnit(String projectFileLine, TaskModel parent) throws Exception { // line = 'unitCode@tablemark1@tablemark2@...'
schedules = new ArrayList<Integer>();
processProjectFileLine(projectFileLine);
this.parent = parent;
subUnits = new ArrayList<>();
initialiseSubUnits();
loadUnit(unitCode);
}
public TaskUnit(TaskModel parent, String unitCode){
schedules = new ArrayList<Integer>();
this.parent = parent;
this.unitCode = unitCode;
subUnits = new ArrayList<>();
initialiseSubUnits();
try{
loadUnit(unitCode);
}
catch (FileNotFoundException e){
this.unitCode = unitCode;
}
catch (Exception e) {
e.printStackTrace();
}
}
public void processProjectFileLine(String line) {
String[] tokens = line.split(Finals.TOK_D);
unitCode = tokens[0];
for (int i = 1; i < tokens.length; i++) {
schedules.add(Integer.parseInt(tokens[i]));
}
}
public String getProjectFileLine(){
String res = "" + unitCode + Finals.TOK_D;
for (Integer i : schedules) {
res += i + Finals.TOK_D;
}
return res;
}
public void initialiseSubUnits() {
for (int i = 0; i < 4; i ++)
{
subUnits.add(new TaskSubUnit(i, this));
}
}
public String getHeader(){
// return Finals.UNIT_INITAL + Finals.TOK_D + unitTitle + Finals.TOK_D + pretUnitar + Finals.TOK_D + unitateMetric
// + Finals.TOK_D + cantitate + Finals.TOK_D + ore + Finals.TOK_D + pretTotal + Finals.TOK_D
// + material + Finals.TOK_D + manopera + Finals.TOK_D + utilaj + Finals.TOK_D
// + transport;
//
return Finals.UNIT_INITAL + Finals.TOK_D + unitTitle + Finals.TOK_D + unitateMetric
+ Finals.TOK_D + cantitate + Finals.TOK_D + ore + Finals.TOK_D;
}
public String[] getTableHeader(){
String[] res = new String[Finals.LENGTH_OF_UNIT_TABLE];
res[1] = unitTitle;
res[2] = unitCode;
res[3] = "" + (double)Math.round(pretUnitar * 100d) / 100d;
res[4] = unitateMetric;
res[5] = "" + cantitate;
res[6] = "" + (double)Math.round(ore * 100d) / 100d;
res[7] = "" + (double)Math.round(pretTotal * 100d) / 100d;
res[8] = "" + (double)Math.round(material * 100d) / 100d;
res[9] = "" + (double)Math.round(manopera * 100d) / 100d;
res[10] = "" + (double)Math.round(utilaj * 100d) / 100d;
res[11] = "" + (double)Math.round(transport * 100d) / 100d;
return res;
}
public void processHeader(String line) throws Exception{
String[] tokens = line.split(Finals.TOK_D);
if (tokens.length != Finals.NR_OF_FIELDS_IN_UNIT){
System.out.println("" + tokens.length);
for (String token : tokens)
{
System.out.println("sor: " + token);
}
throw new InvalidUnitHeaderException("incorrect number of fields in header");
}
int i = 1;
unitTitle = tokens[i];
i++;
// String nextTok = tokens[i];
// pretUnitar = Float.parseFloat(nextTok);
// i++;
unitateMetric = tokens[i];
i++;
String nextTok = tokens[i];
cantitate = Float.parseFloat(nextTok);
i++;
nextTok = tokens[i];
ore = Float.parseFloat(nextTok);
// i++;
// nextTok = tokens[i];
// pretTotal = Float.parseFloat(nextTok);
// i++;
//
// nextTok = tokens[i];
// material = Float.parseFloat(nextTok);
// i++;
//
// nextTok = tokens[i];
// manopera = Float.parseFloat(nextTok);
// i++;
//
// nextTok = tokens[i];
// utilaj = Float.parseFloat(nextTok);
// i++;
//
// nextTok = tokens[i];
// transport = Float.parseFloat(nextTok);
}
public void loadUnit(String unitCode) throws Exception{
int currentSubUnit = -1;
Scanner scan = new Scanner(new File(Finals.UNITS_PATH + unitCode + ".txt"));
if(scan.hasNextLine()) {
String line = scan.nextLine();
if(!line.substring(0,1).equals(Finals.UNIT_INITAL)){
throw new MissingUnitHeaderException("nincs hader");
}
else {
processHeader(line);
}
}
else
{
throw new MissingUnitHeaderException("URES FILE");
}
while(scan.hasNextLine()){
String line = scan.nextLine();
if(line.equals(Finals.UNIT_END_STRING)){
break;
}
if (line.substring(0,1).equals(Finals.SUB_UNIT_INITAL)){
currentSubUnit ++;
subUnits.get(currentSubUnit).processHeader(line);
}
else{
if (currentSubUnit == -1){
throw new MissingSubUnitException("missing subunit");
}
try {
subUnits.get(currentSubUnit).loadLine(line);
} catch (Exception e) {
e.printStackTrace();
}
}
}
scan.close();
clculateAll();
}
public void saveUnit() throws Exception{
//FileWriter fw = new FileWriter(Finals.UNITS_PATH + unitCode + "_" + unitTitle + ".txt");
//ennel az a baj ugye hgoy mi ugy mentenenk el ha ez nem lenne kicommentelve hogy
//unitCode_unitTitle.txt, viszont a load az arra keres hogy unitCode.txt
FileWriter fw = new FileWriter(Finals.UNITS_PATH + unitCode + ".txt");
fw.write(getHeader() + "\n");
for (int i = 0; i < Finals.NUMBER_OF_SUBUNITS; i++){
String currentLine = subUnits.get(i).saveLine();
while(!currentLine.equals(Finals.NO_MORE_ITEMS))
{
fw.write(currentLine + "\n");
currentLine = subUnits.get(i).saveLine();
}
}
fw.write(Finals.UNIT_END_STRING);
fw.close();
}
// public void insertSubUnit(int subUnitIndex){
//
// if(subUnitIndex == subUnits.size()){
// subUnits.add(new TaskSubUnit());
// }
// else{
// subUnits.add(subUnitIndex,new TaskSubUnit());
// }
// }
public void deleteSubUnit(int subUnitIndex){
subUnits.remove(subUnitIndex);
}
public void insertRow(int subUnitIndex, int rowIndex){
subUnits.get(subUnitIndex).insertRow(rowIndex);
}
public void deleteRow(int subUnitIndex, int rowIndex){
subUnits.get(subUnitIndex).deleteRow(rowIndex);
}
public String getUnitCode() {
return unitCode;
}
// private void setUnitCode(String unitCode) {
// this.unitCode = unitCode;
// }
//.........................setters
public void setUnitCode(String unitCode) {
this.unitCode = unitCode;
}
public void setUnitTitle(String unitTitle) {
this.unitTitle = unitTitle;
}
public void setUnitateMetric(String unitateMetric) {
this.unitateMetric = unitateMetric;
}
public void setCantitate(float cantitate) {
this.cantitate = cantitate;
calculatePretTotal();
calculateMaterial();
calculateManopera();
calculateUtilaj();
calculateTransport();
for (TaskSubUnit su : subUnits){
su.calculateAll();
}
// for (TaskSubUnit su : subUnits){
// for (TaskRow tr : su.getTaskRows())
// }
}
public void setOre(float ore) {
this.ore = ore;
}
//.........................calculations
public void calculatePretUnitar(){
float res = 0;
for (TaskSubUnit tsu : subUnits){
res += tsu.getSumPretTotalUnitar();
}
pretUnitar = res;
calculatePretTotal();
}
public void calculatePretTotal() {
pretTotal = cantitate * pretUnitar;
}
public void calculateMaterial(){
material = subUnits.get(0).getSumPretTotalUnitar() * cantitate;
parent.calculateMaterial();
}
public void calculateManopera(){
manopera = subUnits.get(1).getSumPretTotalUnitar() * cantitate / 60;
parent.calculateManopera();
}
public void calculateUtilaj(){
utilaj = subUnits.get(2).getSumPretTotalUnitar() * cantitate;
parent.calculateUtilaj();
}
public void calculateTransport(){
transport = subUnits.get(3).getSumPretTotalUnitar() * cantitate;
parent.calculateTransport();
}
public void clculateAll() {
for (TaskSubUnit tsu : subUnits){
tsu.calculateAll();
}
calculatePretUnitar();
}
//.............................GETTERS
public String getUnitTitle() {
return unitTitle;
}
public float getPretUnitar() {
return pretUnitar;
}
public String getUnitateMetric() {
return unitateMetric;
}
public float getCantitate() {
return cantitate;
}
public float getOre() {
return ore;
}
public float getPretTotal() {
return pretTotal;
}
public float getMaterial() {
return material;
}
public float getManopera() {
return manopera;
}
public float getUtilaj() {
return utilaj;
}
public float getTransport() {
return transport;
}
public ArrayList<TaskSubUnit> getSubUnits() {
return subUnits;
}
public ArrayList<Integer> getSchedules() {
return schedules;
}
}