-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
353 lines (313 loc) · 13.9 KB
/
Main.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.Console;
public class Main {
private static User currentUser = null;
static final String RESET = "\033[0m";
static final String RED = "\033[0;31m";
static final String GREEN = "\033[0;32m";
public static void main(String[] args) {
Main.clearScreen();
// Initialize user store with admin user if it doesn't exist
callBashScript("user-manager.sh", "initialize_user_store");
// Main loop for the application
while (true) {
if (currentUser == null) {
// Display main menu options
// showLogo();
// System.out.println();
System.out.println("______________________________\nLife Prognosis Management Tool \nBy BISOKE TEAM 3\n"+Main.GREEN+">>HOME"+Main.RESET+"\n______________________________");
System.out.println("1. Login");
System.out.println("2. Complete Registration");
System.out.println("3. Exit");
System.out.print("_______________________\n"+Main.GREEN+"Choose an option: "+Main.RESET);
String option = Main.getUserInput();
System.out.println("_______________________");
// Process user input
switch (option) {
case "1":
currentUser = loginUser();
break;
case "2":
Patient.completeRegistration();
break;
case "3":
System.exit(0); // Exit the application
default:
System.out.println(Main.RED+"\u274C Invalid option"+Main.RESET);
}
} else {
// Redirect to appropriate menu based on user role
if (currentUser.getUserRole() == UserRoles.ADMIN) {
adminMenu((Admin) currentUser);
} else {
patientMenu((Patient) currentUser);
}
}
}
}
private static User loginUser() {
System.out.println("\n________ Log in ________");
Console console = System.console();
System.out.print("Enter your email: ");
String email = Main.getUserInput();
System.out.print("Enter your password: ");
char[] passwordArray = console.readPassword();
String password = new String(passwordArray);
// Call Bash script to authenticate user
String userData = User.login(email, password);
if (!userData.equals("null")) {
// Parse user data returned from the script
String[] parts = userData.split(":");
String userRole = parts[0];
String firstName = parts[1];
String lastName = parts[2];
// Create and return user object based on role
if (userRole.equals("Admin")) {
Main.clearScreen();
return new Admin(firstName, lastName, email, password);
} else {
Main.clearScreen();
return new Patient(firstName, lastName, email, password);
}
} else {
Main.clearScreen();
System.out.println(Main.RED+"\u274C Incorrect email or password."+Main.RESET);
return null;
}
}
private static void adminMenu(Admin admin) {
while (true) {
// Display admin menu options
System.out.println("______________________________\nLife Prognosis Management Tool \nBy BISOKE TEAM 3\n"+Main.GREEN+"ADMIN,"+admin.getFirstName()+""+Main.RESET+"\n______________________________");
System.out.println("1. Initiate Patient Registration");
System.out.println("2. Export User Data");
System.out.println("3. Export Analytics");
System.out.println("4. Logout");
System.out.print("_______________________\n"+Main.GREEN+"Choose an option: "+Main.RESET);
String option = Main.getUserInput();
System.out.println("_______________________");
// Process admin menu input
switch (option) {
case "1":
((Admin) currentUser).initializeRegistration();
break;
case "2":
((Admin) currentUser).exportUserData();
break;
case "3":
((Admin) currentUser).exportAnalytics();
break;
case "4":
currentUser = null;
User.logout();
return; // Return to the main loop
default:
{
Main.clearScreen();
System.out.println(Main.RED+"\u274C Invalid option"+Main.RESET);
}
}
}
}
public static void patientMenu(Patient patient) {
while (true) {
// Display patient menu options
System.out.println("______________________________\nLife Prognosis Management Tool \nBy BISOKE TEAM 3\n"+Main.GREEN+"PATIENT,"+patient.getFirstName()+""+Main.RESET+"\n______________________________");
System.out.println("1. View Profile");
System.out.println("2. Update Profile");
System.out.println("3. View Lifespan");
System.out.println("4. Logout");
System.out.print("_______________________\n"+Main.GREEN+"Choose an option: "+Main.RESET);
String option = Main.getUserInput();
System.out.println("_______________________");
// Process patient menu input
switch (option) {
case "1":
patient.viewProfile();
break;
case "2":
patient.updateProfile();
break;
case "3":
patient.viewLifespan();
break;
case "4":
currentUser = null;
User.logout();
return; // Return to the main loop
default:
System.out.println(Main.RED+"\u274C Invalid option"+Main.RESET);
}
}
}
public static String callBashScript(String scriptName, String option, String... args) {
StringBuilder output = new StringBuilder();
try {
// Prepare command to execute the Bash script
List<String> command = new ArrayList<>();
command.add("./bash-scripts/" + scriptName);
command.add(option);
for (String arg : args) {
command.add(arg);
}
// Execute command
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
// Read output from the script
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
process.waitFor();
} catch (IOException | InterruptedException e) {
System.err.println("Error calling Bash script: " + e.getMessage());
}
return output.toString().trim();
}
public static String getUserInput() {
Scanner scanner = new Scanner(System.in);
return scanner.nextLine(); // Read input from the user
}
public static String generateUUID() {
return java.util.UUID.randomUUID().toString(); // Generate a unique identifier
}
public static Date parseDate(String date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
// Parse the date string into a Date object
Date parsedDate = dateFormat.parse(date);
return parsedDate;
} catch (ParseException e) {
return null; // Return null if parsing fails
}
}
public static int calculateDateDifference(Date initialDate) {
// Convert Date to LocalDate
LocalDate initialLocalDate = initialDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
// Get today's date
LocalDate today = LocalDate.now();
// Calculate the period between the years
int diff = today.getYear() - initialLocalDate.getYear();
return diff;
}
public static int calculateDateDifference(Date initialDate, Date finalDate) {
// Convert Date to LocalDate
LocalDate initialLocalDate = initialDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate finalLocalDate = finalDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
// Calculate the period between the years
int diff = finalLocalDate.getYear() - initialLocalDate.getYear();
return diff;
}
// function to show system logo
public static void showLogo() {
String teamLogo = "\n" +
" ____ _____ _____ ____ _ __ ______ ____ \n" +
" | _ \\ |_ _| / ____| / __ \\ | |/ / | ____| |___ \\ \n" +
" | |_) | | | | (___ | | | | | ' / | |__ __) |\n" +
" | _ < | | \\___ \\ | | | | | < | __| |__ < \n" +
" | |_) | _| |_ ____) | | |__| | | . \\ | |____ ___) |\n" +
" |____/ |_____| |_____/ \\____/ |_|\\_\\ |______| |____/ \n" +
" \n" +
" ";
System.out.println(teamLogo);
}
// function to clear screen
public static void clearScreen() {
try {
String os = System.getProperty("os.name");
if (os.contains("Windows")) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} else {
System.out.print("\033[H\033[2J");
System.out.flush();
}
} catch (Exception e) {
System.out.println("Error clearing screen: " + e.getMessage());
}
}
// function to validate email
public static boolean isValidEmail(String email) {
String emailRegex = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$";
Pattern pattern = Pattern.compile(emailRegex);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
// function to validate names
public static boolean isValidName(String nameString) {
return nameString != null && !nameString.trim().isEmpty() && nameString.matches("[a-zA-Z]+");
}
// function to validate date of birth
public static boolean isValidDateOfBirth(String dob) {
if (dob == null || dob.trim().isEmpty()) {
return false;
}
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try {
LocalDate parsedDate = LocalDate.parse(dob, dateFormatter);
LocalDate today = LocalDate.now();
LocalDate hundredYearsAgo = today.minusYears(100);
// The date must be within the last 100 years and not in the future
return !parsedDate.isAfter(today) && !parsedDate.isBefore(hundredYearsAgo);
} catch (DateTimeParseException e) {
return false; // Invalid format or invalid date
}
}
// function to validate yes or no inputs
public static boolean isValidInput(String hivStatus) {
return "yes".equalsIgnoreCase(hivStatus) || "no".equalsIgnoreCase(hivStatus);
}
// function to validate diagnosis date
public static boolean isValidDiagnosisDate(String diagnosisDate, String dob) {
LocalDate dobDate = LocalDate.parse(dob, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
return isValidDate(diagnosisDate, dobDate, LocalDate.now());
}
// date comparator
private static boolean isValidDate(String date, LocalDate minDate, LocalDate maxDate) {
if (date == null || date.trim().isEmpty()) {
return false;
}
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try {
LocalDate parsedDate = LocalDate.parse(date, dateFormatter);
if (minDate != null && parsedDate.isBefore(minDate)) {
return false;
}
if (maxDate != null && parsedDate.isAfter(maxDate)) {
return false;
}
return true;
} catch (DateTimeParseException e) {
return false;
}
}
// function to validate date order
public static boolean isValidDateOrder(String startedART, String diagnosisDate) {
LocalDate diagnosisDDate = LocalDate.parse(diagnosisDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
return isValidDate(startedART, diagnosisDDate, LocalDate.now());
}
// function to validate country's ISO code
public static boolean isValidISOCode(String countryISO) {
String ISOCODE = Main.callBashScript("validation-functions.sh", "validateISO", countryISO);
return countryISO != null && countryISO.trim().matches("[A-Z]{2,3}") && countryISO.equals(ISOCODE);
}
// function to validate password
public static boolean isValidPassword(String password) {
return password != null && !password.trim().isEmpty();
}
}