0% found this document useful (0 votes)
3 views6 pages

With UI

The document outlines the implementation of an Attendance System in Java, detailing the importation of necessary libraries, class definitions, data structures, and methods for marking attendance and viewing reports. It includes a constructor for initializing student data, methods to mark attendance based on student IDs, and a user interface for interacting with the system. The main method provides a menu for users to mark attendance, view reports, or exit the application.

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)
3 views6 pages

With UI

The document outlines the implementation of an Attendance System in Java, detailing the importation of necessary libraries, class definitions, data structures, and methods for marking attendance and viewing reports. It includes a constructor for initializing student data, methods to mark attendance based on student IDs, and a user interface for interacting with the system. The main method provides a menu for users to mark attendance, view reports, or exit the application.

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/ 6

1.

Importing Required Libraries

import java.time.LocalDate;
import java.util.*;

 import java.time.LocalDate; → Allows the program to get the current


date.
 import java.util.*; → Imports ’s utility classes such as HashMap,
HashSet, ArrayList, and Scanner.

2. Defining the Class and Data Structures

public class AttendanceSystem {

 Defines a class named AttendanceSystem, which contains all the methods


for handling attendance.

private static final Map<Integer, String> studentRecords = new


HashMap<>();

 Declares a HashMap (studentRecords) that stores student IDs as keys


(Integer) and names as values (String).

private static final Map<Integer, List<String>> attendance = new


HashMap<>();

 Declares another HashMap (attendance), where each student ID is mapped


to a list of dates (strings) on which they attended class.

private static final Set<String> classDates = new HashSet<>();

 Declares a HashSet (classDates) that stores all unique dates when


attendance is taken.
3. Constructor to Initialize Student Data

public AttendanceSystem() {
initializeStudents();
}

 When an AttendanceSystem object is created, the constructor automatically


calls initializeStudents() to populate student data.

4. Initializing Student Records

private void initializeStudents() {


int[] ids = {46032, 2323, 123456, 234567, 345678, 456789, 567890,
678901, 789012, 890123, 901234, 123789};
String[] names = {"Shalom", "Gian", "JB", "Richard", "Ian",
"Aron", "Patrick", "Rommel", "Kevs", "Ralp", "CJ", "Fred"};

 Creates two arrays:


o ids → Stores predefined student IDs.
o names → Stores student names corresponding to those IDs.

for (int i = 0; i < names.length; i++) {


studentRecords.put(ids[i], names[i]);
}
}

 Loops through the names array.


 Inserts each student ID-name pair into studentRecords using .put().

5. Marking Attendance

public void markAttendance(int studentId) {


 Defines a method that marks a student as present based on their ID.

try {
if (!studentRecords.containsKey(studentId)) throw new
NumberFormatException();

 Checks if the given studentId exists in studentRecords.


 If the ID is not found, it throws an exception.

String date = LocalDate.now().toString();

 Retrieves the current date in YYYY-MM-DD format.

if (attendance.containsKey(studentId) &&
attendance.get(studentId).contains(date)) {
System.out.println("Attendance already marked for
today.");
return;
}

 Checks if the student has already been marked present today.


 If yes, prints a message and exits the method.

classDates.add(date);

 Stores today’s date in classDates to track class attendance days.

attendance.computeIfAbsent(studentId, k -> new


ArrayList<>()).add(date);

 computeIfAbsent(studentId, k -> new ArrayList<>()) → If no record


exists for this student, it creates a new list.
 .add(date) → Adds today’s date to the list of attendance records for that
student.
System.out.println("Marked Present: " +
studentRecords.get(studentId) + " (ID: " + studentId + ") on " +
date);

 Prints a confirmation message including the student’s name, ID, and


attendance date.

} catch (NumberFormatException e) {
System.out.println("Invalid Student ID.");
}
}

 If the student ID does not exist, it catches the exception and prints an error
message.

6. Viewing Attendance Report

public void viewReport() {


StringBuilder report = new StringBuilder();

 Defines viewReport(), which generates a summary of student attendance


using StringBuilder for efficient string manipulation.

studentRecords.forEach((id, name) -> {

 Loops through all student records using forEach().

List<String> presentDates = attendance.getOrDefault(id, new


ArrayList<>());

 Retrieves the list of dates the student was present.


 If the student has no attendance, it returns an empty list instead.

int absences = classDates.size() - presentDates.size();


 Calculates absences by subtracting the number of days attended from the
total recorded class days.

report.append("Student: ").append(name).append(" (ID:


").append(id).append(")\n")
.append("Present on: ").append(presentDates).append("\
n")
.append("Number of Absences:
").append(absences).append("\n\n");

 Formats and appends the attendance details for each student.

});
System.out.println(report.toString());
}

 Prints the final attendance report to the console.

7. Main Method (User Interface and Menu System)

public static void main(String[] args) {


AttendanceSystem system = new AttendanceSystem();
Scanner scanner = new Scanner(System.in);

 Creates an AttendanceSystem object to manage attendance.


 Creates a Scanner to read user input.

while (true) {
System.out.println("1. Mark Attendance");
System.out.println("2. View Report");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();

 Starts an infinite loop displaying the menu.


 Reads the user’s input (choice).
switch (choice) {
case 1:
System.out.print("Enter Student ID: ");
int studentId = scanner.nextInt();
system.markAttendance(studentId);
break;

 Case 1: Prompts the user to enter a student ID and calls markAttendance().

case 2:
system.viewReport();
break;

 Case 2: Calls viewReport() to display the attendance summary.

case 3:
System.out.println("Exiting...");
scanner.close();
return;

 Case 3: Exits the program and closes the scanner.

default:
System.out.println("Invalid choice. Please try
again.");

 Handles invalid inputs by displaying an error message.

You might also like