ludwig
ludwig
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import javax.swing.*;
public MarkAttendance() {
initDatabase();
initComponents();
setTitle("Attendance App");
setSize(2000, 1500);
setResizable(false);
setLocationRelativeTo(null);
setUndecorated(true);
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
showError("Failed to apply UI theme", e);
}
loadAttendanceRecords();
setVisible(true);
}
add(topPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
}
logArea.append("=============================================================
========================\n");
logArea.append(String.format("%-10s | %-20s | %-20s | %-10s | %-10s\n", "ID", "Name",
"Time", "Status", "Role"));
logArea.append("=============================================================
========================\n");
String query;
if ("All".equals(role)) {
query = """
SELECT a.id, s.name, a.date_time, a.status, 'Student' AS role
FROM stud_attendance a
JOIN students s ON a.id = s.id
WHERE DATE(a.date_time) = CURDATE()
UNION ALL
SELECT a.id, t.name, a.date_time, a.status, 'Trainer' AS role
FROM trainer_attendance a
JOIN trainers t ON a.id = t.id
WHERE DATE(a.date_time) = CURDATE()
UNION ALL
SELECT a.id, sf.name, a.date_time, a.status, 'Staff' AS role
FROM staff_attendance a
JOIN staff sf ON a.id = sf.id
WHERE DATE(a.date_time) = CURDATE()
ORDER BY a.date_time ASC;
""";
} else {
String tableName = getTableForRole(role);
String joinTable = getJoinTableForRole(role);
query = "SELECT a.id, t.name, a.date_time, a.status FROM " + tableName +
" a JOIN " + joinTable + " t ON a.id = t.id WHERE DATE(a.date_time) = CURDATE()
ORDER BY a.date_time ASC";
}
if (isValidId(id, role)) {
markAttendance();
}
}
try {
String name = fetchNameForId(id, joinTable);
if (name == null) {
logArea.append("ID not found in the " + role + " database: " + id + "\n");
return;
}
if (hasAttendanceBeenRecordedToday(id, tableName)) {
logArea.append("Attendance already recorded for ID: " + id + "\n");
} else {
insertAttendanceRecord(id, name, tableName);
}
idField.setText("");
} catch (SQLException e) {
showError("Error marking attendance", e);
}
}