Attendance-System-Line-By-Line-without-UI
Attendance-System-Line-By-Line-without-UI
This Java program is a simple automated attendance system. It allows users to:
Mark attendance
View attendance records
See a list of absent students
It uses file handling to store attendance records and a HashMap to manage student data
efficiently.
At the beginning of the program, the following two import statements are used:
import java.io.*;
import java.util.*;
java.io.* is imported to handle file operations, such as reading from and writing to a
file.
java.util.* is imported to use data structures like Map, Scanner, and Set.
This constant string stores the name of the file where attendance records will be saved.
It is marked final, meaning its value cannot be changed after being initialized.
The static block automatically executes once when the class is loaded.
It fills the studentData map with student IDs and names.
This data is predefined, meaning it doesn’t change when the program runs.
while (true) {
This infinite loop ensures the program keeps running until the user chooses to exit.
7. Marking Attendance
if (!studentData.containsKey(studentID)) {
System.out.println("Invalid Student ID. Please try again.");
return;
}
If the entered student ID does not exist in studentData, an error message is displayed.
String studentName = studentData.get(studentID);
String entry = studentID + " - " + studentName + " - Present\n";
If the ID is valid, the program retrieves the student’s name and creates a formatted
attendance record.
The program opens attendance.txt in append mode and writes the attendance record.
If there is an error while writing, an error message is displayed.
8. Viewing Attendance
if (file.exists()) {
try (BufferedReader reader = new BufferedReader(new
FileReader(FILE_NAME))) {
String line;
System.out.println("\n===== Attendance Records =====");
while ((line = reader.readLine()) != null) {
System.out.println(line);
String studentID = line.split(" - ")[0];
presentStudents.add(studentID);
}
} catch (IOException e) {
System.out.println("Error reading attendance records.");
}
}
The program loops through all students and prints the IDs and names of absent
students.