-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSHMS.java
1495 lines (1232 loc) · 57.6 KB
/
SHMS.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
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.util.*;
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.chart.*;
import javafx.collections.*;
import javafx.geometry.*;
import java.time.*;
import java.time.format.*;
import javafx.beans.property.*;
public class SHMS extends Application {
// Shared data stores
private static final ObservableList<Patient> patients = FXCollections.observableArrayList();
private static final ObservableList<Doctor> doctors = FXCollections.observableArrayList();
private static final ObservableList<Appointment> appointments = FXCollections.observableArrayList();
private static final ObservableList<BillingRecord> billingRecords = FXCollections.observableArrayList();
@Override
public void start(Stage primaryStage) {
TabPane tabPane = new TabPane();
// Initialize views with shared data stores
Tab patientTab = new Tab("Patient Management");
patientTab.setContent(new PatientManagementView(patients));
patientTab.setClosable(false);
Tab appointmentTab = new Tab("Appointment Scheduling");
appointmentTab.setContent(new AppointmentSchedulingView(patients, doctors, appointments));
appointmentTab.setClosable(false);
Tab doctorTab = new Tab("Doctor Management");
doctorTab.setContent(new DoctorManagementView(doctors));
doctorTab.setClosable(false);
Tab billingTab = new Tab("Billing");
billingTab.setContent(new BillingView(patients, billingRecords));
billingTab.setClosable(false);
Tab analyticsTab = new Tab("Analytics");
analyticsTab.setContent(new AnalyticsView(patients, doctors, appointments, billingRecords));
analyticsTab.setClosable(false);
tabPane.getTabs().addAll(patientTab, appointmentTab, doctorTab, billingTab, analyticsTab);
Scene scene = new Scene(tabPane, 1024, 768);
primaryStage.setScene(scene);
primaryStage.setTitle("Smart Healthcare Management System");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class Patient {
private final javafx.beans.property.StringProperty name;
private final javafx.beans.property.ObjectProperty<LocalDate> dateOfBirth;
private final javafx.beans.property.StringProperty contactInfo;
private final javafx.beans.property.StringProperty medicalHistory;
private final javafx.beans.property.StringProperty patientId;
public Patient(String name, LocalDate dateOfBirth, String contactInfo, String medicalHistory) {
this.name = new javafx.beans.property.SimpleStringProperty(name);
this.dateOfBirth = new javafx.beans.property.SimpleObjectProperty<>(dateOfBirth);
this.contactInfo = new javafx.beans.property.SimpleStringProperty(contactInfo);
this.medicalHistory = new javafx.beans.property.SimpleStringProperty(medicalHistory);
this.patientId = new javafx.beans.property.SimpleStringProperty(generatePatientId());
}
public Patient(String patientId, String name, LocalDate dateOfBirth, String contactInfo, String medicalHistory) {
this.patientId = new javafx.beans.property.SimpleStringProperty(patientId);
this.name = new javafx.beans.property.SimpleStringProperty(name);
this.dateOfBirth = new javafx.beans.property.SimpleObjectProperty<>(dateOfBirth);
this.contactInfo = new javafx.beans.property.SimpleStringProperty(contactInfo);
this.medicalHistory = new javafx.beans.property.SimpleStringProperty(medicalHistory);
}
public String getName() {
return name.get();
}
public javafx.beans.property.StringProperty nameProperty() {
return name;
}
public javafx.beans.property.ObjectProperty<LocalDate> dateOfBirthProperty() {
return dateOfBirth;
}
public javafx.beans.property.StringProperty contactInfoProperty() {
return contactInfo;
}
public javafx.beans.property.StringProperty medicalHistoryProperty() {
return medicalHistory;
}
public javafx.beans.property.StringProperty patientIdProperty() {
return patientId;
}
public String getPatientId() {
return patientId.get();
}
public LocalDate getDateOfBirth() {
return dateOfBirth.get();
}
public String getContactInfo() {
return contactInfo.get();
}
public String getMedicalHistory() {
return medicalHistory.get();
}
private String generatePatientId() {
// Simple ID generation
return "P" + System.currentTimeMillis() % 10000;
}
@Override
public String toString() {
return getName();
}
}
class PatientManagementView extends VBox {
private final TextField nameField;
private final DatePicker dateOfBirthPicker;
private final TextField contactInfoField;
private final TextArea medicalHistoryArea;
private final TextField patientIdField;
private final TableView<Patient> patientTable;
private final ObservableList<Patient> patients;
private final String SYSTEM_PASSWORD = "javaFX_24";
private final TextField searchField;
private final ComboBox<String> searchCriteriaBox;
private Patient currentEditingPatient;
private Button addUpdateButton;
public PatientManagementView(ObservableList<Patient> patients) {
this.patients = patients;
// Initialize search components
searchField = new TextField();
searchField.setPromptText("Enter search term...");
searchCriteriaBox = new ComboBox<>();
searchCriteriaBox.getItems().addAll("ID", "Name", "Contact Info");
searchCriteriaBox.setValue("Name");
// Initialize input components
patientIdField = new TextField();
patientIdField.setPromptText("Leave empty for auto-generated ID");
nameField = new TextField();
dateOfBirthPicker = new DatePicker();
contactInfoField = new TextField();
medicalHistoryArea = new TextArea();
medicalHistoryArea.setPrefRowCount(3);
patientTable = new TableView<>();
setupPatientTable();
addUpdateButton = new Button("Add Patient");
addUpdateButton.setOnAction(e -> handleAdd());
// Layout
HBox searchBox = createSearchBox();
GridPane inputGrid = createInputGrid();
HBox buttonBox = new HBox(10, addUpdateButton);
getChildren().addAll(searchBox, inputGrid, buttonBox, patientTable);
setSpacing(10);
setPadding(new Insets(10));
searchField.textProperty().addListener((obs, oldVal, newVal) -> performSearch());
}
private void viewPatientInformation(Patient patient) {
Stage infoStage = new Stage();
infoStage.initModality(Modality.APPLICATION_MODAL);
infoStage.setTitle("Patient Information");
VBox content = new VBox(10);
content.setPadding(new Insets(15));
TextField nameField = new TextField(patient.getName());
DatePicker dobPicker = new DatePicker(patient.getDateOfBirth());
TextField contactField = new TextField(patient.getContactInfo());
TextArea historyArea = new TextArea(patient.getMedicalHistory());
historyArea.setPrefRowCount(5);
historyArea.setWrapText(true);
// Initially set fields as non-editable
nameField.setEditable(false);
dobPicker.setEditable(false);
contactField.setEditable(false);
historyArea.setEditable(false);
Button editButton = new Button("Edit");
Button saveButton = new Button("Save Changes");
Button closeButton = new Button("Close");
HBox buttonBox = new HBox(10, editButton, saveButton, closeButton);
// Set up buttons
editButton.setOnAction(e -> showPasswordDialogForEdit(nameField, dobPicker,
contactField, historyArea,
saveButton));
saveButton.setDisable(true);
saveButton.setOnAction(e -> {
updatePatient(patient, nameField.getText(), dobPicker.getValue(),
contactField.getText(), historyArea.getText());
infoStage.close();
});
closeButton.setOnAction(e -> infoStage.close());
content.getChildren().addAll(
new Label("Patient ID: " + patient.getPatientId()),
new Label("Name:"), nameField,
new Label("Date of Birth:"), dobPicker,
new Label("Contact:"), contactField,
new Label("Medical History:"), historyArea,
buttonBox
);
Scene scene = new Scene(content);
infoStage.setScene(scene);
infoStage.showAndWait();
}
private void setupPatientTable() {
// ID column
TableColumn<Patient, String> idCol = new TableColumn<>("ID");
idCol.setCellValueFactory(cellData -> cellData.getValue().patientIdProperty());
// Name column
TableColumn<Patient, String> nameCol = new TableColumn<>("Name");
nameCol.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
// Date of Birth column
TableColumn<Patient, LocalDate> dobCol = new TableColumn<>("Date of Birth");
dobCol.setCellValueFactory(cellData -> cellData.getValue().dateOfBirthProperty());
// Contact Info column
TableColumn<Patient, String> contactCol = new TableColumn<>("Contact Info");
contactCol.setCellValueFactory(cellData -> cellData.getValue().contactInfoProperty());
// Actions column
TableColumn<Patient, Void> actionsCol = new TableColumn<>("Actions");
actionsCol.setCellFactory(column -> new TableCell<>() {
private final Button viewButton = new Button("View");
{
viewButton.setOnAction(event -> {
Patient patient = getTableView().getItems().get(getIndex());
showPasswordDialog(patient);
});
}
@Override
protected void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(viewButton);
}
}
});
patientTable.getColumns().addAll(idCol, nameCol, dobCol, contactCol, actionsCol);
patientTable.setItems(patients);
}
private void handleAdd() {
String name = nameField.getText().trim();
LocalDate dob = dateOfBirthPicker.getValue();
String contactInfo = contactInfoField.getText().trim();
String medicalHistory = medicalHistoryArea.getText().trim();
String customId = patientIdField.getText().trim();
if (name.isEmpty() || dob == null || contactInfo.isEmpty()) {
showAlert("Error", "Please fill in all required fields.", Alert.AlertType.ERROR);
return;
}
// Check for duplicate patient
Optional<Patient> existingPatient = findExistingPatient(name, dob);
if (existingPatient.isPresent()) {
showAlert("Duplicate Patient",
"A patient with this name and date of birth already exists.",
Alert.AlertType.WARNING);
return;
}
// Check if custom ID is already in use
if (!customId.isEmpty() && patients.stream()
.anyMatch(p -> p.getPatientId().equals(customId))) {
showAlert("Error", "This Patient ID is already in use.", Alert.AlertType.ERROR);
return;
}
Patient newPatient = customId.isEmpty() ?
new Patient(name, dob, contactInfo, medicalHistory) :
new Patient(customId, name, dob, contactInfo, medicalHistory);
patients.add(newPatient);
showAlert("Success", "Patient added successfully.", Alert.AlertType.INFORMATION);
clearInputFields();
}
private void showPasswordDialogForEdit(TextField nameField, DatePicker dobPicker,
TextField contactField, TextArea historyArea,
Button saveButton) {
Dialog<String> dialog = new Dialog<>();
dialog.setTitle("Authentication Required");
dialog.setHeaderText("Please enter password to edit patient information");
ButtonType loginButtonType = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
PasswordField passwordField = new PasswordField();
passwordField.setPromptText("Password");
VBox content = new VBox(10);
content.getChildren().addAll(new Label("Password:"), passwordField);
dialog.getDialogPane().setContent(content);
dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
return passwordField.getText();
}
return null;
});
Optional<String> result = dialog.showAndWait();
result.ifPresent(password -> {
if (password.equals(SYSTEM_PASSWORD)) {
enableEditing(nameField, dobPicker, contactField, historyArea, saveButton);
} else {
showAlert("Error", "Incorrect password!", Alert.AlertType.ERROR);
}
});
}
private void enableEditing(TextField nameField, DatePicker dobPicker,
TextField contactField, TextArea historyArea,
Button saveButton) {
nameField.setEditable(true);
dobPicker.setEditable(true);
contactField.setEditable(true);
historyArea.setEditable(true);
saveButton.setDisable(false);
}
private void updatePatient(Patient patient, String name, LocalDate dob,
String contactInfo, String medicalHistory) {
patient.nameProperty().set(name);
patient.dateOfBirthProperty().set(dob);
patient.contactInfoProperty().set(contactInfo);
patient.medicalHistoryProperty().set(medicalHistory);
showAlert("Success", "Patient information updated successfully.",
Alert.AlertType.INFORMATION);
}
private GridPane createInputGrid() {
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(10));
grid.addRow(0, new Label("Patient ID (Optional):"), patientIdField);
grid.addRow(1, new Label("Name:"), nameField);
grid.addRow(2, new Label("Date of Birth:"), dateOfBirthPicker);
grid.addRow(3, new Label("Contact Info:"), contactInfoField);
grid.addRow(4, new Label("Medical History:"), medicalHistoryArea);
return grid;
}
private void showAlert(String title, String content, Alert.AlertType alertType) {
Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(content);
alert.showAndWait();
}
private HBox createSearchBox() {
HBox searchBox = new HBox(10);
searchBox.setAlignment(Pos.CENTER_LEFT);
searchBox.getChildren().addAll(
new Label("Search by:"),
searchCriteriaBox,
searchField
);
return searchBox;
}
private void performSearch() {
String searchTerm = searchField.getText().toLowerCase();
String criteria = searchCriteriaBox.getValue();
if (searchTerm.isEmpty()) {
patientTable.setItems(patients);
return;
}
ObservableList<Patient> filteredList = FXCollections.observableArrayList();
for (Patient patient : patients) {
boolean matches = switch (criteria) {
case "ID" -> patient.getPatientId().toLowerCase().contains(searchTerm);
case "Name" -> patient.getName().toLowerCase().contains(searchTerm);
case "Contact Info" -> patient.getContactInfo().toLowerCase().contains(searchTerm);
default -> false;
};
if (matches) {
filteredList.add(patient);
}
}
patientTable.setItems(filteredList);
}
private void startEditing(Patient patient) {
currentEditingPatient = patient;
nameField.setText(patient.getName());
dateOfBirthPicker.setValue(patient.getDateOfBirth());
contactInfoField.setText(patient.getContactInfo());
medicalHistoryArea.setText(patient.getMedicalHistory());
addUpdateButton.setText("Update Patient");
addUpdateButton.getScene().lookup("Button:contains('Cancel')").setVisible(true);
}
private void cancelEditing() {
currentEditingPatient = null;
clearInputFields();
addUpdateButton.setText("Add Patient");
addUpdateButton.getScene().lookup("Button:contains('Cancel')").setVisible(false);
}
private void handleAddUpdate() {
String name = nameField.getText().trim();
LocalDate dob = dateOfBirthPicker.getValue();
String contactInfo = contactInfoField.getText().trim();
String medicalHistory = medicalHistoryArea.getText().trim();
if (name.isEmpty() || dob == null || contactInfo.isEmpty()) {
showAlert("Error", "Please fill in all required fields.");
return;
}
if (currentEditingPatient != null) {
// Update existing patient
currentEditingPatient.nameProperty().set(name);
currentEditingPatient.dateOfBirthProperty().set(dob);
currentEditingPatient.contactInfoProperty().set(contactInfo);
currentEditingPatient.medicalHistoryProperty().set(medicalHistory);
showAlert("Success", "Patient information updated successfully.");
cancelEditing();
} else {
// Check for duplicate patient
Optional<Patient> existingPatient = findExistingPatient(name, dob);
if (existingPatient.isPresent()) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Duplicate Patient");
alert.setHeaderText("A patient with this name and date of birth already exists.");
alert.setContentText("Would you like to update the existing patient record?");
alert.showAndWait().ifPresent(response -> {
if (response == ButtonType.OK) {
startEditing(existingPatient.get());
}
});
return;
}
patients.add(new Patient(name, dob, contactInfo, medicalHistory));
showAlert("Success", "Patient added successfully.");
clearInputFields();
}
}
private Optional<Patient> findExistingPatient(String name, LocalDate dob) {
return patients.stream()
.filter(p -> p.getName().equalsIgnoreCase(name) && p.getDateOfBirth().equals(dob))
.findFirst();
}
private void showPasswordDialog(Patient patient) {
Dialog<String> dialog = new Dialog<>();
dialog.setTitle("Authentication Required");
dialog.setHeaderText("Please enter password to view patient information");
ButtonType loginButtonType = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
PasswordField passwordField = new PasswordField();
passwordField.setPromptText("Password");
VBox content = new VBox(10);
content.getChildren().addAll(new Label("Password:"), passwordField);
dialog.getDialogPane().setContent(content);
dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
return passwordField.getText();
}
return null;
});
Optional<String> result = dialog.showAndWait();
result.ifPresent(password -> {
if (password.equals(SYSTEM_PASSWORD)) {
viewPatientInformation(patient);
} else {
showAlert("Error", "Incorrect password!");
}
});
}
private void addPatient() {
String name = nameField.getText().trim();
LocalDate dob = dateOfBirthPicker.getValue();
String contactInfo = contactInfoField.getText().trim();
String medicalHistory = medicalHistoryArea.getText().trim();
if (name.isEmpty() || dob == null || contactInfo.isEmpty()) {
showAlert("Error", "Please fill in all required fields.");
return;
}
patients.add(new Patient(name, dob, contactInfo, medicalHistory));
clearInputFields();
}
private void clearInputFields() {
nameField.clear();
dateOfBirthPicker.setValue(null);
contactInfoField.clear();
medicalHistoryArea.clear();
}
private void showAlert(String title, String content) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(content);
alert.showAndWait();
}
}
class AppointmentSchedulingView extends VBox {
private final ComboBox<Patient> patientComboBox;
private final ComboBox<Doctor> doctorComboBox;
private final DatePicker appointmentDatePicker;
private final ComboBox<LocalTime> appointmentTimeComboBox;
private final TableView<Appointment> appointmentTable;
private final ObservableList<Appointment> appointments;
private final ObservableList<Patient> patients;
private final ObservableList<Doctor> doctors;
public AppointmentSchedulingView(ObservableList<Patient> patients,
ObservableList<Doctor> doctors,
ObservableList<Appointment> appointments) {
this.patients = patients;
this.doctors = doctors;
this.appointments = appointments;
// Initialize components
patientComboBox = new ComboBox<>(patients);
doctorComboBox = new ComboBox<>(doctors);
appointmentDatePicker = new DatePicker();
appointmentTimeComboBox = new ComboBox<>(createTimeSlots());
appointmentTable = new TableView<>();
setupAppointmentTable();
// Layout
GridPane inputGrid = createInputGrid();
Button scheduleButton = new Button("Schedule Appointment");
scheduleButton.setOnAction(e -> scheduleAppointment());
getChildren().addAll(inputGrid, scheduleButton, appointmentTable);
setSpacing(10);
setPadding(new Insets(10));
// Add listeners for data changes
patients.addListener((ListChangeListener<Patient>) c -> patientComboBox.setItems(FXCollections.observableArrayList(patients)));
doctors.addListener((ListChangeListener<Doctor>) c -> doctorComboBox.setItems(FXCollections.observableArrayList(doctors)));
// Set default date to today
appointmentDatePicker.setValue(LocalDate.now());
// Add date validation
appointmentDatePicker.setDayCellFactory(picker -> new DateCell() {
@Override
public void updateItem(LocalDate date, boolean empty) {
super.updateItem(date, empty);
setDisabled(empty || date.compareTo(LocalDate.now()) < 0);
}
});
}
private ObservableList<LocalTime> createTimeSlots() {
ObservableList<LocalTime> timeSlots = FXCollections.observableArrayList();
LocalTime startTime = LocalTime.of(9, 0);
LocalTime endTime = LocalTime.of(17, 0);
while (!startTime.isAfter(endTime)) {
timeSlots.add(startTime);
startTime = startTime.plusMinutes(30);
}
return timeSlots;
}
private void setupAppointmentTable() {
TableColumn<Appointment, String> patientCol = new TableColumn<>("Patient");
patientCol.setCellValueFactory(cellData -> cellData.getValue().patientProperty());
TableColumn<Appointment, String> doctorCol = new TableColumn<>("Doctor");
doctorCol.setCellValueFactory(cellData -> cellData.getValue().doctorProperty());
TableColumn<Appointment, LocalDate> dateCol = new TableColumn<>("Date");
dateCol.setCellValueFactory(cellData -> cellData.getValue().dateProperty());
TableColumn<Appointment, LocalTime> timeCol = new TableColumn<>("Time");
timeCol.setCellValueFactory(cellData -> cellData.getValue().timeProperty());
TableColumn<Appointment, Void> actionCol = new TableColumn<>("Actions");
actionCol.setCellFactory(column -> new TableCell<>() {
private final Button deleteButton = new Button("Cancel");
{
deleteButton.setOnAction(event -> {
Appointment appointment = getTableView().getItems().get(getIndex());
handleAppointmentCancellation(appointment);
});
}
@Override
protected void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
setGraphic(empty ? null : deleteButton);
}
});
appointmentTable.getColumns().addAll(patientCol, doctorCol, dateCol, timeCol, actionCol);
appointmentTable.setItems(appointments);
}
private GridPane createInputGrid() {
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(10));
grid.addRow(0, new Label("Patient:"), patientComboBox);
grid.addRow(1, new Label("Doctor:"), doctorComboBox);
grid.addRow(2, new Label("Date:"), appointmentDatePicker);
grid.addRow(3, new Label("Time:"), appointmentTimeComboBox);
return grid;
}
private void scheduleAppointment() {
Patient patient = patientComboBox.getValue();
Doctor doctor = doctorComboBox.getValue();
LocalDate date = appointmentDatePicker.getValue();
LocalTime time = appointmentTimeComboBox.getValue();
if (patient == null || doctor == null || date == null || time == null) {
showAlert(Alert.AlertType.ERROR, "Error", "Please fill in all required fields.");
return;
}
if (date.isBefore(LocalDate.now())) {
showAlert(Alert.AlertType.ERROR, "Error", "Cannot schedule appointments in the past.");
return;
}
if (isTimeSlotTaken(doctor, date, time)) {
showAlert(Alert.AlertType.ERROR, "Error", "This time slot is already taken for the selected doctor.");
return;
}
Appointment newAppointment = new Appointment(patient.getPatientId(), patient.getName(), doctor.getName(), date, time);
appointments.add(newAppointment);
showAlert(Alert.AlertType.INFORMATION, "Success", "Appointment scheduled successfully.");
clearInputFields();
}
private boolean isTimeSlotTaken(Doctor doctor, LocalDate date, LocalTime time) {
return appointments.stream()
.anyMatch(apt ->
apt.doctorProperty().get().equals(doctor.getName()) &&
apt.dateProperty().get().equals(date) &&
apt.timeProperty().get().equals(time)
);
}
private void handleAppointmentCancellation(Appointment appointment) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Cancel Appointment");
alert.setHeaderText("Cancel appointment for " + appointment.patientProperty().get());
alert.setContentText("Are you sure you want to cancel this appointment?");
alert.showAndWait().ifPresent(response -> {
if (response == ButtonType.OK) {
appointments.remove(appointment);
showAlert(Alert.AlertType.INFORMATION, "Success", "Appointment cancelled successfully.");
}
});
}
private void clearInputFields() {
patientComboBox.setValue(null);
doctorComboBox.setValue(null);
appointmentDatePicker.setValue(LocalDate.now());
appointmentTimeComboBox.setValue(null);
}
private void showAlert(Alert.AlertType alertType, String title, String content) {
Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(content);
alert.showAndWait();
}
}
class DoctorManagementView extends VBox {
private final TextField nameField;
private final TextField specializationField;
private final TextField contactInfoField;
private final TableView<Doctor> doctorTable;
private final ObservableList<Doctor> doctors;
public DoctorManagementView(ObservableList<Doctor> doctors) {
this.doctors = doctors;
// Initialize components
nameField = new TextField();
specializationField = new TextField();
contactInfoField = new TextField();
doctorTable = new TableView<>();
setupDoctorTable();
// Layout
GridPane inputGrid = createInputGrid();
Button addUpdateButton = new Button("Add Doctor");
addUpdateButton.setOnAction(e -> addDoctor());
getChildren().addAll(inputGrid, addUpdateButton, doctorTable);
setSpacing(10);
setPadding(new Insets(10));
}
private void setupDoctorTable() {
TableColumn<Doctor, String> nameCol = new TableColumn<>("Name");
nameCol.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
TableColumn<Doctor, String> specializationCol = new TableColumn<>("Specialization");
specializationCol.setCellValueFactory(cellData -> cellData.getValue().specializationProperty());
TableColumn<Doctor, String> contactCol = new TableColumn<>("Contact Info");
contactCol.setCellValueFactory(cellData -> cellData.getValue().contactInfoProperty());
TableColumn<Doctor, Void> actionCol = new TableColumn<>("Actions");
actionCol.setCellFactory(column -> new TableCell<>() {
private final Button deleteButton = new Button("Remove");
{
deleteButton.setOnAction(event -> {
Doctor doctor = getTableView().getItems().get(getIndex());
handleDoctorRemoval(doctor);
});
}
@Override
protected void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
setGraphic(empty ? null : deleteButton);
}
});
doctorTable.getColumns().addAll(nameCol, specializationCol, contactCol, actionCol);
doctorTable.setItems(doctors);
}
private GridPane createInputGrid() {
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(10));
grid.addRow(0, new Label("Name:"), nameField);
grid.addRow(1, new Label("Specialization:"), specializationField);
grid.addRow(2, new Label("Contact Info:"), contactInfoField);
return grid;
}
private void addDoctor() {
String name = nameField.getText().trim();
String specialization = specializationField.getText().trim();
String contactInfo = contactInfoField.getText().trim();
if (name.isEmpty() || specialization.isEmpty() || contactInfo.isEmpty()) {
showAlert(Alert.AlertType.ERROR, "Error", "Please fill in all required fields.");
return;
}
Doctor newDoctor = new Doctor(name, specialization, contactInfo);
doctors.add(newDoctor);
showAlert(Alert.AlertType.INFORMATION, "Success", "Doctor added successfully.");
clearInputFields();
}
private void handleDoctorRemoval(Doctor doctor) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Remove Doctor");
alert.setHeaderText("Remove " + doctor.getName());
alert.setContentText("Are you sure you want to remove this doctor?");
alert.showAndWait().ifPresent(response -> {
if (response == ButtonType.OK) {
doctors.remove(doctor);
showAlert(Alert.AlertType.INFORMATION, "Success", "Doctor removed successfully.");
}
});
}
private void clearInputFields() {
nameField.clear();
specializationField.clear();
contactInfoField.clear();
}
private void showAlert(Alert.AlertType alertType, String title, String content) {
Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(content);
alert.showAndWait();
}
}
class Doctor {
private final javafx.beans.property.StringProperty name;
private final javafx.beans.property.StringProperty specialization;
private final javafx.beans.property.StringProperty contactInfo;
public Doctor(String name, String specialization, String contactInfo) {
this.name = new javafx.beans.property.SimpleStringProperty(name);
this.specialization = new javafx.beans.property.SimpleStringProperty(specialization);
this.contactInfo = new javafx.beans.property.SimpleStringProperty(contactInfo);
}
public String getName() {
return name.get();
}
public javafx.beans.property.StringProperty nameProperty() {
return name;
}
public javafx.beans.property.StringProperty specializationProperty() {
return specialization;
}
public javafx.beans.property.StringProperty contactInfoProperty() {
return contactInfo;
}
@Override
public String toString() {
return getName();
}
}
class BillingView extends VBox {
private final ComboBox<Patient> patientComboBox;
private final TextField serviceField;
private final TextField amountField;
private final DatePicker billingDatePicker;
private final TableView<BillingRecord> billingTable;
private final ObservableList<BillingRecord> billingRecords;
private final ObservableList<Patient> patients;
public BillingView(ObservableList<Patient> patients, ObservableList<BillingRecord> billingRecords) {
this.patients = patients;
this.billingRecords = billingRecords;
// Initialize components
patientComboBox = new ComboBox<>(patients);
serviceField = new TextField();
amountField = new TextField();
billingDatePicker = new DatePicker(LocalDate.now());
billingTable = new TableView<>();
setupBillingTable();
// Layout
GridPane inputGrid = createInputGrid();
Button addUpdateButton = new Button("Add Billing Record");
addUpdateButton.setOnAction(e -> addBillingRecord());
getChildren().addAll(inputGrid, addUpdateButton, billingTable);
setSpacing(10);
setPadding(new Insets(10));
// Add listener for patient data changes
patients.addListener((ListChangeListener<Patient>) c ->
patientComboBox.setItems(FXCollections.observableArrayList(patients)));
}
private void setupBillingTable() {
TableColumn<BillingRecord, String> patientCol = new TableColumn<>("Patient");
patientCol.setCellValueFactory(cellData -> cellData.getValue().patientProperty());
TableColumn<BillingRecord, String> serviceCol = new TableColumn<>("Service");
serviceCol.setCellValueFactory(cellData -> cellData.getValue().serviceProperty());
TableColumn<BillingRecord, Double> amountCol = new TableColumn<>("Amount");
amountCol.setCellValueFactory(cellData -> cellData.getValue().amountProperty().asObject());
amountCol.setCellFactory(col -> new TableCell<>() {
@Override
protected void updateItem(Double amount, boolean empty) {
super.updateItem(amount, empty);
if (empty || amount == null) {
setText(null);
} else {
setText(String.format("$%.2f", amount));
}
}
});
TableColumn<BillingRecord, LocalDate> dateCol = new TableColumn<>("Date");
dateCol.setCellValueFactory(cellData -> cellData.getValue().dateProperty());
TableColumn<BillingRecord, Void> actionCol = new TableColumn<>("Actions");
actionCol.setCellFactory(column -> new TableCell<>() {
private final Button deleteButton = new Button("Delete");
{
deleteButton.setOnAction(event -> {
BillingRecord record = getTableView().getItems().get(getIndex());
handleBillingRecordDeletion(record);
});
}
@Override
protected void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
setGraphic(empty ? null : deleteButton);
}
});
billingTable.getColumns().addAll(patientCol, serviceCol, amountCol, dateCol, actionCol);
billingTable.setItems(billingRecords);
}
private GridPane createInputGrid() {
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(10));
grid.addRow(0, new Label("Patient:"), patientComboBox);
grid.addRow(1, new Label("Service:"), serviceField);
grid.addRow(2, new Label("Amount:"), amountField);
grid.addRow(3, new Label("Date:"), billingDatePicker);
return grid;
}
private void addBillingRecord() {
Patient patient = patientComboBox.getValue();
String service = serviceField.getText().trim();
String amountText = amountField.getText().trim();
LocalDate date = billingDatePicker.getValue();
if (patient == null || service.isEmpty() || amountText.isEmpty() || date == null) {
showAlert(Alert.AlertType.ERROR, "Error", "Please fill in all required fields.");
return;
}
if (date.isAfter(LocalDate.now())) {
showAlert(Alert.AlertType.ERROR, "Error", "Cannot create billing records for future dates.");
return;
}