Day 9 - Java

Problem Statement: Attendance Tracker and Personalized Greeter

You are required to develop a Java program for an attendance system that not only tracks the attendance of students but also provides personalized greetings to those present. The program should read input from the user, including the number of students, their names, and their attendance status. For each student marked present, a randomly selected greeting message should be displayed along with their name. Finally, the program should output the total attendance count.

Instructions:

Implement a Java program named AttendanceSystem with the following components:

  • A static variable totalAttendance to keep track of the overall attendance count.
  • A static block to initialize the totalAttendance variable to 0 when the class is loaded.
  • A static inner class Student with attributes name (String) and present (boolean) to represent student information.
  • A constructor in the Student class to initialize the name and set present to false by default.
  • A method markPresent() in the Student class to mark a student as present.
  • Getter methods getName() and isPresent() in the Student class.
  • A static method markAttendanceAndGreet() in the AttendanceSystem class to mark attendance and greet the student with a random message if present.
  • A static method getTotalAttendance() in the AttendanceSystem class to retrieve the total attendance count.
  • A main() method to handle user input, create student objects, mark attendance, greet students, and display the total attendance count.

Input Format:

The first line of input should contain an integer 'n', denoting the number of students.

The second line should contain 'n' comma-separated strings denoting the names of the students.

The third line should contain 'n' integers separated by spaces, representing the attendance status of each student (1 for present, 0 for absent).

Output Format:

For each present student, print a greeting message followed by their name.

Sample Greetings:

1. Shine bright like a Sun!

2.Be colorful like a Rainbow!

3. Embrace the day with positivity!

Print "Total attendance: " followed by the total attendance count.

Ensure proper error handling for invalid inputs.

Sample Input:

5

Dhoni, Raina, Rutu, Jaddu, Faf

1 0 1 1 0

Sample Output:

Welcome, Dhoni! Shine bright like a Sun!

Welcome, Rutu! Be colorful like a Rainbow!

Welcome, Jaddu! Embrace the day with positivity!

Total attendance: 3

Constraints:

1 <= n <= 100 (number of students)

Names consist only of alphabetical characters and spaces.

Each attendance status is either 0 or 1.


SOLUTION:

import java.util.*;


public class AttendanceSystem {

    private static int totalAttendance;


    static {

        totalAttendance = 0;

    }


    static class Student {

        String name;

        boolean present;


        public Student(String name) {

            this.name = name;

            this.present = false; 

        }


        public void markPresent() {

            this.present = true;

        }


        public String getName() {

            return name;

        }


        public boolean isPresent() {

            return present;

        }

    }


    public static void markAttendanceAndGreet(Student s) {

        if (s.isPresent()) {

            totalAttendance++;

            String[] greetings = {"Shine bright like a Sun!", "Be colorful like a Rainbow!", "Embrace the day with positivity!"};

            Random random = new Random();

            int index = random.nextInt(greetings.length);

            System.out.println("Welcome, " + s.getName() + "! " + greetings[index]);

        }

    }


    public static int getTotalAttendance() {

        return totalAttendance;

    }


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);


        int n = sc.nextInt();

        sc.nextLine(); 


        String names = sc.nextLine();

        String[] stNames = names.split(",");


        Student[] s = new Student[n];

        for (int i = 0; i < n; i++) {

            s[i] = new Student(stNames[i].trim());

        }


        for (Student i : s) {

            int status = sc.nextInt();

            if (status == 1) {

                i.markPresent();

            }

            markAttendanceAndGreet(i);

        }


     System.out.println("Total attendance: " + getTotalAttendance());

    }

}

Insights:
  • The static block initializes the totalAttendance variable to 0 when the class is loaded. Static blocks are executed only once when the class is loaded into memory.
  • The Student class is encapsulated within the AttendanceSystem class as a static inner class. This provides a clear and logical grouping of related classes.
  • The totalAttendance variable is declared as private to restrict direct access from outside the class. It is static, ensuring that all instances of the class share the same variable.
  • The markPresent() method in the Student class allows marking a student as present by setting the present attribute to true.
  • The markAttendanceAndGreet() method selects a random greeting message from an array and displays it along with the student's name if they are marked present.
  • The random message feature adds variety and personalization to the attendance process. It creates a unique and positive experience for each student marked present.
  • The getTotalAttendance() method returns the total attendance count, which is incremented each time a student is marked present.
Happy Coding! 
All the best for all Students for your upcoming exams! 
Let's welcome April- mostly the month of Exams with our static program!
For the people who have completed the school and college, cherish your days of saying Present in your class and those who are in the learning phase, enjoy the Golden Period - the school and college days! :)

Comments

Post a Comment