-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.java
More file actions
661 lines (584 loc) · 25.2 KB
/
View.java
File metadata and controls
661 lines (584 loc) · 25.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
import java.io.IOException;
import User.User;
import lib.Colors;
import lib.Logger;
import lib.Read;
import static java.lang.System.out;
import java.time.LocalDateTime;
import java.util.List;
public class View {
private Controller controller;
private boolean loggedIn;
private Read read;
public View(Controller controller) {
this.controller = controller;
this.loggedIn = false;
this.read = new Read();
}
public void run() throws IOException, ClassNotFoundException, InterruptedException {
this.controller.loadTempo();
while (true) {
this.controller.getTempo().printCurrentTime();
if (loggedIn && this.controller.userLoginName() != null) {
Logger.log("Usuário: " + this.controller.userLoginName(), Colors.GREEN);
}
this.controller.userLoginName();
this.showMenu();
int option = this.read.readInt("Opção: ");
switch (option) {
case 1:
this.cls();
if (!loggedIn) {
this.signUp();
} else {
Logger.error("Opção inválida!");
}
break;
case 2:
this.cls();
if (!loggedIn) {
this.signIn();
} else {
Logger.error("Opção inválida!");
}
break;
case 3:
if (loggedIn) {
this.cls();
this.logout();
} else {
this.cls();
out.println("Opção inválida!");
}
break;
case 4:
this.cls();
controller.DeleteSaveData();
controller.resetTime();
break;
case 5:
this.cls();
this.readTime();
break;
case 6:
if (loggedIn) {
this.cls();
readActivity(null);
} else {
this.cls();
out.println("Opção inválida!");
}
break;
case 7:
if (loggedIn) {
this.cls();
readTrainingPlan();
} else {
this.cls();
out.println("Opção inválida!");
}
break;
case 8:
if (loggedIn) {
this.cls();
this.readProgramStatus();
} else {
this.cls();
out.println("Opção inválida!");
}
break;
case 0:
this.cls();
this.controller.saveTempo();
out.println("Saindo...");
return;
default:
this.cls();
out.println("Opção inválida!");
}
}
}
private void cls() {
out.print("\033[H\033[2J");
out.flush();
}
/////////////////////////// Sign In/Sign Up ///////////////////////////
private void signUp() throws IOException {
String name = this.read.readString("Definir nome: ");
while (!this.controller.isUsernameAvailable(name)) {
Logger.error("Nome de utilizador já existe!");
name = this.read.readString("Definir nome: ");
}
String password = this.read.readPassword("Definir senha: ");
while (password.length() < 5) {
Logger.error("Senha inválida! Ela deve ter pelo menos 5 caracteres!");
password = this.read.readPassword("Definir password: ");
}
String email = this.read.readString("Definir email: ");
while (!email.contains("@") || !email.contains(".") || email.length() < 5
|| !this.controller.isEmailAvailable(email)) {
Logger.error("Email inválido!");
email = this.read.readString("Definir email: ");
}
double height = this.read.readDouble("Definir altura (ex: 1.90): ");
while (height < 0 || height > 3) {
Logger.error("Altura inválida!");
height = this.read.readDouble("Definir altura (ex: 1.90): ");
}
double weight = this.read.readDouble("Definir peso (ex: 77.5): ");
while (weight < 0 || weight > 500) {
Logger.error("Peso inválido!");
weight = this.read.readDouble("Definir peso (ex: 77.5): ");
}
double heartRate = this.read.readDouble("Definir ritmo cardíaco (0 - 200): ");
while (heartRate < 0 || heartRate > 200) {
Logger.error("Ritmo cardíaco inválido!");
heartRate = this.read.readDouble("Definir ritmo cardíaco (0 - 200): ");
}
String address = this.read.readString("Definir morada: ");
int age = this.read.readInt("Definir idade: ");
while (age < 0 || age > 120) {
Logger.error("Idade inválida!");
age = this.read.readInt("Definir idade: ");
}
Logger.log("Escolha o tipo de utilizador:", Colors.WHITE);
Logger.log("1 - Praticante Ocasional", Colors.BLUE);
Logger.log("2 - Profissional", Colors.CYAN);
Logger.log("3 - Amador", Colors.PURPLE);
int userType = this.read.readInt("Opção: ");
while (userType < 1 || userType > 3) {
Logger.error("Opção inválida!");
userType = this.read.readInt("Opção: ");
}
controller.SignUp(name, password, email, height, weight, heartRate, address, age, userType);
}
private void signIn() throws IOException, InterruptedException, ClassNotFoundException {
String loginName = this.read.readString("Nome: ");
String loginPassword = this.read.readPassword("Password: ");
loggedIn = controller.SignIn(loginName, loginPassword);
if (loggedIn) {
Logger.log("Login successful", Colors.GREEN);
} else {
Logger.error("Login failed!");
}
}
//////////////////// Sign In/Sign Up ///////////////////////////
/////////////////////////// Menus ///////////////////////////
private void logout() {
loggedIn = false;
controller.SignOut();
Logger.log("Logout successful", Colors.GREEN);
}
private void showMenu() {
out.println("Menu:");
if (!loggedIn) {
Logger.log("0 - Sair", Colors.RED);
Logger.log("1 - Sign up", Colors.BLUE);
Logger.log("2 - Sign in", Colors.BLUE);
}
if (loggedIn) {
showUserMenu();
}
}
private void showUserMenu() {
Logger.log("3 - Logout", Colors.RED);
Logger.log("4 - Apagar dados", Colors.YELLOW);
Logger.log("5 - Tempo", Colors.PURPLE);
Logger.log("6 - Adicionar atividade", Colors.GREEN);
Logger.log("7 - Criar plano de treino", Colors.GREEN);
Logger.log("8 - Program status", Colors.PURPLE);
}
/////////////////////////// Menus ///////////////////////////
/////////////////////////// Atividades ///////////////////////////
private void readActivity(String trainingPlan) {
this.cls();
Logger.log("Escolha o tipo de atividade:", Colors.WHITE);
Logger.log("1 - Distância", Colors.BLUE);
Logger.log("2 - Altimetria", Colors.CYAN);
Logger.log("3 - Repetição", Colors.PURPLE);
Logger.log("4 - Peso", Colors.YELLOW);
Logger.log("5 - Voltar ao menu", Colors.RED);
int activityType = this.read.readInt("Opção: ");
while (activityType < 1 || activityType > 5) {
Logger.error("Opção inválida!");
activityType = this.read.readInt("Opção: ");
}
switch (activityType) {
case 1:
readDistanceActivity(trainingPlan);
break;
case 2:
readAltimetryActivity(trainingPlan);
break;
case 3:
readRepetitionActivity(trainingPlan);
break;
case 4:
readWeightActivity(trainingPlan);
break;
default:
out.println("Voltando ao menu...");
break;
}
}
private void readDistanceActivity(String trainingPlan) {
this.cls();
this.controller.getTempo().printCurrentTime();
Logger.log("Escolha a atividade de distância:", Colors.WHITE);
Logger.log("1 - Corrida de atletismo", Colors.BLUE);
Logger.log("2 - Remo", Colors.CYAN);
Logger.log("3 - Patinagem", Colors.PURPLE);
int ActivityType_1 = this.read.readInt("Opção: ");
while (ActivityType_1 < 1 || ActivityType_1 > 3) {
Logger.error("Opção inválida!");
ActivityType_1 = this.read.readInt("Opção: ");
}
String name;
switch (ActivityType_1) {
case 1:
name = "Corrida de atletismo";
break;
case 2:
name = "Remo";
break;
case 3:
name = "Patinagem";
break;
default:
name = "";
}
int duration = this.read.readInt("Definir duração (minutos): ");
while (duration < 0) {
Logger.error("Duração inválida!");
duration = this.read.readInt("Definir duração (minutos): ");
}
double distance = this.read.readDouble("Definir distância (metros): ");
while (distance < 0) {
Logger.error("Distância inválida!");
distance = this.read.readDouble("Definir distância (metros): ");
}
int choice2 = this.read.readInt("Começar agora? 1 - Sim, 0 - Não: ");
while (choice2 < 0 || choice2 > 1) {
Logger.error("Opção inválida!");
choice2 = this.read.readInt("Começar agora? 1 - Sim, 0 - Não: ");
}
if (choice2 == 1) this.controller.createDistanceActivity(name, duration, true, distance, ActivityType_1, this.controller.getTempo().getCurrentTime(), trainingPlan);
else {
LocalDateTime date = this.read.readLocalDateTime("Definir data (yyyy-MM-dd HH:mm): ");
this.controller.createDistanceActivity(name, duration, true, distance, ActivityType_1, date, trainingPlan);
}
}
private void readAltimetryActivity(String trainingPlan) {
this.cls();
Logger.log("Escolha a atividade de altimetria:", Colors.WHITE);
Logger.log("1 - Bicicleta de estrada", Colors.BLUE);
Logger.log("2 - Bicicleta de montanha", Colors.CYAN);
Logger.log("3 - Corrida na estrada", Colors.PURPLE);
Logger.log("4 - Trail no monte", Colors.YELLOW);
int ActivityType_2 = this.read.readInt("Opção: ");
while (ActivityType_2 < 1 || ActivityType_2 > 4) {
Logger.error("Opção inválida!");
ActivityType_2 = this.read.readInt("Opção: ");
}
String name;
switch (ActivityType_2) {
case 1:
name = "Bicicleta de estrada";
break;
case 2:
name = "Bicicleta de montanha";
break;
case 3:
name = "Corrida na estrada";
break;
case 4:
name = "Trail no monte";
break;
default:
name = "";
}
int duration = this.read.readInt("Definir duração (minutos): ");
while (duration < 0) {
Logger.error("Duração inválida!");
duration = this.read.readInt("Definir duração (minutos): ");
}
double distance = this.read.readDouble("Definir distância (metros): ");
while (distance < 0) {
Logger.error("Distância inválida!");
distance = this.read.readDouble("Definir distância (metros): ");
}
double altimetry = this.read.readDouble("Definir altimetria (metros): ");
while (altimetry < 0) {
Logger.error("Altimetria inválida!");
altimetry = this.read.readDouble("Definir altimetria (metros): ");
}
int choice2 = this.read.readInt("Começar agora? 1 - Sim, 0 - Não: ");
while (choice2 < 0 || choice2 > 1) {
Logger.error("Opção inválida!");
choice2 = this.read.readInt("Começar agora? 1 - Sim, 0 - Não: ");
}
// boolean ishard = this.read.readBoolean("A atividade é hard: ");
if (choice2 == 1)
this.controller.createAltimetryActivity(name, duration, true, distance, altimetry, ActivityType_2,
this.controller.getTempo().getCurrentTime(), trainingPlan);
else {
LocalDateTime date = this.read.readLocalDateTime("Definir data (yyyy-MM-dd HH:mm): ");
this.controller.createAltimetryActivity(name, duration, true, distance, altimetry, ActivityType_2, date, trainingPlan);
}
}
private void readRepetitionActivity(String trainingPlan) {
this.cls();
Logger.log("Escolha a atividade de repetição:", Colors.WHITE);
Logger.log("1 - Abdominais", Colors.BLUE);
Logger.log("2 - Flexões", Colors.CYAN);
Logger.log("3 - Alongamentos", Colors.PURPLE);
Logger.log("4 - Muscle-ups", Colors.YELLOW);
int ActivityType_3 = this.read.readInt("Opção: ");
while (ActivityType_3 < 1 || ActivityType_3 > 4) {
Logger.error("Opção inválida!");
ActivityType_3 = this.read.readInt("Opção: ");
}
String name;
switch (ActivityType_3) {
case 1:
name = "Abdominais";
break;
case 2:
name = "Flexões";
break;
case 3:
name = "Alongamentos";
break;
case 4:
name = "Muscle-ups";
break;
default:
name = "";
}
int duration = this.read.readInt("Definir duração (minutos): ");
while (duration < 0) {
Logger.error("Duração inválida!");
duration = this.read.readInt("Definir duração (minutos): ");
}
int repetitions = this.read.readInt("Definir repetições: ");
while (repetitions < 0) {
Logger.error("Repetições inválidas!");
repetitions = this.read.readInt("Definir repetições: ");
}
int sets = this.read.readInt("Definir séries: ");
while (sets < 0) {
Logger.error("Séries inválidas!");
sets = this.read.readInt("Definir séries: ");
}
int choice2 = this.read.readInt("Começar agora? 1 - Sim, 0 - Não: ");
while (choice2 < 0 || choice2 > 1) {
Logger.error("Opção inválida!");
choice2 = this.read.readInt("Começar agora? 1 - Sim, 0 - Não: ");
}
// boolean ishard = this.read.readBoolean("A atividade é hard: ");
if (choice2 == 1) this.controller.createRepetitionActivity(name, duration, true, repetitions, sets, ActivityType_3, this.controller.getTempo().getCurrentTime(), trainingPlan);
else {
LocalDateTime date = this.read.readLocalDateTime("Definir data (yyyy-MM-dd HH:mm): ");
this.controller.createRepetitionActivity(name, duration, true, repetitions, sets, ActivityType_3, date, trainingPlan);
}
}
private void readWeightActivity(String trainingPlan) {
this.cls();
Logger.log("Escolha a atividade de peso:", Colors.WHITE);
Logger.log("1 - Levantamento de pesos", Colors.BLUE);
Logger.log("2 - Extensão de pernas", Colors.CYAN);
int ActivityType_4 = this.read.readInt("Opção: ");
while (ActivityType_4 < 1 || ActivityType_4 > 2) {
Logger.error("Opção inválida!");
ActivityType_4 = this.read.readInt("Opção: ");
}
String name;
switch (ActivityType_4) {
case 1:
name = "Levantamento de pesos";
break;
case 2:
name = "Extensão de pernas";
break;
default:
name = "";
}
int duration = this.read.readInt("Definir duração (minutos): ");
while (duration < 0) {
Logger.error("Duração inválida!");
duration = this.read.readInt("Definir duração (minutos): ");
}
int repetitions = this.read.readInt("Definir repetições: ");
while (repetitions < 0) {
Logger.error("Repetições inválidas!");
repetitions = this.read.readInt("Definir repetições: ");
}
int sets = this.read.readInt("Definir séries: ");
while (sets < 0) {
Logger.error("Séries inválidas!");
sets = this.read.readInt("Definir séries: ");
}
double weight = this.read.readDouble("Definir peso (Kg, ex: 12.5): ");
while (weight < 0) {
Logger.error("Peso inválido!");
weight = this.read.readDouble("Definir peso (Kg, ex: 12.5): ");
}
int choice2 = this.read.readInt("Começar agora? 1 - Sim, 0 - Não: ");
while (choice2 < 0 || choice2 > 1) {
Logger.error("Opção inválida!");
choice2 = this.read.readInt("Começar agora? 1 - Sim, 0 - Não: ");
}
if (choice2 == 1) this.controller.createWeightActivity(name, duration, true, repetitions, sets, weight, ActivityType_4, this.controller.getTempo().getCurrentTime(), trainingPlan);
else {
LocalDateTime date = this.read.readLocalDateTime("Definir data (yyyy-MM-dd HH:mm): ");
this.controller.createWeightActivity(name, duration, true, repetitions, sets, weight, ActivityType_4, date, trainingPlan);
}
}
private void readTrainingPlan() {
Logger.log("Criar plano de treino", Colors.WHITE);
String name = this.read.readString("Nome: ");
while (name.length() < 1) {
Logger.error("Nome inválido!");
name = this.read.readString("Nome: ");
}
boolean addMoreActivities = true;
while (addMoreActivities) {
readActivity(name);
String response = this.read.readString("Deseja adicionar mais atividades? (s/n): ");
addMoreActivities = response.equalsIgnoreCase("s");
}
}
/////////////////////////// Atividades ///////////////////////////
/////////////////////////// Tempo ///////////////////////////
public void readTime() {
this.controller.getTempo().printCurrentTime();
Logger.log("1- Avançar x minutos", Colors.YELLOW);
Logger.log("2- Avançar x horas", Colors.YELLOW);
Logger.log("3- Avançar x dias", Colors.YELLOW);
Logger.log("4- Voltar x minutos", Colors.CYAN);
Logger.log("5- Voltar x horas", Colors.CYAN);
Logger.log("6- Voltar x dias", Colors.CYAN);
Logger.log("7- Reiniciar tempo", Colors.RED);
Logger.log("0- Voltar para o menu", Colors.RED);
int opcao = this.read.readInt("Selecione alguma opção para mudar o tempo: ");
while (opcao < 0 || opcao > 7) {
Logger.error("Opção inválida!");
opcao = this.read.readInt("Selecione alguma opção para mudar o tempo: ");
}
switch (opcao) {
case 0:
this.cls();
return;
case 1:
int minutos = this.read.readInt("Minutos para avançar: ");
this.controller.avancarXMinutos(minutos);
break;
case 2:
int horas = this.read.readInt("Horas para avançar: ");
this.controller.avancarXHoras(horas);
break;
case 3:
int dias = this.read.readInt("Dias para avançar: ");
this.controller.avancarXDias(dias);
break;
case 4:
int minutosDiminuir = this.read.readInt("Minutos para voltar: ");
this.controller.voltarXMinutos(minutosDiminuir);
break;
case 5:
int horasDiminuir = this.read.readInt("Horas para voltar: ");
this.controller.voltarXHoras(horasDiminuir);
break;
case 6:
int diasDiminuir = this.read.readInt("Dias para voltar: ");
this.controller.voltarXDias(diasDiminuir);
break;
case 7:
this.controller.resetTime();
break;
default:
break;
}
this.cls();
}
/////////////////////////// Tempo /////////////////////////////////////
/////////////////////////// Program Status ///////////////////////////
public void readProgramStatus() {
out.print("Menu Status:\n");
Logger.log("1 - Utilizador com mais calorias gastas", Colors.YELLOW);
Logger.log("2 - Utilizador com mais calorias gastas num intervalo de tempo", Colors.YELLOW);
Logger.log("3 - Utilizador com mais atividades realizadas", Colors.CYAN);
Logger.log("4 - Utilizador com mais atividades realizadas num periodo de tempo", Colors.CYAN);
Logger.log("5 - Atividade mais realizada", Colors.PURPLE);
Logger.log("6 - Kms totalizados pelo utilizador", Colors.BLUE);
Logger.log("7 - Kms totalizados pelo utilizador num intervalo de tempo", Colors.BLUE);
Logger.log("8 - Altimetria totalizada pelo utilizador", Colors.YELLOW);
Logger.log("9 - Altimetria totalizada pelo tilizador num intervalo de tempo", Colors.YELLOW);
Logger.log("10 - Plano de treino com maior gasto calórico", Colors.PURPLE);
Logger.log("11 - Listar atividades do utilizador", Colors.RED);
Logger.log("12 - Listar planos de treino do utilizador", Colors.RED);
Logger.log("13 - Listar todos os users", Colors.RED);
int opcao_status = this.read.readInt("Selecione alguma opção: ");
while (opcao_status < 0 || opcao_status > 13) {
Logger.error("Opção inválida!");
opcao_status = this.read.readInt("Selecione alguma opção: ");
}
switch (opcao_status) {
case 1:
Logger.log(this.controller.userMostCalories(), Colors.GREEN);
break;
case 2:
LocalDateTime tmp7 = this.read.readLocalDateTime("Tempo inicial (yyyy-MM-dd HH:mm): ");
LocalDateTime tmp8 = this.read.readLocalDateTime("Tempo final (yyyy-MM-dd HH:mm): ");
Logger.log(this.controller.userMostCaloriesInterval(tmp7, tmp8), Colors.GREEN);
break;
case 3:
Logger.log(this.controller.userMostActivity(), Colors.GREEN);
break;
case 4:
LocalDateTime tmp5 = this.read.readLocalDateTime("Tempo inicial (yyyy-MM-dd HH:mm): ");
LocalDateTime tmp6 = this.read.readLocalDateTime("Tempo final (yyyy-MM-dd HH:mm): ");
Logger.log(this.controller.userMostActivityInterval(tmp5, tmp6), Colors.GREEN);
break;
case 5:
Logger.log(this.controller.MostPerformedActivity(), Colors.GREEN);
break;
case 6:
Logger.log(this.controller.KmsUser(), Colors.GREEN);
break;
case 7:
LocalDateTime tmp1 = this.read.readLocalDateTime("Tempo inicial (yyyy-MM-dd HH:mm): ");
LocalDateTime tmp2 = this.read.readLocalDateTime("Tempo final (yyyy-MM-dd HH:mm): ");
Logger.log(this.controller.KmsUserInterval(tmp1, tmp2), Colors.GREEN);
break;
case 8:
Logger.log(this.controller.AltimetryUser(), Colors.GREEN);
break;
case 9:
LocalDateTime tmp3 = this.read.readLocalDateTime("Tempo inicial (yyyy-MM-dd HH:mm): ");
LocalDateTime tmp4 = this.read.readLocalDateTime("Tempo final (yyyy-MM-dd HH:mm): ");
Logger.log(this.controller.AltimetryUserInterval(tmp3, tmp4), Colors.GREEN);
break;
case 10:
Logger.log(controller.userMostCaloriesOfTrainingPlan(), Colors.GREEN);
break;
case 11:
controller.printActivities(); //devia printar na view e nao no controler
break;
case 12:
controller.printTrainingPlans();
break;
case 13:
printUsers(controller.getUsers());
break;
default:
break;
}
}
/////////////////////////// Program Status ///////////////////////////
private void printUsers(List<User> users) {
for (User user : users) {
Logger.log(user.toString(), Colors.GREEN);
}
}
}