-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVPrinter.java
More file actions
94 lines (74 loc) · 3.08 KB
/
Copy pathCSVPrinter.java
File metadata and controls
94 lines (74 loc) · 3.08 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
/*
Made for Bellevue College RISE Makerspace by Evan Johnson
Project Description:
Digital sign-in system. New users input their first name, last name, student ID number,
and college email. When a user is in the system already, all they need to sign in is
their student ID number, greatly shortening sign-in time.
Class Description: contains 2 public static functions that generate .csv files containing
student information.
*/
package src;
import java.io.File;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.format.DateTimeFormatter;
public class CSVPrinter
{
// create a .csv file that contains a record of every sign-in performed, including all student data
public static boolean printAll(ResultSet RS)
{
boolean successful = true;
java.time.LocalDateTime now = java.time.LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDT = now.format(formatter);
try (PrintWriter printWriter = new PrintWriter(new File((formattedDT + " total attendance.csv")), "UTF-8"))
{
printWriter.write(buildString(RS));
} catch (Exception e)
{
System.out.println("problem in CSVPrinter.printAll() ");
System.err.println(e.getClass().getName() + ": " + e.getMessage());
successful = false;
}
return successful;
}
// create a .csv file that contains a record of every sign-in performed within a range, (given a ResultSet of data within that range)
public static boolean printTimeframe(String startDate, String endDate, ResultSet RS)
{
boolean successful = true;
try (PrintWriter printWriter = new PrintWriter(new File(("attendance from " + startDate.substring(10) + " to " + endDate.substring(10) + ".csv")), "UTF-8"))
{
printWriter.write(buildString(RS));
} catch (Exception e)
{
System.out.println("problem in CSVPrinter.printTimeframe() ");
System.err.println(e.getClass().getName() + ": " + e.getMessage());
successful = false;
}
return successful;
}
// build a .csv formatted string to fill the .csv file with
private static String buildString(ResultSet RS)
{
StringBuilder sb = new StringBuilder();
sb.append("SID,First Name,Last Name,Email,Timestamp\n"); // first line of CSV
try
{
while (RS.next())
{
sb.append("" + RS.getInt("SID") + ",");
sb.append(RS.getString("FNAME") + ",");
sb.append(RS.getString("LNAME") + ",");
sb.append(RS.getString("EMAIL") + ",");
sb.append(RS.getString("timestamp") + "\n");
}
} catch (SQLException e)
{
System.out.println("problem in CSVPrinter.buildString() ");
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
return sb.toString();
}
}