-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatient.java
More file actions
165 lines (140 loc) · 5.85 KB
/
Patient.java
File metadata and controls
165 lines (140 loc) · 5.85 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package BloodMatch.src.bloodmatch;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
// a class, which manipulates with patients.txt file
public class Patient {
static List<String> list = new ArrayList<>();
static String line;
// method, which adds patients's information to data/requests.txt
public static void addPatient(String id, String name, String bloodType, String city, int priority) {
writeRequest(id, name, city, bloodType, priority);
System.out.println(id + " has been written to " +
"our database successfully.");
}
// method, which reads the file to array.
public static void readToArray() {
list.clear();
try (BufferedReader in = new BufferedReader(new FileReader("BloodMatch\\data\\patients.txt"))) {
while ((line = in.readLine()) != null) {
list.add(line);
}
} catch (IOException exc) {
System.out.println("It seems that exception has been ocurred: " + exc);
}
}
// method, which matches the patiens id and city.
public static boolean matchWithDonor(String id) {
// check, if patient exists in out database.
String blood = getBlood(id);
String city = getCity(id);
if ((city == null) || (blood == null)) {
System.out.println("Patient with " + id + " doesn't exist.");
return false;
}
return Donor.matchBlood(id, blood, city);
}
// method, which matches the patiens id and city.
public static String matchForId(String id) {
// check, if patient exists in out database.
String blood = getBlood(id);
String city = getCity(id);
if (!(blood == null || city == null)) {
return Donor.matchForId(id, blood, city);
}
System.out.println("Irrelevant information for blood and city.");
return null;
}
// method, which returns the city of the patient.
private static String getCity(String id) {
readToArray();
// iterate through arraylist.
for (int i = 0; i < list.size(); i++) {
if (list.get(i).startsWith("Id: ") && list.get(i).substring(4).equals(id)) {
return list.get(i + 3).substring(6);
}
}
return null;
}
// method, which returns the urgenct of the patiens.
public static int getUrgency(String id) {
readToArray();
// iterate through the list
for (int i = 0; i < list.size(); i++) {
// check, if array's id matches the patient's.
if (list.get(i).startsWith("Id: ") && list.get(i).substring(4).equals(id)) {
String prior = list.get(i + 4).substring(9);
return Integer.parseInt(prior);
}
}
return -1;
}
// method, which returns the bloodType of the patient.
private static String getBlood(String id) {
readToArray();
// iterate through the list.
for (int i = 0; i < list.size(); i++) {
// check, if array's id matches the patient's.
if (list.get(i).startsWith("Id: ") && list.get(i).substring(4).equals(id)) {
return list.get(i + 2).substring(11); // return formated bloodtype.
}
}
return null;
}
// method, which writes information to the database.
private static void writeRequest(String id, String name, String bloodType, String city, int priority) {
try (BufferedWriter out = new BufferedWriter(new FileWriter("BloodMatch\\data\\patients.txt", true))) {
if (!idExists(id)) {
out.write("Id: " + id + "\n");
out.write("Name: " + name + "\n");
out.write("BloodType: " + bloodType + "\n");
out.write("City: " + city + "\n");
out.write("Urgency: " + priority + "\n");
out.write("---\n");
return;
}
} catch (IOException exc) {
System.out.println("It seems, exception has been occurred: " + exc);
}
System.out.println(id + "already exists in our database!");
}
// method, which removes donor from the database.
public static void removePatient(String id) {
readToArray();
// iterate through list.
for (int i = 0; i < list.size(); i++) {
if (list.get(i).startsWith("Id: ") && list.get(i).substring(4).equals(id)) {
for (int j = 0; j < 6; j++) {
list.remove(i);
}
rewritePatient();
return;
}
}
}
// method, which rewrites the information of 'requests.txt'.
private static void rewritePatient() {
try (BufferedWriter out = new BufferedWriter(new FileWriter("BloodMatch\\data\\patients.txt"))) {
for (String l : list) {
out.write(l + "\n");
}
} catch (IOException exc) {
System.out.println("It seems, exception has been occurred: " + exc);
}
}
// method, which checks if donor's id exists.
private static boolean idExists(String id) {
try (BufferedReader in = new BufferedReader(new FileReader("BloodMatch\\data\\patients.txt"))) {
// read to the end of the file.
while (((line = in.readLine()) != null)) {
// start comparring id's if line starts with "Id: ".
if (line.startsWith("Id: ") && line.substring(4).equals(id)) {
return true;
}
}
} catch (IOException exc) {
System.out.println("It seems, exception has been occurred: " + exc);
}
return false;
}
}