-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
452 lines (408 loc) · 19.2 KB
/
Copy pathMain.java
File metadata and controls
452 lines (408 loc) · 19.2 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import ControlSystem.*;
import Exceptions.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Main {
// comandos disponiveis
private static final String QUIT = "EXIT";
private static final String HELP = "HELP";
private static final String REGISTER = "REGISTER";
private static final String USERS = "USERS";
private static final String CREATE = "CREATE";
private static final String PROJECTS = "PROJECTS";
private static final String ARTEFACTS = "ARTEFACTS";
private static final String PROJECT = "PROJECT";
private static final String REVISION = "REVISION";
private static final String MANAGES = "MANAGES";
private static final String KEYWORD = "KEYWORD";
private static final String CONFIDENTIALITY = "CONFIDENTIALITY";
private static final String WORKAHOLICS = "WORKAHOLICS";
private static final String TEAM = "TEAM";
private static final String COMMON = "COMMON";
// feedback dado pelo programa
private static final String BYE_MESSAGE = "Bye!";
private static final String UNKNOWN_COMMAND = "Unknown command. Type help to see available commands.";
private static final String HELP_TITLE = "Available commands:";
private static final String USER_REGISTERED = "User %s was registered as %s with clearance level %d.\n";
private static final String LIST_USERS_TITLE = "All registered users:";
private static final String LIST_PROJECTS_TITLE = "All projects:";
private static final String MANAGER_INFO = "manager %s [%d, %d, %d]\n";
private static final String DEVELOPER_INFO = "developer %s is managed by %s [%d]\n";
private static final String INHOUSE_INFO = "in-house %s is managed by %s [%d, %d, %d, %d]\n";
private static final String OUTSOURCED_INFO = "outsourced %s is managed by %s and developed by %s\n";
private static final String CREATED_PROJECT = "%s project was created.\n";
private static final String TEAM_TITLE = "Latest team members:";
private static final String ADDED_EMPLOYEE_TO_TEAM = "%s: added to the team.\n";
private static final String ARTEFACTS_TITLE = "Latest project artefacts:";
private static final String ADDED_ARTEFACT_TO_PROJECT = "%s: added to the project.\n";
private static final String PROJECT_FORMAT = "%s [%d] managed by %s [%d]:\n";
private static final String EMPLOYEE_INFO = "%s [%d]\n";
private static final String ARTEFACT_INFO = "%s [%d]\n";
private static final String REVISION_INFO = "revision %d %s %s %s\n";
private static final String REVISION_ADDED = "Revision %d of artefact %s was submitted.\n";
private static final String MANAGER_TITLE = "Manager %s:\n";
private static final String REVISION_EMPLOYEE_INFO = "%s, %s, revision %d, %s, %s\n";
private static final String KEYWORD_TITLE = "All projects with keyword %s:\n";
private static final String KEYWORD_INHOUSE = "in-house %s is managed by %s [%d, %d, %d, %d, %s]\n";
private static final String KEYWORD_OUTSOURCED = "outsourced %s is managed by %s and developed by %s\n";
private static final String CONFIDENTIALITY_TITLE = "All projects within levels %d and %d:\n";
private static final String CONFIDENTIALITY_INFO = "%s is managed by %s and has keywords %s\n";
private static final String WORKAHOLICS_INFO = "%s: %d updates, %d projects, last update on %s\n";
private static final String COMMON_INFO = "%s %s have %d projects in common.\n";
// manipulacao de datas
private static final String WRITE_DATE_FORMAT = "dd-MM-yyyy";
public static void main(String[] args) {
ControlSystem system = new ControlSystemClass();
Scanner in = new Scanner(System.in);
String comm = getCommand(in);
while (!comm.equals(QUIT)) {
switch (comm) {
case HELP:
help();
break;
case REGISTER:
register(in, system);
break;
case USERS:
listUsers(system);
break;
case CREATE:
createProject(in, system);
break;
case PROJECTS:
listProjects(system);
break;
case TEAM:
addTeamToProject(in, system);
break;
case ARTEFACTS:
addArtefactsToProject(in, system);
break;
case PROJECT:
projectInfo(in, system);
break;
case REVISION:
addRevisionToArtefact(in, system);
break;
case MANAGES:
managesInfo(in, system);
break;
case KEYWORD:
listProjectsByKeyword(in, system);
break;
case CONFIDENTIALITY:
listByConfidentiality(in, system);
break;
case WORKAHOLICS:
listWokaholics(system);
break;
case COMMON:
listCommon(system);
break;
default:
System.out.println(UNKNOWN_COMMAND);
}
comm = getCommand(in);
}
System.out.println(BYE_MESSAGE);
}
private static String getCommand(Scanner in) {
String comm = in.next();
return comm.toUpperCase();
}
/**
* Prints every available command
*/
private static void help() {
System.out.println(HELP_TITLE);
for (HelpCommands h : HelpCommands.values())
System.out.println(h.getText());
}
private static void register(Scanner in, ControlSystem system) {
String manager = "";
String jobName = in.next().toUpperCase();
String username = in.next();
try {
Job job = system.getJob(jobName);
switch (job) {
case DEVELOPER:
manager = in.next();
}
int level = in.nextInt();
in.nextLine();
switch (job) {
case DEVELOPER:
system.register(username, manager, job, level);
System.out.printf(USER_REGISTERED, username, "developer", level);
break;
case MANAGER:
system.register(username, job, level);
System.out.printf(USER_REGISTERED, username, "manager", level);
break;
}
} catch (UserAlreadyExistsException | ManagerDoesNotExistException e1) {
System.out.println(e1.getMessage());
} catch (UnknownJobPositionException e2) {
System.out.println(e2.getMessage());
in.nextLine();
}
}
private static void listUsers(ControlSystem system) {
try {
Iterator<Employee> it = system.listUsers();
System.out.println(LIST_USERS_TITLE);
while (it.hasNext()) {
Employee current = (Employee) it.next();
if (current instanceof Manager) {
Manager manager = (Manager) current;
System.out.printf(MANAGER_INFO, manager.getUsername(), manager.getNumDevelopers(), manager.getNumManagedProjects(), manager.getNumProjects() + manager.getNumManagedProjects());
} else if (current instanceof Developer) {
Developer developer = (Developer) current;
System.out.printf(DEVELOPER_INFO, developer.getUsername(), developer.getManager(), developer.getNumProjects());
}
}
} catch (EmptyUsersException e) {
System.out.println(e.getMessage());
}
}
private static void createProject(Scanner in, ControlSystem system) {
String manager = in.next();
String projectTypeName = in.next();
String id = in.nextLine().trim();
int numKeywords = in.nextInt();
String keywords = "";
for (int i = 0; i < numKeywords; i++) {
keywords += in.next() + " ";
}
keywords = keywords.trim();
in.nextLine();
try {
ProjectType projectType = system.getProjectType(projectTypeName);
switch (projectType) {
case INHOUSE:
int level = in.nextInt();
in.nextLine();
system.createProject(id, manager, keywords, level);
break;
case OUTSOURCED:
String company = in.next();
system.createProject(id, manager, keywords, company);
break;
}
System.out.printf(CREATED_PROJECT, id);
} catch (ManagerDoesNotExistException | ProjectAlreadyExistsException | UnderClearanceLevelException e1) {
System.out.println(e1.getMessage());
} catch (UnknownProjectTypeException e2) {
in.nextLine();
System.out.println(e2.getMessage());
}
}
private static void listProjects(ControlSystem system) {
try {
Iterator<Project> it = system.listProjects();
System.out.println(LIST_PROJECTS_TITLE);
while (it.hasNext()) {
Project current = (Project) it.next();
if (current instanceof InHouse) {
InHouse inHouse = (InHouse) current;
System.out.printf(INHOUSE_INFO, inHouse.getId(), inHouse.getManagerUsername(), inHouse.getLevel(), inHouse.getNumEmployees(), inHouse.getNumArtefacts(), inHouse.getNumRevisions());
} else if (current instanceof Outsourced) {
Outsourced outsourced = (Outsourced) current;
System.out.printf(OUTSOURCED_INFO, outsourced.getId(), outsourced.getManagerUsername(), outsourced.getCompany());
}
}
} catch (EmptyProjectsException e) {
System.out.println(e.getMessage());
}
}
private static void addTeamToProject(Scanner in, ControlSystem system) {
boolean flag = true;
String manager = in.next();
String id = in.nextLine().trim();
int amount = in.nextInt();
List<String> members = new LinkedList<>();
for (int i = 0; i < amount; i++) {
members.add(in.next());
}
for (String member : members) {
try {
system.addEmployeeToProject(manager, id, member);
if (flag) {
System.out.println(TEAM_TITLE);
flag = false;
}
System.out.printf(ADDED_EMPLOYEE_TO_TEAM, member);
} catch (MemberAlreadyInTeamException | EmployeeDoesNotExistException | InsufficientClearanceLevelException e1) {
if (flag) {
System.out.println(TEAM_TITLE);
flag = false;
}
System.out.println(e1.getMessage());
} catch (ProjectNotManagedByUserException | ProjectDoesNotExistException | ManagerDoesNotExistException e2) {
System.out.println(e2.getMessage());
break;
}
}
}
private static void addArtefactsToProject(Scanner in, ControlSystem system) {
boolean flag = true;
String member = in.next();
String id = in.nextLine().trim();
String dateString = in.nextLine().trim();
int amount = in.nextInt();
in.nextLine();
List<Artefact> artefacts = new LinkedList<>();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(WRITE_DATE_FORMAT);
LocalDate date = LocalDate.parse(dateString, formatter);
for (int i = 0; i < amount; i++) {
String name = in.next();
int level = in.nextInt();
String description = in.nextLine().trim();
artefacts.add(new ArtefactClass(member, name, level, description, date, id));
}
for (Artefact artefact : artefacts) {
try {
system.addArtefactToProject(artefact, id, date);
if (flag) {
System.out.println(ARTEFACTS_TITLE);
flag = false;
}
System.out.printf(ADDED_ARTEFACT_TO_PROJECT, artefact.getName());
} catch (ArtefactAlreadyInProjectException | ArtefactHasHigherLevelException e1) {
if (flag) {
System.out.println(ARTEFACTS_TITLE);
flag = false;
}
System.out.println(e1.getMessage());;
} catch (UserDoesNotExistException | ProjectDoesNotExistException | MemberNotInTeamException | EmployeeDoesNotExistException e2) {
System.out.println(e2.getMessage());
break;
}
}
}
private static void projectInfo(Scanner in, ControlSystem system) {
String id = in.nextLine().trim();
try {
InHouse inHouse = system.getProjectInfo(id);
System.out.printf(PROJECT_FORMAT, inHouse.getId(), inHouse.getLevel(), inHouse.getManagerUsername(), inHouse.getManager().getLevel());
Iterator<Employee> itTeam = system.listTeamInProject(id);
while (itTeam.hasNext()) {
Employee current = (Employee) itTeam.next();
System.out.printf(EMPLOYEE_INFO, current.getUsername(), current.getLevel());
}
Iterator<Artefact> itArtefact = system.listArtefactsInProject(id);
while (itArtefact.hasNext()) {
Artefact artefact = (Artefact) itArtefact.next();
System.out.printf(ARTEFACT_INFO, artefact.getName(), artefact.getLevel());
Iterator<Revision> itRevision = system.listRevisions(artefact);
while (itRevision.hasNext()) {
Revision revision = (Revision) itRevision.next();
System.out.printf(REVISION_INFO, revision.getNumber(), revision.getOwner(), revision.getDateAsString(), revision.getComment());
}
}
} catch (ProjectDoesNotExistException | OutsourcedProjectException e) {
System.out.println(e.getMessage());;
}
}
private static void addRevisionToArtefact(Scanner in, ControlSystem system) {
String username = in.next();
String id = in.nextLine().trim();
String artefact = in.next();
String dateAsString = in.next().trim();
String comment = in.nextLine().trim();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(WRITE_DATE_FORMAT);
LocalDate date = LocalDate.parse(dateAsString, formatter);
try {
int revisionNum = system.addRevision(username, id, artefact, date, comment);
System.out.printf(REVISION_ADDED, revisionNum, artefact);
} catch (UserDoesNotExistException | EmployeeDoesNotExistException| ProjectDoesNotExistException | ArtefactDoesNotExistException | MemberNotInTeamException e) {
System.out.println(e.getMessage());
}
}
private static void managesInfo(Scanner in, ControlSystem system) {
String username = in.nextLine().trim();
try {
Iterator<Developer> itDev = system.listManagedDevelopers(username);
System.out.printf(MANAGER_TITLE, username);
while (itDev.hasNext()) {
Developer current = (Developer) itDev.next();
System.out.println(current.getUsername());
Iterator<Revision> itRev = current.listRevisions();
while (itRev.hasNext()) {
Revision r = (Revision) itRev.next();
System.out.printf(REVISION_EMPLOYEE_INFO, r.getProject(), r.getArtefact(), r.getNumber(), r.getDateAsString(), r.getComment());
}
}
} catch (ManagerDoesNotExistException e) {
System.out.println(e.getMessage());
}
}
private static void listProjectsByKeyword(Scanner in, ControlSystem system) {
String keyword = in.nextLine().trim();
try {
Iterator<InHouse> itInHouse = system.listInHousesByKeyword(keyword);
System.out.printf(KEYWORD_TITLE, keyword);
while (itInHouse.hasNext()) {
InHouse current = (InHouse) itInHouse.next();
System.out.printf(KEYWORD_INHOUSE, current.getId(), current.getManagerUsername(), current.getLevel(), current.getNumEmployees(), current.getNumArtefacts(), current.getNumRevisions(), current.getRecentDateRevisionAsString());
}
Iterator<Outsourced> itOutsourced = system.listOutsourcedByKeyword(keyword);
while (itOutsourced.hasNext()) {
Outsourced current = (Outsourced) itOutsourced.next();
System.out.printf(KEYWORD_OUTSOURCED, current.getId(), current.getManagerUsername(), current.getCompany());
}
} catch (NoProjectsWithKeywordException e) {
System.out.println(e.getMessage());
}
}
private static void listByConfidentiality(Scanner in, ControlSystem system) {
int lower = in.nextInt();
int upper = in.nextInt();
in.nextLine();
if (lower > upper) {
int aux = lower;
lower = upper;
upper = aux;
}
try {
Iterator<InHouse> it = system.listByConfidentiality(lower, upper);
System.out.printf(CONFIDENTIALITY_TITLE, lower, upper);
while (it.hasNext()) {
InHouse current = (InHouse) it.next();
System.out.printf(CONFIDENTIALITY_INFO, current.getId(), current.getManagerUsername(), current.getKeywordsFormatted());
}
} catch (NoProjectsWithinLevelsException e) {
System.out.println(e.getMessage());
}
}
private static void listWokaholics(ControlSystem system) {
try {
Iterator<Employee> it = system.listWorkaholics();
while (it.hasNext()) {
Employee current = (Employee) it.next();
if (current instanceof Manager)
System.out.printf(WORKAHOLICS_INFO, current.getUsername(), current.getUpdates(), current.getNumProjects() + ((Manager) current).getNumManagedProjects(), current.getLastDateFormatted());
else
System.out.printf(WORKAHOLICS_INFO, current.getUsername(), current.getUpdates(), current.getNumProjects(), current.getLastDateFormatted());
}
} catch (NoWorkaholicsException e) {
System.out.println(e.getMessage());
}
}
private static void listCommon(ControlSystem system) {
try {
Iterator<Employee> it = system.listCommon();
while (it.hasNext()) {
Employee first = (Employee) it.next();
Employee second = (Employee) it.next();
System.out.printf(COMMON_INFO, first.getUsername(), second.getUsername(), first.getCommonProjectsNum(second));
}
} catch (NoCommonProjectsException e) {
System.out.println(e.getMessage());
}
}
}