-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDonor.java
More file actions
123 lines (105 loc) · 4.65 KB
/
Donor.java
File metadata and controls
123 lines (105 loc) · 4.65 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
package BloodMatch.src.bloodmatch;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
// a class, which manipulates with donors.txt file
public class Donor {
static List<String> list = new ArrayList<>();
static String line;
// method, which adds donor's information to data/requests.txt
public static void addDonor(String id, String name, String email, String city, String bloodType) {
writeDonor(id, name, email, city, bloodType);
System.out.println(name + " with id " + id + " has been" +
" written to our database successfully.");
}
// method, which find the suitable donor for request.
public static boolean matchBlood(String id, String blood, String city) {
readToArray();
// iterate through list.
for (int i = 0; i < list.size(); i++) {
// check, if line of list matches with Id.
if (list.get(i).startsWith("Id: ")) {
// get information of the donor.
if (i + 4 >= list.size())
break;
String dID = list.get(i).substring(4);
String dCity = list.get(i + 3).substring(6);
String dBlood = list.get(i + 4).substring(11);
// check, if the city and blood type of donor equals to patient's.
if (dBlood.equalsIgnoreCase(blood) && dCity.equalsIgnoreCase(city)) {
System.out.println(dID + " is suitable for " + id +
" with " + dBlood);
return true;
}
}
}
System.out.println("No matches found in " + id);
return false;
}
// method, which find the suitable donor for request.
public static String matchForId(String id, String blood, String city) {
readToArray();
// iterate through list.
for (int i = 0; i < list.size(); i++) {
// check, if line of list matches with Id.
if (list.get(i).startsWith("Id: ")) {
// get information of the donor.
if (i + 4 >= list.size())
break;
String dID = list.get(i).substring(4);
String dCity = list.get(i + 3).substring(6);
String dBlood = list.get(i + 4).substring(11);
// check, if the city and blood type of donor equals to patient's.
if (dBlood.equalsIgnoreCase(blood) && dCity.equalsIgnoreCase(city)) {
return dID;
}
}
}
return null;
}
// method, which reads the file to array.
public static void readToArray() {
list.clear();
try (BufferedReader in = new BufferedReader(new FileReader("BloodMatch\\data\\donors.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 writes information to the database.
private static void writeDonor(String id, String name, String email, String city, String bloodType) {
try (BufferedWriter out = new BufferedWriter(new FileWriter("BloodMatch\\data\\donors.txt", true))) {
if (!idExists(id)) {
out.write("Id: " + id + "\n");
out.write("Name: " + name + "\n");
out.write("Email: " + email + "\n");
out.write("City: " + city + "\n");
out.write("BloodType: " + bloodType + "\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 checks if donor's id exists.
private static boolean idExists(String id) {
try (BufferedReader in = new BufferedReader(new FileReader("BloodMatch\\data\\donors.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: ")) {
if (line.substring(4).equals(id)) {
return true;
}
}
}
} catch (IOException exc) {
System.out.println("It seems, exception has been occurred: " + exc);
}
return false;
}
}