0% found this document useful (0 votes)
9 views5 pages

Attendance-System-Line-By-Line-without-UI

The Java Attendance System is an automated program that allows users to mark attendance, view attendance records, and see absent students using file handling and a HashMap for data management. It includes a main method for user interaction, where users can choose options to mark attendance or view records. The program handles user input and displays attendance statistics, including total present and absent students.

Uploaded by

jbd20152
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Attendance-System-Line-By-Line-without-UI

The Java Attendance System is an automated program that allows users to mark attendance, view attendance records, and see absent students using file handling and a HashMap for data management. It includes a main method for user interaction, where users can choose options to mark attendance or view records. The program handles user input and displays attendance statistics, including total present and absent students.

Uploaded by

jbd20152
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Understanding the Java Attendance System

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.

1. Importing Required Libraries

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.

2. Declaring the Class

public class Attendance {

 The program defines a public class named Attendance.


 Since it's public, it can be accessed from anywhere in the program.

3. Defining Constants and Data Storage


private static final String FILE_NAME = "attendance.txt";

 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.

private static final Map<String, String> studentData = new HashMap<>();


 This creates a HashMap where:
o The keys are student IDs (String).
o The values are student names (String).
 This is marked static final, meaning it belongs to the class and cannot be
reassigned.

4. Initializing Student Data in a Static Block


static {
studentData.put("1001", "John Doe");
studentData.put("1002", "Jane Smith");
// ... More student entries ...
}

 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.

5. The Main Method – Program Entry Point


public static void main(String[] args) {

 The program starts execution from the main method.

Scanner scanner = new Scanner(System.in);

 A Scanner object is created to read user input.

while (true) {

 This infinite loop ensures the program keeps running until the user chooses to exit.

System.out.println("\n===== Automated Attendance System =====");


System.out.println("1. Mark Attendance");
System.out.println("2. View Attendance");
System.out.println("3. Exit");
System.out.print("Choose an option: ");

 This menu presents the user with three options:


1. Mark Attendance
2. View Attendance
3. Exit

int choice = scanner.nextInt();


scanner.nextLine(); // Consume newline
 The program reads the user’s choice and removes the extra newline character to avoid
input issues.

6. Handling User Input with Switch-Case


switch (choice) {
case 1:
markAttendance(scanner);
break;
case 2:
viewAttendance();
break;
case 3:
System.out.println("Exiting... Goodbye!");
return;
default:
System.out.println("Invalid choice! Please try again.");
}

 The program checks the user’s input:


o If the user enters 1, the program marks attendance.
o If the user enters 2, it displays attendance records.
o If the user enters 3, it exits.
o If the user enters anything else, it shows an error message.

7. Marking Attendance

The method markAttendance() is called when the user chooses option 1.

private static void markAttendance(Scanner scanner) {

 This method is responsible for recording attendance.

System.out.print("Enter Student ID: ");


String studentID = scanner.nextLine();

 The user is prompted to enter a student ID.

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.

try (FileWriter writer = new FileWriter(FILE_NAME, true)) {


writer.write(entry);
System.out.println("Attendance marked successfully for " + studentName +
"!");
} catch (IOException e) {
System.out.println("Error recording attendance.");
}

 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

The method viewAttendance() is called when the user chooses option 2.

private static void viewAttendance() {

 This method displays the attendance records.

File file = new File(FILE_NAME);


Set<String> presentStudents = new HashSet<>();

 A File object is created to check if attendance.txt exists.


 A HashSet is used to store the IDs of present students.

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.");
}
}

 If the file exists, the program:


o Reads each line from attendance.txt.
o Prints the attendance records.
o Extracts student IDs of present students.

int totalPresent = presentStudents.size();


int totalAbsent = studentData.size() - totalPresent;

 The total number of present and absent students is calculated.

System.out.println("\nTotal Present: " + totalPresent);


System.out.println("Total Absent: " + totalAbsent);

 These values are displayed to the user.

System.out.println("\n===== Absent Students =====");


for (Map.Entry<String, String> entry : studentData.entrySet()) {
if (!presentStudents.contains(entry.getKey())) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
}

 The program loops through all students and prints the IDs and names of absent
students.

You might also like