-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
758 lines (662 loc) · 24.4 KB
/
main.c
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
struct Patient
{
char name[50];
int age;
char gender;
int id;
struct Patient *next;
};
struct Slot
{
char time[20];
int isReserved;
int patientId;
};
struct Slot slots[5] = {
{"2pm to 2:30pm", 0, 0},
{"2:30pm to 3pm", 0, 0},
{"3pm to 3:30pm", 0, 0},
{"4pm to 4:30pm", 0, 0},
{"4:30pm to 5pm", 0, 0}};
struct Patient *head = NULL;
int length = 0;
void Welcome_Screen();
int login();
void Admin_Window();
void Add_Patient_Window();
void Add_Patient(char name[], int age, char gender, int id);
void Edit_Patient_Record();
void reserveSlot();
void Cancel_Reservations();
void Print_Reservations();
void Print_Patients();
void User_Window();
void View_Patient_Record();
void View_Todays_Reservations();
void clearInputBuffer();
int main()
{
Welcome_Screen();
Admin_Window();
return 0;
}
void Welcome_Screen()
{
system("cls"); // Clear the console screen, should be replaced with "clear" in case of linux
// Print a header
printf("\033[1;32m"); // Set color to green
printf("**********************************\n");
printf("* Welcome to the Clinic System! *\n");
printf("**********************************\n");
printf("\033[0m"); // Reset color
// Print a menu
printf("Please choose an option:\n");
printf("1. Admin Mode\n");
printf("2. User Mode\n");
printf("3. Exit\n");
while (1)
{
int choice = 0;
// Get user input
scanf("%d", &choice);
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
// Handle the user's choice
switch (choice)
{
case 1:
// Admin Mode
printf("Admin Mode selected\n");
sleep(1);
system("cls"); // Clear the console screen
if (login()) // login
{
Admin_Window();
}
break;
case 2:
// User Mode
printf("User Mode selected\n");
sleep(1);
User_Window();
break;
case 3:
// Exit
printf("Exiting the system...\n");
exit(0); // Exit Success
default:
printf("\033[1;31m"); // Set color to red
printf("Invalid choice! Please choose a valid option.\n");
printf("\033[0m"); // Reset color
break;
}
}
}
int login()
{
int password_tries = 0;
char password[5];
while (password_tries < 3)
{
printf("Enter the password: ");
scanf("%s", password);
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
if (strcmp(password, "1234") == 0)
{
return 1; // Password is correct
}
else
{
password_tries++;
printf("\033[1;31m"); // Set color to red
printf("Incorrect password! Please try again.\n");
printf("\033[0m"); // Reset color
sleep(1); // Delay for 1 second
}
}
// Too many incorrect password attempts
printf("\033[1;31m"); // Set color to red
printf("Too many incorrect password attempts. Exiting the system...\n");
printf("\033[0m"); // Reset color
sleep(1); // Delay for 1 second
exit(0); // Exit
}
void Admin_Window()
{
system("cls"); // Clear the console screen
// print the menu
printf("Please choose an option:\n");
printf("1. Add a new patient record.\n");
printf("2. Edit patient record.\n");
printf("3. Reserve a slot with the doctor.\n");
printf("4. Cancel reservation.\n");
printf("5. Print all patients.\n");
printf("6. Print all reservations.\n");
printf("7. Back.\n");
while (1) // Loop until the user chooses to go back
{
int choice = 0;
// Get user input
scanf("%d", &choice);
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
// Handle the user's choice
switch (choice)
{
case 1:
// Add a new patient record
printf("New patient record selected.\n");
sleep(1); // Pause for 1 second
Add_Patient_Window();
break;
case 2:
// Edit a patient record
printf("Edit patient record selected.\n");
sleep(1);
Edit_Patient_Record();
break;
case 3:
// Reserve a slot
printf("Reserve a slot with the doctor selected.\n");
sleep(1);
reserveSlot();
break;
case 4:
// Cancel reservation
printf("Cancel reservation selected.\n");
sleep(1);
Cancel_Reservations();
break;
case 5:
// Print all patients
printf("Print all patients selected.\n");
sleep(1);
Print_Patients();
sleep(1);
printf("\nType (e) to go back when you are ready: ");
while (1) // Loop until the user types 'e'
{
char e;
fflush(stdin); // Flush the input buffer
scanf(" %c", &e);
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
if (e == 'e')
{
system("cls"); // Clear the console screen
Admin_Window(); // Go back to the admin window
break;
}
}
break;
case 6:
// Print all reservations
printf("Print all reservations selected.\n");
sleep(1);
system("cls"); // Clear the console screen
Print_Reservations();
sleep(1);
printf("\nType 'e' to go back when you are ready: ");
while (1) // Loop until the user types 'e'
{
char e;
fflush(stdin); // Flush the input buffer
scanf(" %c", &e);
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
if (e == 'e')
{
system("cls"); // Clear the console screen
Admin_Window(); // Go back to the admin window
break;
}
}
break;
case 7:
// Back option
Welcome_Screen();
break;
default:
printf("\033[1;31m"); // Set color to red
printf("Invalid choice! Please choose a valid option.\n");
printf("\033[0m"); // Reset color
break;
}
}
}
void Add_Patient_Window()
{
system("cls"); // Clear the console screen
char name[50];
int age;
char gender;
int id;
printf("Enter patient information:\n");
printf("Name: ");
fgets(name, sizeof(name), stdin); // to get the name with space included
name[strcspn(name, "\n")] = 0; // Remove the newline character from the name which fgets() adds to prevent problems later
while (1)
{
printf("Age: ");
if (scanf("%d", &age) != 1 || age == 0) // Check if the entered number is a non zero integer
{
printf("\033[1;31m"); // Set color to red
printf("Invalid input. Please enter a valid age.\n");
printf("\033[0m"); // Reset color
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
}
else
{
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
break; // Exit the loop if a valid age is entered
}
}
while (1)
{
printf("Gender (M or F): ");
if (scanf(" %c", &gender) != 1 || (gender != 'M' && gender != 'F')) // Check if the entered gender is a character, either 'M' or 'F'
{
printf("\033[1;31m"); // Set color to red
printf("Invalid input. Please enter either 'M' or 'F' for gender.\n");
printf("\033[0m"); // Reset color
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
}
else
{
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
break; // Exit the loop if a valid gender is entered
}
}
while (1)
{
printf("ID: ");
if (scanf("%d", &id) != 1 || id == 0) // Check if the entered ID is a non-zero integer
{
printf("\033[1;31m"); // Set color to red
printf("Invalid input. Please enter a non-zero ID.\n");
printf("\033[0m"); // Reset color
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
}
else
{
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
break; // Exit the loop if a valid ID is entered
}
}
// Call the Add_Patient function
Add_Patient(name, age, gender, id);
}
void Add_Patient(char name[], int age, char gender, int id)
{
// Check if the ID already exists
struct Patient *current = head; // Pointer to traverse the linked list
while (current != NULL)
{
if (current->id == id) // If the ID already exists
{
printf("\033[1;31m"); // Set color to red
printf("ID already exists. Entry rejected.\n");
printf("\033[0m"); // Reset color
sleep(1);
Add_Patient_Window(); // Call the function to add a new patient
}
current = current->next; // Move to the next node
}
// Create a new patient node
struct Patient *newPatient = (struct Patient *)malloc(sizeof(struct Patient));
strcpy(newPatient->name, name); // Copy the name to the new patient node
newPatient->age = age; // Assign the age
newPatient->gender = gender; // Assign the gender
newPatient->id = id; // Assign the ID
newPatient->next = NULL; // Set the next pointer to NULL
// If the linked list is empty, make the new patient the head
if (head == NULL)
{
head = newPatient;
}
else
{
// Find the last node and add the new patient to the end
struct Patient *last = head;
while (last->next != NULL)
{
last = last->next;
}
last->next = newPatient;
}
length++; // Increment the length of the linked list
printf("Patient added Successfully!\n");
sleep(1);
Admin_Window(); // Call the function to go back to the admin window
}
void Edit_Patient_Record()
{
system("cls"); // Clear the console screen
int id;
printf("Enter the Patient ID: ");
while (1)
{
if (scanf("%d", &id) != 1 || id == 0) // Check if the entered ID is a non-zero integer
{
printf("\033[1;31m"); // Set color to red
printf("Invalid input. Please enter a non-zero ID.\n");
printf("\033[0m"); // Reset color
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
}
else
{
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
break; // Exit the loop if a valid ID is entered
}
}
struct Patient *current = head; // Pointer to traverse the linked list
while (current != NULL)
{
if (current->id == id) // If the patient with the given ID is found
{
// Edit the patient information
printf("Enter new patient information:\n");
printf("Name: ");
fgets(current->name, sizeof(current->name), stdin);
current->name[strcspn(current->name, "\n")] = 0; // Remove the newline character
while (1)
{
printf("Age: ");
if (scanf("%d", &(current->age)) != 1 || current->age == 0) // Check if the entered number is a non zero integer
{
printf("\033[1;31m"); // Set color to red
printf("Invalid input. Please enter a valid age.\n");
printf("\033[0m"); // Reset color
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
}
else
{
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
break; // Exit the loop if a valid age is entered
}
}
while (1)
{
printf("Gender (M or F): ");
// Check if the entered gender is a character, either 'M' or 'F'
if (scanf(" %c", &(current->gender)) != 1 || (current->gender != 'M' && current->gender != 'F'))
{
printf("\033[1;31m"); // Set color to red
printf("Invalid input. Please enter either 'M' or 'F' for gender.\n");
printf("\033[0m"); // Reset color
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
}
else
{
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
break; // Exit the loop if a valid gender is entered
}
}
printf("Patient record updated successfully.\n");
sleep(1);
Admin_Window(); // Call the function to go back to the admin window
}
current = current->next; // Move to the next node
}
// If we reach here, it means the patient with the given ID was not found
printf("\033[1;31m"); // Set color to red
printf("Incorrect ID! No patient found with the given ID.\n");
printf("\033[0m"); // Reset color
sleep(2);
Admin_Window(); // Call the function to go back to the admin window
}
void reserveSlot()
{
system("cls"); // Clear the console screen
int id;
int slotNumber;
// Display the available slots
printf("Available slots:\n");
for (int i = 0; i < 5; i++)
{
if (slots[i].isReserved == 0) // If the slot is not reserved
{
printf("%d. %s\n", i + 1, slots[i].time);
}
}
// Get the patient ID and desired slot from the admin
printf("\nEnter the patient ID: ");
while (1)
{
if (scanf("%d", &id) != 1 || id == 0) // Check if the entered ID is a non-zero integer
{
printf("\033[1;31m"); // Set color to red
printf("Invalid input. Please enter a non-zero ID.\n");
printf("\033[0m"); // Reset color
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
}
else
{
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
break; // Exit the loop if a valid ID is entered
}
}
// Check if the patient exists
int found = 0;
struct Patient *current = head; // Pointer to traverse the linked list
while (current != NULL)
{
if (current->id == id) // If the patient with the given ID is found
{
found = 1; // Patient exists
break;
}
current = current->next; // Move to the next node
}
if (found == 0)
{
printf("\033[1;31m"); // Set color to red
printf("Patient ID is not found.");
printf("\033[0m"); // Reset color
sleep(2);
Admin_Window(); // Call the function to go back to the admin window
}
printf("Enter the number of the desired slot: ");
while (1)
{
if (scanf("%d", &slotNumber) != 1) // Check if the entered ID is a non-zero integer
{
printf("\033[1;31m"); // Set color to red
printf("Invalid input.\n");
printf("\033[0m"); // Reset color
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
}
else
{
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
break; // Exit the loop if a valid ID is entered
}
}
// Check if the slot is available
if (slotNumber < 1 || slotNumber > 5 || slots[slotNumber - 1].isReserved == 1)
{
printf("\033[1;31m"); // Set color to red
printf("Invalid slot number! Please try again.\n");
printf("\033[0m"); // Reset color
sleep(2);
system("cls"); // Clear the console screen
reserveSlot(); // Call the function again to reserve a slot
}
// Reserve the slot
slots[slotNumber - 1].isReserved = 1; // Mark the slot as reserved
slots[slotNumber - 1].patientId = id; // Assign the patient ID to the slot
printf("Slot reserved successfully.\n");
sleep(2);
Admin_Window(); // Call the function to go back to the admin window
}
void Cancel_Reservations()
{
system("cls"); // Clear the console screen
int id;
printf("Enter the patient ID to cancel his reservation: ");
while (1)
{
if (scanf("%d", &id) != 1 || id == 0) // Check if the entered number is an integer
{
printf("\033[1;31m"); // Set color to red
printf("Invalid input. Please enter a valid ID.\n");
printf("\033[0m"); // Reset color
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
}
else
{
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
break; // Exit the loop if a valid age is entered
}
}
// Check each slot to see if it's reserved by the patient
for (int i = 0; i < 5; i++)
{
if (slots[i].isReserved == 1 && slots[i].patientId == id) // If the slot is reserved by the patient
{
// Cancel the reservation
slots[i].isReserved = 0; // Mark the slot as not reserved
slots[i].patientId = 0; // Remove the patient ID from the slot
printf("Reservation for patient ID %d in slot %s has been cancelled.\n", id, slots[i].time);
sleep(2);
Admin_Window(); // Call the function to go back to the admin window
return;
}
}
// If we reach here, it means the patient didn't have any reservations
printf("\033[1;31m"); // Set color to red
printf("No reservations found for patient ID %d.\n", id);
printf("\033[0m"); // Reset color
sleep(2);
Admin_Window(); // Call the function to go back to the admin window
}
void Print_Patients()
{
struct Patient *current = head; // Pointer to traverse the linked list starting from the head
while (current != NULL) // Iterate until the end of the linked list is reached
{
printf("Name: %s, Age: %d, Gender: %c, ID: %d\n", current->name, current->age, current->gender, current->id);
// Print the patient's name, age, gender, and ID using the printf function
current = current->next; // Move to the next node in the linked list
}
}
void Print_Reservations()
{
printf("Reservations:\n"); // Print the heading for the reservations
for (int i = 0; i < 5; i++)
{
if (slots[i].isReserved == 1) // If the slot is reserved
{
printf("Slot: %s, Patient ID: %d\n", slots[i].time, slots[i].patientId); // Print the slot time and patient ID
}
}
}
void User_Window()
{
system("cls"); // Clear the console screen
// print the menu:
printf("Please choose an option:\n");
printf("1. View patient record.\n");
printf("2. View today's reservations.\n");
printf("3. Back.\n");
while (1) // Infinite loop to keep the user in the user window until they choose to go back
{
int choice = 0;
// Get user input
scanf("%d", &choice);
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
// Handle the user's choice
switch (choice)
{
case 1:
// View patient record
printf("View patient record selected.\n");
sleep(1);
View_Patient_Record(); // Call the function to view the patient record
break;
case 2:
// View today's reservations
printf("View today's reservations selected.\n");
sleep(1);
View_Todays_Reservations(); // Call the function to view today's reservations
break;
case 3:
Welcome_Screen(); // Call the function to go back to the welcome screen
break;
default:
printf("\033[1;31m"); // Set color to red
printf("Invalid choice! Please choose a valid option.\n");
printf("\033[0m"); // Reset color
break;
}
}
}
void View_Patient_Record()
{
system("cls"); // Clear the console screen
int id;
printf("Enter the patient ID: ");
while (1)
{
if (scanf("%d", &id) != 1 || id == 0) // Check if the entered ID is a non-zero integer
{
printf("\033[1;31m"); // Set color to red
printf("Invalid input. Please enter a non-zero ID.\n");
printf("\033[0m"); // Reset color
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
}
else
{
clearInputBuffer(); // Clear the input buffer to prevent issues with subsequent input
break; // Exit the loop if a valid ID is entered
}
}
// Find the patient with the given ID
struct Patient *current = head; // Pointer to traverse the linked list starting from the head
while (current != NULL) // Iterate until the end of the linked list is reached
{
if (current->id == id) // If the current patient's ID matches the given ID
{
// Print the patient's information
printf("Patient ID: %d\n", current->id);
printf("Name: %s\n", current->name);
printf("Age: %d\n", current->age);
printf("Gender: %c\n", current->gender);
printf("\n");
sleep(5);
User_Window(); // Call the function to go back to the user window
return;
}
current = current->next; // Move to the next node in the linked list
}
// If we reach here, it means no patient was found with the given ID
printf("\033[1;31m"); // Set color to red
printf("No patient found with ID %d.\n", id);
printf("\033[0m"); // Reset color
sleep(5);
User_Window(); // Call the function to go back to the user window
}
void View_Todays_Reservations()
{
system("cls"); // Clear the console screen
printf("Today's Reservations:\n");
for (int i = 0; i < 5; i++)
{
if (slots[i].isReserved == 1) // If the slot is reserved
{
printf("Slot: %s\n", slots[i].time); // Print the slot time
printf("Patient ID: %d\n", slots[i].patientId); // Print the patient ID
printf("\n");
}
}
sleep(5);
User_Window(); // Call the function to go back to the user window
}
void clearInputBuffer()
{
int c;
while ((c = getchar()) != '\n' && c != EOF) // Read characters from the input buffer until a newline character or end-of-file is encountered
{
// Do nothing with the characters, effectively discarding them
}
}