-
Notifications
You must be signed in to change notification settings - Fork 0
/
Patient.java
473 lines (378 loc) · 20.9 KB
/
Patient.java
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
import java.io.Console;
import java.util.Date;
public class Patient extends User {
// Patient-specific attributes
private Date dateOfBirth;
private String hasHIV;
private Date diagnosisDate;
private String isOnART;
private Date startedART;
private String countryISO;
// Constructor for Patient class, initializing the user with the Patient role
public Patient(String firstName, String lastName, String email, String password) {
super(firstName, lastName, email, password, UserRoles.PATIENT);
}
// Getter and setter for date of birth
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
// Getter and setter for HIV status
public String getHasHIV() {
return hasHIV;
}
public void setHasHIV(String hasHIV) {
this.hasHIV = hasHIV;
}
// Getter and setter for diagnosis date
public Date getDiagnosisDate() {
return diagnosisDate;
}
public void setDiagnosisDate(Date diagnosisDate) {
this.diagnosisDate = diagnosisDate;
}
// Getter and setter for ART status
public String getIsOnART() {
return isOnART;
}
public void setIsOnART(String isOnART) {
this.isOnART = isOnART;
}
// Getter and setter for ART start date
public Date getStartedART() {
return startedART;
}
public void setStartedART(Date startedART) {
this.startedART = startedART;
}
// Getter and setter for country ISO code
public String getCountryISO() {
return countryISO;
}
public void setCountryISO(String countryISO) {
this.countryISO = countryISO;
}
// Completes the registration process for a patient
public static void completeRegistration() {
Console console = System.console();
System.out.println("\n_______________________\nCompleting Registration\n_______________________");
// Prompt user for UUID and email
System.out.print("Enter UUID: ");
String uuid = Main.getUserInput();
System.out.print("Enter Your Email: ");
String email = Main.getUserInput();
// Check UUID validity and registration status
String uuidCheckResult = Main.callBashScript("user-manager.sh", "check_uuid", uuid, email);
if (uuidCheckResult.equals("1")) {
Main.clearScreen();
System.out.println(Main.RED+"\u2139 Info: Registration already done."+Main.RESET);
return;
} else if (uuidCheckResult.equals("2")) {
Main.clearScreen();
System.out.println(Main.RED+"\u2139 Info: Authentication failed."+Main.RESET);
return;
}
// Prompt user for remaining registration details
System.out.print("Enter Your firstname: ");
String firstName = Main.getUserInput();
// Validate firstname
while (!Main.isValidName(firstName)) {
System.out.println(Main.RED+"Invalid firstname format. "+Main.RESET+"\nPlease enter a valid firstname: ");
firstName = Main.getUserInput();
}
System.out.print("Enter Your lastname: ");
String lastName = Main.getUserInput();
// Validate lastName
while (!Main.isValidName(lastName)) {
System.out.println(Main.RED+"Invalid lastname format. "+Main.RESET+"\nPlease enter a valid lastname: ");
lastName = Main.getUserInput();
}
System.out.print("Enter date of birth (YYYY-MM-DD): ");
String dob = Main.getUserInput();
while (!Main.isValidDateOfBirth(dob)) {
System.out.println(Main.RED + "Invalid date or date out of valid range." + Main.RESET +
"\nPlease enter a real valid date of birth (YYYY-MM-DD): ");
dob = Main.getUserInput();
}
System.out.print("Are you HIV positive? (yes/no): ");
String hivStatus = Main.getUserInput();
while (!Main.isValidInput(hivStatus)) {
System.out.println(Main.RED + "Invalid input. Please enter 'yes' or 'no'." + Main.RESET);
System.out.print("Are you HIV positive? (yes/no): ");
hivStatus = Main.getUserInput();
}
boolean hasHIV = hivStatus.equalsIgnoreCase("yes");
String diagnosisDate = "null";
boolean isOnART = false;
String startedART = "null";
// If HIV positive, ask for diagnosis date and ART status
if (hasHIV) {
System.out.print("Enter diagnosis date (YYYY-MM-DD): ");
diagnosisDate = Main.getUserInput();
// Validate the diagnosis date
while (!Main.isValidDiagnosisDate(diagnosisDate, dob)) {
System.out.println(Main.RED + "Invalid diagnosis. It must be a valid date greater than the date of birth." + Main.RESET);
System.out.print("Enter diagnosis date (YYYY-MM-DD): ");
diagnosisDate = Main.getUserInput();
}
System.out.print("Are you on ART drugs? (yes/no): ");
String artStatus = Main.getUserInput();
while (!Main.isValidInput(artStatus)) {
System.out.println(Main.RED + "Invalid input. Please enter 'yes' or 'no'." + Main.RESET);
System.out.print("Are you on ART drugs? (yes/no): ");
artStatus = Main.getUserInput();
}
isOnART = artStatus.equalsIgnoreCase("yes");
if (isOnART) {
System.out.print("Enter the date you started ART (YYYY-MM-DD): ");
startedART = Main.getUserInput();
// Validate the date of starting ART
while (!Main.isValidDateOrder(startedART, diagnosisDate)) {
System.out.println(Main.RED + "Invalid date. It must be a valid date greater than the date of diagnosis." + Main.RESET);
System.out.print("Enter the date you started ART (YYYY-MM-DD): ");
startedART = Main.getUserInput();
}
}
}
System.out.print("Enter country ISO code: ");
String countryISO = Main.getUserInput();
while (!Main.isValidISOCode(countryISO)) {
System.out.println(Main.RED + "Invalid ISO code. It cannot be empty and must be 2 or 3 uppercase letters." + Main.RESET);
System.out.print("Enter country ISO code: ");
countryISO = Main.getUserInput();
}
// Prompt for password
System.out.print("Enter password: ");
char[] passwordArray = System.console().readPassword();
String password = new String(passwordArray);
while (!Main.isValidPassword(password)) {
System.out.println(Main.RED + "Invalid password. It cannot be empty." + Main.RESET);
System.out.print("Enter password: ");
passwordArray = System.console().readPassword();
password = new String(passwordArray);
}
// Register patient via bash script
String result = Main.callBashScript("user-manager.sh", "complete_registration", uuid, firstName, lastName, dob, hasHIV ? "yes" : "no", diagnosisDate, isOnART ? "yes" : "no", startedART, countryISO, password);
System.out.println(result);
}
// Displays the patient's profile information
public void viewProfile() {
// Retrieve patient UUID using email
String uuid = Main.callBashScript("user-manager.sh", "get_uuid", getEmail());
// Get patient information from the system
String patientInformation = Main.callBashScript("user-manager.sh", "get_patient_info", uuid);
String[] patientInformationParts = patientInformation.split(":");
// Set patient attributes from retrieved information
if (patientInformationParts[0] != null && !patientInformationParts[0].isEmpty()) {
setDateOfBirth(Main.parseDate(patientInformationParts[0]));
}
if (patientInformationParts[1] != null && !patientInformationParts[1].isEmpty()) {
setHasHIV(patientInformationParts[1]);
}
if (patientInformationParts[2] != null && !patientInformationParts[2].isEmpty()) {
setDiagnosisDate(Main.parseDate(patientInformationParts[2]));
}
if (patientInformationParts[3] != null && !patientInformationParts[3].isEmpty()) {
setIsOnART(patientInformationParts[3]);
}
if (patientInformationParts[4] != null && !patientInformationParts[4].isEmpty()) {
setStartedART(Main.parseDate(patientInformationParts[4]));
}
if (patientInformationParts[5] != null && !patientInformationParts[5].isEmpty()) {
setCountryISO(patientInformationParts[5]);
}
Main.clearScreen();
// Display patient profile information
System.out.println("\n_______________________\nMy Profile\n_______________________\n");
System.out.println("Firstname: " +Main.GREEN+ getFirstName()+Main.RESET);
System.out.println("Lastname: " +Main.GREEN+ getLastName()+Main.RESET);
System.out.println("Email: " +Main.GREEN+ getEmail()+Main.RESET);
System.out.println("Date of birth: " +Main.GREEN+ getDateOfBirth()+Main.RESET);
System.out.println("HIV status: " +Main.GREEN+ getHasHIV()+Main.RESET);
System.out.println("Diagnosis Date: " +Main.GREEN+ getDiagnosisDate()+Main.RESET);
System.out.println("ART status: " +Main.GREEN+ getIsOnART()+Main.RESET);
System.out.println("Date - ART: " +Main.GREEN+ getStartedART()+Main.RESET);
System.out.println("Country: " +Main.GREEN+ getCountryISO()+Main.RESET);
System.out.println("Survival rate: " +Main.GREEN+ this.calculateLifespan()+Main.RESET+" years");
System.out.println("_______________________");
}
// Method to update profile, not implemented yet
public void updateProfile() {
Console console = System.console();
// Retrieve patient UUID using email
String uuid = Main.callBashScript("user-manager.sh", "get_uuid", getEmail());
// Get patient information from the system
String patientInformation = Main.callBashScript("user-manager.sh", "get_patient_info", uuid);
String[] patientInformationParts = patientInformation.split(":");
System.out.println(Main.GREEN+"Current information (press Enter to keep current value):"+Main.RESET);
System.out.print("Enter your new first name: ");
String newFirstName = Main.getUserInput();
newFirstName = newFirstName.isEmpty() ? getFirstName() : newFirstName;
// Validate firstname
while (!Main.isValidName(newFirstName)) {
System.out.println(Main.RED+"Invalid firstname format. "+Main.RESET+"\nPlease enter a valid firstname: ");
newFirstName = Main.getUserInput();
newFirstName = newFirstName.isEmpty() ? getFirstName() : newFirstName;
}
System.out.print("Enter your new last name: ");
String newLastName = Main.getUserInput();
newLastName = newLastName.isEmpty() ? getLastName() : newLastName;
// Validate lastName
while (!Main.isValidName(newLastName)) {
System.out.println(Main.RED+"Invalid lastname format. "+Main.RESET+"\nPlease enter a valid lastname: ");
newLastName = Main.getUserInput();
newLastName = newLastName.isEmpty() ? getLastName() : newLastName;
}
System.out.print("Enter your new DOB (YYYY-MM-DD): ");
String newDOB = Main.getUserInput();
newDOB = newDOB.isEmpty() ? patientInformationParts[0] : newDOB;
while (!Main.isValidDateOfBirth(newDOB)) {
System.out.println(Main.RED + "Invalid date or date out of valid range." + Main.RESET +
"\nPlease enter a real valid date of birth (YYYY-MM-DD): ");
newDOB = Main.getUserInput();
newDOB = newDOB.isEmpty() ? patientInformationParts[0] : newDOB;
}
System.out.print("Are you HIV positive? (yes/no): ");
String newHIVStatus = Main.getUserInput();
newHIVStatus = newHIVStatus.isEmpty() ? patientInformationParts[1] : newHIVStatus;
while (!Main.isValidInput(newHIVStatus)) {
System.out.println(Main.RED + "Invalid input. Please enter 'yes' or 'no'." + Main.RESET);
System.out.print("Are you HIV positive? (yes/no): ");
newHIVStatus = Main.getUserInput();
newHIVStatus = newHIVStatus.isEmpty() ? patientInformationParts[1] : newHIVStatus;
}
String newDiagnosisDate = "null", newARTStatus = "no", newStartedART = "null";
if (newHIVStatus.equalsIgnoreCase("yes")) {
System.out.print("Enter your new diagnosis date (YYYY-MM-DD): ");
newDiagnosisDate = Main.getUserInput();
newDiagnosisDate = newDiagnosisDate.isEmpty() ? patientInformationParts[2] : newDiagnosisDate;
// Validate the diagnosis date
while (!Main.isValidDiagnosisDate(newDiagnosisDate, newDOB)) {
System.out.println(Main.RED + "Invalid diagnosis. It must be a valid date greater than the date of birth." + Main.RESET);
System.out.print("Enter diagnosis date (YYYY-MM-DD): ");
newDiagnosisDate = Main.getUserInput();
newDiagnosisDate = newDiagnosisDate.isEmpty() ? patientInformationParts[2] : newDiagnosisDate;
}
System.out.print("Are you on ART drugs? (yes/no): ");
newARTStatus = Main.getUserInput();
newARTStatus = newARTStatus.isEmpty() ? patientInformationParts[3] : newARTStatus;
while (!Main.isValidInput(newARTStatus)) {
System.out.println(Main.RED + "Invalid input. Please enter 'yes' or 'no'." + Main.RESET);
System.out.print("Are you on ART drugs? (yes/no): ");
newARTStatus = Main.getUserInput();
newARTStatus = newARTStatus.isEmpty() ? patientInformationParts[3] : newARTStatus;
}
if (newARTStatus.equalsIgnoreCase("yes")) {
System.out.print("Enter the date you started ART (YYYY-MM-DD): ");
newStartedART = Main.getUserInput();
newStartedART = newStartedART.isEmpty() ? patientInformationParts[4] : newStartedART;
// Validate the date of starting ART
while (!Main.isValidDateOrder(newStartedART, newDiagnosisDate)) {
System.out.println(Main.RED + "Invalid date. It must be a valid date greater than the date of diagnosis." + Main.RESET);
System.out.print("Enter the date you started ART (YYYY-MM-DD): ");
newStartedART = Main.getUserInput();
}
}
}
System.out.print("Enter your new country ISO code: ");
String newCountryISO = Main.getUserInput();
newCountryISO = newCountryISO.isEmpty() ? patientInformationParts[5] : newCountryISO;
while (!Main.isValidISOCode(newCountryISO)) {
System.out.println(Main.RED + "Invalid ISO code. It cannot be empty and must be 2 or 3 uppercase letters." + Main.RESET);
System.out.print("Enter country ISO code: ");
newCountryISO = Main.getUserInput();
newCountryISO = newCountryISO.isEmpty() ? patientInformationParts[5] : newCountryISO;
}
System.out.print("Enter new password:");
char[] passwordArray = console.readPassword();
String newPassword = new String(passwordArray);
newPassword = newPassword.isEmpty() ? getPassword() : newPassword;
while (!Main.isValidPassword(newPassword)) {
System.out.println(Main.RED + "Invalid password. It cannot be empty." + Main.RESET);
System.out.print("Enter password: ");
passwordArray = System.console().readPassword();
newPassword = new String(passwordArray);
newPassword = newPassword.isEmpty() ? getPassword() : newPassword;
}
System.out.println();
System.out.println("Updating your profile...\n ");
System.out.println(this.getEmail()+"-"+ newFirstName+"-"+ newLastName+"-"+ newDOB+"-"+ newHIVStatus+"-"+ newDiagnosisDate+"-"+ newARTStatus+"-"+ newStartedART+"-"+ newCountryISO);
String result = Main.callBashScript("user-manager.sh", "update_patient_profile", this.getEmail(), newFirstName, newLastName, newPassword, newDOB, newHIVStatus, newDiagnosisDate, newARTStatus, newStartedART, newCountryISO);
if(result.equals("OK")){
Main.clearScreen();
System.out.println(Main.GREEN+"\u2714 Profile updated successfully"+Main.RESET);
}else{
System.out.println(Main.GREEN+"\u274C Failed to update profile"+Main.RESET);
}
this.setFirstName(newFirstName);
this.setLastName(newLastName);
}
// Calculates the estimated lifespan of the patient based on HIV status and ART treatment
public float calculateLifespan() {
final float factor = 0.9f; // lifespan reduction factor due to delayed ART
final int maxNotOnART = 5; // maximum expected lifespan without ART (in years)
// Retrieve patient information
String uuid = Main.callBashScript("user-manager.sh", "get_uuid", this.getEmail());
String patientInformation = Main.callBashScript("user-manager.sh", "get_patient_info", uuid);
String[] patientInformationParts = patientInformation.split(":");
// Set patient attributes from retrieved information
if (patientInformationParts[0] != null && !patientInformationParts[0].isEmpty()) {
setDateOfBirth(Main.parseDate(patientInformationParts[0]));
}
if (patientInformationParts[1] != null && !patientInformationParts[1].isEmpty()) {
setHasHIV(patientInformationParts[1]);
}
if (patientInformationParts[2] != null && !patientInformationParts[2].isEmpty()) {
setDiagnosisDate(Main.parseDate(patientInformationParts[2]));
}
if (patientInformationParts[3] != null && !patientInformationParts[3].isEmpty()) {
setIsOnART(patientInformationParts[3]);
}
if (patientInformationParts[4] != null && !patientInformationParts[4].isEmpty()) {
setStartedART(Main.parseDate(patientInformationParts[4]));
}
if (patientInformationParts[5] != null && !patientInformationParts[5].isEmpty()) {
setCountryISO(patientInformationParts[5]);
}
// Get country-specific life expectancy and calculate default lifespan
float countryLifeExpectancy = Float.parseFloat(Main.callBashScript("user-manager.sh", "get_country_lifespan", getCountryISO()));
// declare the number of years that was remaining to reach the lifespan before testing HIV Positive
float defaultLifeSpan;
// Calculate lifespan based on HIV status and ART treatment
if (getHasHIV().equals("yes")) {
float calculatedLifespan;
defaultLifeSpan = countryLifeExpectancy - Main.calculateDateDifference(getDateOfBirth(), getDiagnosisDate());
if (getIsOnART().equals("yes")) {
// calculating the number of years delayed before taking ART
int delayBeforeART = Main.calculateDateDifference(getDiagnosisDate(), getStartedART());
// calculating the number of years taking ART
int yearsOnART = Main.calculateDateDifference(getStartedART());
// calculate lifespan by applying the formula
calculatedLifespan = (float) (defaultLifeSpan * factor * Math.pow(factor, delayBeforeART));
// subtract years lived after testing HIV Positive
calculatedLifespan -= yearsOnART;
} else {
int yearsAfterDiagnosis = Main.calculateDateDifference(getDiagnosisDate());
if(defaultLifeSpan > maxNotOnART){
// There were more than 5 years to live
calculatedLifespan = maxNotOnART - yearsAfterDiagnosis;
}else{
// There were less than 5 years to live
calculatedLifespan = defaultLifeSpan - yearsAfterDiagnosis;
}
}
return calculatedLifespan>0?calculatedLifespan:0;
} else {
defaultLifeSpan = countryLifeExpectancy - Main.calculateDateDifference(getDateOfBirth());
return defaultLifeSpan>0?defaultLifeSpan:0;
}
}
// Displays the estimated lifespan of the patient
public void viewLifespan() {
Main.clearScreen();
float lifespan = this.calculateLifespan();
System.out.println("\n______________________________\n"+Main.GREEN+"Your Estimated Survival rate is " + lifespan + " years"+Main.RESET+"\n______________________________");
}
}