Repository navigation
Expand file tree
/
Copy pathPatientRecord.java
More file actions
147 lines (110 loc) · 3.92 KB
/
Copy pathPatientRecord.java
File metadata and controls
147 lines (110 loc) · 3.92 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
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* PatientRecord.java
* Manages a collection of patients and their results.
*/
public class PatientRecord {
private static final String FILE_PATH = "patient_records.csv";
// Collections to store patients and their results
private final ArrayList<Patient> patients = new ArrayList<>();
private final HashMap<String, RiskResult> results = new HashMap<>();
// Check if a patient already exists based on name and age
private boolean patientExists(Patient newPatient) {
for (Patient p : patients) {
if (p.getName().equalsIgnoreCase(newPatient.getName())
&& p.getAge() == newPatient.getAge()) {
return true;
}
}
return false;
}
// Add a patient and their result
public boolean addRecord(Patient patient, RiskResult result) {
if (patientExists(patient)) {
return false;
}
patients.add(patient);
results.put("record_" + patients.size(), result);
saveToFile(patient, result);
return true;
}
// Get all patients
public List<Patient> getPatients() {
return new ArrayList<>(patients);
}
// - Get all results
public Map<String, RiskResult> getResults() {
return new HashMap<>(results);
}
// Save records to CSV file
public void saveToFile(Patient p, RiskResult r) {
File file = new File(FILE_PATH);
boolean newFile = !file.exists() || file.length() == 0;
try (PrintWriter pw = new PrintWriter(new FileWriter(file, true))) {
if (newFile) {
pw.println("RecordID,Date,PatientName,Age,Pregnancies,Glucose,BloodPressure,SkinThickness,Insulin,BMI,DiabetesPedigree,RiskScore,RiskLevel,Model");
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
int id = patients.size();
pw.printf(
"%d,%s,%s,%d,%d,%.2f,%.2f,%.2f,%.2f,%.2f,%.4f,%.1f,%s,%s%n",
id,
LocalDateTime.now().format(formatter),
p.getName(),
p.getAge(),
p.getPregnancies(),
p.getGlucose(),
p.getBloodPressure(),
p.getSkinThickness(),
p.getInsulin(),
p.getBmi(),
p.getDiabetesPedigree(),
r.getRiskScore(),
r.getRiskLevel().getLabel(),
"Logistic Risk Model"
);
} catch (IOException e) {
System.err.println("Error saving records: " + e.getMessage());
}
}
// Load records from CSV file
public void loadFromFile() {
File file = new File(FILE_PATH);
if (!file.exists()) return;
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("RecordID")) continue;
String[] parts = line.split(",");
if (parts.length >= 14) {
Patient p = new Patient(
parts[2],
Integer.parseInt(parts[3]),
Double.parseDouble(parts[5]),
Double.parseDouble(parts[6]),
Double.parseDouble(parts[9]),
Double.parseDouble(parts[8]),
Double.parseDouble(parts[7]),
Double.parseDouble(parts[10]),
Integer.parseInt(parts[4])
);
double score = Double.parseDouble(parts[11]);
patients.add(p);
results.put("record_" + patients.size(), new RiskResult(score));
}
}
} catch (IOException e) {
System.err.println("Error loading records: " + e.getMessage());
}
}
// Count of stored records
public int size() {
return patients.size();
}
}