-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
568 lines (470 loc) · 19.8 KB
/
Controller.java
File metadata and controls
568 lines (470 loc) · 19.8 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
import User.*;
import Activity.*;
import TrainingPlan.TrainingPlan;
import Simulation.Tempo;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
import java.time.Duration;
import java.time.LocalDateTime;
import Status.Status;
public class Controller {
List <User> model;
private User currentUser;
private Tempo tempo;
private Status status;
public Controller(){
this.currentUser = null;
this.model = new ArrayList<User>();
this.tempo = new Tempo();
this.status = new Status();
}
// Check if the username is available for registration
public boolean isUsernameAvailable(String username) {
load();
for (User u : this.model) {
if (u.getUsername().equals(username)) {
return false;
}
}
return true;
}
public boolean isEmailAvailable(String email) {
load();
for (User u : this.model) {
if (u.getEmail().equals(email)) {
return false;
}
}
return true;
}
public void SignUp(String username, String password, String email, double height, double weight, double heartRate, String address, int age, int userType){
load();
User user = null;
switch (userType) {
case 1:
user = new PraticanteOcasional(username, password, email, height, weight, heartRate, address, age);
break;
case 2:
user = new Profissional(username, password, email, height, weight, heartRate, address, age);
break;
case 3:
user = new Amador(username, password, email, height, weight, heartRate, address, age);
break;
default:
break;
}
for (User u : this.model) {
if (u.getUsername().equals(user.getUsername())) {
System.out.println("User already exists");
return;
} else if (u.getEmail().equals(user.getEmail())) {
System.out.println("Email already exists");
return;
}
}
this.model.add(user);
save();
}
public boolean SignIn(String username, String password){
load();
for (User u : this.model) {
if (u.getUsername().equals(username)) {
if (u.getPassword().equals(password)) {
this.currentUser = u; // Set the currently logged-in user
return true;
}
}
}
return false;
}
public void SignOut(){ // atualizar o pointer para o user vazio
this.currentUser = null;
}
public String userLoginName(){
if (this.currentUser != null) {
return this.currentUser.getUsername();
} else {
return "No user logged in";
}
}
/* public void removeUser(String username){
for (User u : this.model) {
if (u.getUsername().equals(username)) {
this.model.remove(u);
return;
}
}
System.out.println("User not found");
} */
public Tempo getTempo(){
return tempo;
}
// Return um clone the list of users
public List<User> getUsers(){
load();
List <User> users = new ArrayList<>();
for (User user : this.model) {
users.add(user);
}
return users;
}
public void addActivity(Activity activity) {
this.save();
this.currentUser.addActivity(activity);
this.save();
}
public List<Activity> getActivitiesCurrentUser() {
load();
return this.currentUser.getArrayActivities();
}
public void addTrainingPlan(TrainingPlan trainingPlan) {
this.save();
this.currentUser.addTrainingPlan(trainingPlan);
this.save();
}
public List<TrainingPlan> getTrainingPlansCurrentUser() {
load();
return this.currentUser.getTrainingPlans();
}
public void printActivities() {
load();
for (Activity a : getActivitiesCurrentUser()) {
System.out.println(" {\n" + a + "\nStatus: " + (a.isInProgress(this.tempo.getCurrentTime()) ? "In progress" : a.isDone(this.tempo.getCurrentTime()) ? "Done" : "Not started") + "\n},");
}
}
public void save() {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("users.ser"))) {
out.writeObject(model);
} catch (IOException e) {
e.printStackTrace();
}
}
public void saveTempo() {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("simulation.ser"))) {
out.writeObject(tempo);
} catch (IOException e) {
e.printStackTrace();
}
}
public void load() {
File fileUsers = new File("users.ser");
if (fileUsers.exists()) {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileUsers))) {
model = (ArrayList<User>) in.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
} else {
model = new ArrayList<>();
}
}
public void loadTempo() {
File fileTime = new File("simulation.ser");
if (fileTime.exists()) {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileTime))) {
tempo = (Tempo) in.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
} else {
tempo = new Tempo();
}
}
public void DeleteSaveData() {
try {
File file = new File("users.ser");
if (file.delete()) {
System.out.println("Users file deleted successfully");
} else {
System.out.println("Failed to delete the users file");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteTempo() {
try {
File file = new File("simulation.ser");
if (file.delete()) {
System.out.println("Simulation file deleted successfully");
} else {
System.out.println("Failed to delete the simulation file");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void createDistanceActivity(String name, int duration, boolean isHard, double distance, int ActivityType_1, LocalDateTime date, String trainingPlan) {
Activity activity = null;
switch (ActivityType_1) {
case 1:
activity = new Corrida_atletismo(name, duration, distance, currentUser.getWeight(), currentUser.getHeartRate(), currentUser.getHeight(), currentUser.getAge(), UserCalorieFactor(currentUser), date);
int calorias_1 = activity.CalculateCalories();
activity.setCalories(calorias_1);
break;
case 2:
activity = new Remo(name, duration, distance, currentUser.getWeight(), currentUser.getHeartRate(), currentUser.getHeight(), currentUser.getAge(), UserCalorieFactor(currentUser), date);
int calorias_2 = activity.CalculateCalories();
activity.setCalories(calorias_2);
break;
case 3:
activity = new Patinagem(name, duration, distance, currentUser.getWeight(), currentUser.getHeartRate(), currentUser.getHeight(), currentUser.getAge(), UserCalorieFactor(currentUser), date);
int calorias_3 = activity.CalculateCalories();
activity.setCalories(calorias_3);
break;
default:
break;
}
if (activity != null) {
if (trainingPlan != null) {
TrainingPlan plan = getTrainingPlanByName(trainingPlan);
if (plan == null) {
createTrainingPlan(trainingPlan);
plan = getTrainingPlanByName(trainingPlan);
}
plan.addActivityToPlan(activity);
this.addTrainingPlan(plan);
} else {
this.addActivity(activity);
}
}
}
public void createAltimetryActivity(String name, int duration, boolean isHard, double distance, double altimetry, int ActivityType_2, LocalDateTime date, String trainingPlan) {
Activity activity = null;
switch (ActivityType_2) {
case 1:
activity = new Bicicleta_estrada(name, duration, distance, altimetry, currentUser.getWeight(), currentUser.getHeartRate(), currentUser.getHeight(), currentUser.getAge(), UserCalorieFactor(currentUser), date);
int calorias_4 = activity.CalculateCalories();
activity.setCalories(calorias_4);
break;
case 2:
activity = new Bicicleta_montanha(name, duration, distance, altimetry, currentUser.getWeight(), currentUser.getHeartRate(), currentUser.getHeight(), currentUser.getAge(), UserCalorieFactor(currentUser), date);
int calorias_5 = activity.CalculateCalories();
activity.setCalories(calorias_5);
break;
case 3:
activity = new Corrida_estrada(name, duration, distance, altimetry, currentUser.getWeight(), currentUser.getHeartRate(), currentUser.getHeight(), currentUser.getAge(), UserCalorieFactor(currentUser), date);
int calorias_6 = activity.CalculateCalories();
activity.setCalories(calorias_6);
break;
case 4:
activity = new Trail_monte(name, duration, distance, altimetry, currentUser.getWeight(), currentUser.getHeartRate(), currentUser.getHeight(), currentUser.getAge(), UserCalorieFactor(currentUser), date);
int calorias_7 = activity.CalculateCalories();
activity.setCalories(calorias_7);
break;
default:
break;
}
if (activity != null) {
if (trainingPlan != null) {
TrainingPlan plan = getTrainingPlanByName(trainingPlan);
if (plan == null) {
createTrainingPlan(trainingPlan);
plan = getTrainingPlanByName(trainingPlan);
}
plan.addActivityToPlan(activity);
this.addTrainingPlan(plan);
} else {
this.addActivity(activity);
}
}
}
public void createRepetitionActivity(String name, int duration, boolean isHard, int repetitions, int sets, int ActivityType_3, LocalDateTime date, String trainingPlan) {
Activity activity = null;
switch (ActivityType_3) {
case 1:
activity = new Abdominais(name, duration, repetitions, sets, currentUser.getWeight(), currentUser.getHeartRate(), currentUser.getHeight(), currentUser.getAge(), UserCalorieFactor(currentUser), date);
int calorias_8 = activity.CalculateCalories();
activity.setCalories(calorias_8);
break;
case 2:
activity = new Flexoes(name, duration, repetitions, sets, currentUser.getWeight(), currentUser.getHeartRate(), currentUser.getHeight(), currentUser.getAge(), UserCalorieFactor(currentUser), date);
int calorias_9 = activity.CalculateCalories();
activity.setCalories(calorias_9);
break;
case 3:
activity = new Alongamentos(name, duration, repetitions, sets, currentUser.getWeight(), currentUser.getHeartRate(), currentUser.getHeight(), currentUser.getAge(), UserCalorieFactor(currentUser), date);
int calorias_10 = activity.CalculateCalories();
activity.setCalories(calorias_10);
break;
case 4:
activity = new Muscle_up(name, duration, repetitions, sets, currentUser.getWeight(), currentUser.getHeartRate(), currentUser.getHeight(), currentUser.getAge(), UserCalorieFactor(currentUser), date);
int calorias_11 = activity.CalculateCalories();
activity.setCalories(calorias_11);
break;
default:
break;
}
if (activity != null) {
if (trainingPlan != null) {
TrainingPlan plan = getTrainingPlanByName(trainingPlan);
if (plan == null) {
createTrainingPlan(trainingPlan);
plan = getTrainingPlanByName(trainingPlan);
}
plan.addActivityToPlan(activity);
this.addTrainingPlan(plan);
} else {
this.addActivity(activity);
}
}
}
public void createWeightActivity( String name, int duration, boolean isHard, int repetition,int sets, double weight, int ActivityType_4, LocalDateTime date, String trainingPlan) {
Activity activity = null;
switch (ActivityType_4) {
case 1:
activity = new Levantamento_pesos(name, duration, repetition, sets, weight, currentUser.getWeight(), currentUser.getHeartRate(), currentUser.getHeight(), currentUser.getAge(), UserCalorieFactor(currentUser), date);
int calorias_12 = activity.CalculateCalories();
activity.setCalories(calorias_12);
break;
case 2:
activity = new Extensao_pernas(name, duration, repetition, sets, weight, currentUser.getWeight(), currentUser.getHeartRate(), currentUser.getHeight(), currentUser.getAge(), UserCalorieFactor(currentUser), date);
int calorias_13 = activity.CalculateCalories();
activity.setCalories(calorias_13);
break;
default:
break;
}
if (activity != null) {
if (trainingPlan != null) {
TrainingPlan plan = getTrainingPlanByName(trainingPlan);
if (plan == null) {
createTrainingPlan(trainingPlan);
plan = getTrainingPlanByName(trainingPlan);
}
plan.addActivityToPlan(activity);
this.addTrainingPlan(plan);
} else {
this.addActivity(activity);
}
}
}
public void printTrainingPlans() {
for (TrainingPlan plan : this.currentUser.getTrainingPlans()) {
System.out.println("Plano de Treino: " + plan.getName() + "\nNº de atividades: " + plan.getPlan().size());
for (Activity a : plan.getPlan()) {
System.out.println(a.getName() + " - " + a.getCalories() + " calorias - " + a.getDuration() + " minutos");
}
System.out.println();
}
}
public void createTrainingPlan(String name) {
TrainingPlan trainingPlan = new TrainingPlan();
trainingPlan.setName(name);
trainingPlan.setPlan(new ArrayList<Activity>());
this.currentUser.addTrainingPlan(trainingPlan);
}
public TrainingPlan getTrainingPlanByName(String name) {
for (TrainingPlan trainingPlan : this.currentUser.getTrainingPlans()) {
if (trainingPlan.getName().equals(name)) {
return new TrainingPlan(trainingPlan.getName(), trainingPlan.getPlan());
}
}
return null;
}
public double UserCalorieFactor(User currentUser) {
if(currentUser instanceof PraticanteOcasional){
return ((PraticanteOcasional) currentUser).getCalorieFactor();
}
if(currentUser instanceof Amador){
return ((Amador) currentUser).getCalorieFactor();
}
return ((Profissional) currentUser).getCalorieFactor();
}
public void avancarXMinutos(int minutos) {
this.tempo.addTime(minutos);
}
public void voltarXMinutos(int minutos) {
this.tempo.decreaseTime(minutos);
}
public void avancarXHoras(int horas) {
this.tempo.addTime(horas, 0);
}
public void voltarXHoras(int horas) {
this.tempo.decreaseTime(horas, 0);
}
public void avancarXDias(int dias) {
this.tempo.addTime(dias, 0, 0);
}
public void voltarXDias(int dias) {
this.tempo.decreaseTime(dias, 0, 0);
}
public void resetTime() {
deleteTempo();
this.tempo = new Tempo();
saveTempo();
}
////////////////////////////// Status ////////////////////////////////////
public String MostPerformedActivity() {
load();
return this.status.mostPerformedActivity(getUsers(), this.tempo.getCurrentTime());
}
public String KmsUser() {
return this.status.totalKms(getActivitiesCurrentUser(), this.tempo.getCurrentTime());
}
public String KmsUserInterval(LocalDateTime tmp1, LocalDateTime tmp2){
return this.status.totalKmsInterval(getActivitiesCurrentUser(), this.tempo.getCurrentTime(), tmp1, tmp2);
}
public String AltimetryUser() {
return this.status.totalAltimetry(getActivitiesCurrentUser(), this.tempo.getCurrentTime());
}
public String AltimetryUserInterval(LocalDateTime tmp3, LocalDateTime tmp4) {
double totalAltimetry = 0;
for (Activity a : getActivitiesCurrentUser()) {
LocalDateTime tempo_1 = a.getDate().plus(Duration.ofMinutes(a.getDuration()));
if (a.isDone(this.tempo.getCurrentTime()) && (a instanceof Altimetry) && tempo_1.isBefore(tmp4) && a.getDate().isAfter(tmp3)){
totalAltimetry += this.status.UserAltimetry(a);
}
}
return "Total de altimetria percorrida: " + totalAltimetry;
}
///////////////////////////////// AltimetryInterval /////////
///////////////////////////////// Plano com maior gasto calórico /////////
public String userMostCaloriesOfTrainingPlan() {
TrainingPlan maxCaloriePlan = null;
int maxCalories = 0;
for (User u : getUsers()) {
for (TrainingPlan plan : u.getTrainingPlans()) {
int totalCalories = 0;
for (Activity a : plan.getPlan()) {
totalCalories += a.getCalories();
}
if (totalCalories > maxCalories) {
maxCalories = totalCalories;
maxCaloriePlan = plan;
}
}
}
// Build the result string
StringBuilder result = new StringBuilder();
if (maxCaloriePlan != null) {
result.append("Plano com maior gasto calórico: ")
.append(maxCaloriePlan.getName())
.append(" com ")
.append(maxCalories)
.append(" calorias");
} else {
result.append("Nenhum plano encontrado.");
}
return result.toString();
}
///////////////////////////////// Plano com maior gasto calórico /////////
//////////////////////////// Usuário que mais atividades realizou ////////
public String userMostActivity() {
load();
return this.status.mostActivity(getUsers(),this.tempo.getCurrentTime());
}
public String userMostActivityInterval(LocalDateTime tmp5, LocalDateTime tmp6) {
load();
return this.status.mostActivityInterval(getUsers(), this.tempo.getCurrentTime(), tmp5, tmp6);
}
public String userMostCalories() {
load();
return this.status.mostCalories(getUsers(),this.tempo.getCurrentTime());
}
public String userMostCaloriesInterval(LocalDateTime tmp7, LocalDateTime tmp8) {
load();
return this.status.mostActivityInterval(getUsers(),this.tempo.getCurrentTime(), tmp7,tmp8);
}
}