- Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathDonor.java
138 lines (125 loc) · 4.83 KB
/
Donor.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
importjava.io.*;
// import java.lang.reflect.Array;
importjava.nio.file.Files;
importjava.nio.file.NoSuchFileException;
importjava.nio.file.Paths;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importorg.joda.time.LocalDate;
// import org.joda.time.DateTime;
importorg.joda.time.Period;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.Iterator;
importjava.util.Scanner;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassDonorimplementsSerializable {
Stringname, addr, contact, bldgrp;
Datedate;
publicstaticvoidmain(String[] args) throwsIOException {
// arraylist for retrieving and adding donors
ArrayList<Donor> ad = newArrayList<Donor>();
intn, i; // counter variables
// format for date and time
SimpleDateFormatft = newSimpleDateFormat("MM-dd-yyyy");
Stringtemp;
// patten for blood group
Stringpattern = "[A|B|O|AB][+|-]";
Matcherm;
Patternr = Pattern.compile(pattern);
// delete existing file first
try {
Files.deleteIfExists(Paths.get("donations.txt"));
} catch (NoSuchFileExceptione) {
System.out.println("Error: No such file/directory exists");
} catch (IOExceptione) {
System.out.println("Error: Invalid permissions.");
}
System.out.println("Success: Deletion successful.");
Scannersc = newScanner(System.in);
System.out.print("Enter number of donors: ");
n = sc.nextInt();
sc.nextLine();
Donord;
System.out.println("----------------------");
for (i = 0; i < n; i++) {
d = newDonor(); // initializing new donor
// taking input from user
System.out.print("Name: ");
d.name = sc.nextLine();
System.out.print("Address: ");
d.addr = sc.nextLine();
System.out.print("Contact: ");
d.contact = sc.nextLine();
d.bldgrp = "";
m = r.matcher(d.bldgrp);
while (!m.find()) {
System.out.print("Blood Group (only A or B or O or AB [+|-] (all caps): ");
d.bldgrp = sc.nextLine();
m = r.matcher(d.bldgrp);
}
booleanflag = false;
while (!flag) {
System.out.print("Date (MM-dd-yyyy): ");
temp = sc.nextLine();
try {
d.date = ft.parse(temp);
flag = true;
} catch (ParseExceptione) {
flag = false;
System.out.println("Unparseable using " + ft);
}
}
// adding donor object to array list
ad.add(d);
}
try {
FileOutputStreamfos = newFileOutputStream("donations.txt"); // creating file to write to
ObjectOutputStreamoos = newObjectOutputStream(fos);
// serialization
oos.writeObject(ad);
oos.close();
fos.close();
System.out.println("Success: File successfully written");
} catch (IOExceptionioe) {
ioe.printStackTrace();
}
// reading data from file
ad.clear();
FileInputStreamfis = newFileInputStream("donations.txt");
ObjectInputStreamois = newObjectInputStream(fis);
try {
ad = (ArrayList<Donor>) ois.readObject();
} catch (ClassNotFoundExceptione) {
System.out.println(e);
}
// defining current time and period
LocalDatedonor_date;
LocalDatecurrent = LocalDate.now();
Periodp;
System.out.println("---------------------------------------------");
System.out.println("---------------------------------------------");
// iterating through the arraylist
System.out.println("\n" + "Donors who haven't donated in 6 months and \"A+\"" + "\n");
Iterator<Donor> itr = ad.iterator();
i = 0;
while (itr.hasNext()) {
d = newDonor();
d = (Donor) itr.next();
donor_date = newLocalDate(d.date);
p = newPeriod(donor_date, current);
// if more than 6 months and blood group is A+, print details
if ((p.getMonths() > 6 | p.getYears() >= 1) && d.bldgrp.equals("A+")) {
System.out.println("Donor " + (i + 1));
System.out.println("----------------------");
System.out.println(d.name); // name
System.out.println(d.addr); // address
System.out.println(d.contact); // contact
System.out.println(d.bldgrp); // blood group
System.out.println(d.date); // date
System.out.println("\n");
}
}
}
}