Student Management System Project in Java

In this tutorial,  we will build a simple Student Management System project in Java. This project allows you to add students, view all students, view single student details, delete students, and search for students by their ID. We use an in-memory object to store the student objects.

Please note that this is a simplified version, and a real-world student management system would involve more complex features and database integration.

1. Defining the Student Class

This Student class represents individual students and their attributes. Each student has an id, a name, and a grade

The class also contains:

  • Constructor to initialize a student's details. 
  • Getter and Setter Methods to access and modify student attributes. 
  • toString() Method to represent the student's information in a string format.

Here is the code for the Student class:

public class Student {
    private int id;
    private String name;
    private String grade;

    public Student(int id, String name, String grade) {
        this.id = id;
        this.name = name;
        this.grade = grade;
    }

    // Getters and setters
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    @Override
    public String toString() {
        return "ID: " + id + ", Name: " + name + ", Grade: " + grade;
    }
}

2. Implementing the Student Management System 

With our Student class in place, we can now implement our CRUD and search operations:
import java.util.ArrayList;
import java.util.List;

public class StudentManagementSystem {

    private List<Student> students = new ArrayList<>();

    // Create (Add) a student
    public void addStudent(Student student) {
        students.add(student);
    }

    // Read (View) all students
    public void viewStudents() {
        students.forEach(System.out::println);
    }

    // Update a student's details
    public void updateStudent(int id, String newName, String newGrade) {
        for (Student s : students) {
            if (s.getId() == id) {
                s.setName(newName);
                s.setGrade(newGrade);
                break;
            }
        }
    }

    // Delete a student by ID
    public void deleteStudent(int id) {
        students.removeIf(s -> s.getId() == id);
    }

    // Search student by name
    public Student searchStudentByName(String name) {
        for (Student s : students) {
            if (s.getName().equalsIgnoreCase(name)) {
                return s;
            }
        }
        return null;
    }
}
This class manages a list of students and provides various operations:
  • addStudent(Student student): Adds a new student to the list.
  • viewStudents(): Displays all the students.
  • updateStudent(int id, String newName, String newGrade): Finds a student by ID and updates their name and grade.
  • deleteStudent(int id): Removes a student based on their ID.
  • searchStudentByName(String name): Searches for a student by their name and returns the first match.

3. Main Execution 

Let's see our system in action:
public class Main {
    public static void main(String[] args) {
        StudentManagementSystem sms = new StudentManagementSystem();

        // Create new students
        sms.addStudent(new Student(1, "Alice", "A"));
        sms.addStudent(new Student(2, "Bob", "B"));
        sms.addStudent(new Student(3, "Ramesh", "B+"));
        sms.addStudent(new Student(4, "Sanjay", "A-"));

        // Read and display students
        System.out.println("Students List:");
        sms.viewStudents();

        // Update student's details
        sms.updateStudent(2, "Robert", "A+");
        System.out.println("\nAfter updating Bob's details:");
        sms.viewStudents();

        // Search for a student by name
        System.out.println("\nSearching for Ramesh:");
        Student foundStudent = sms.searchStudentByName("Ramesh");
        System.out.println(foundStudent != null ? foundStudent : "Student not found.");

        // Delete a student
        sms.deleteStudent(1);
        System.out.println("\nAfter deleting Alice:");
        sms.viewStudents();
    }
}

Output:

Students List:
ID: 1, Name: Alice, Grade: A
ID: 2, Name: Bob, Grade: B
ID: 3, Name: Ramesh, Grade: B+
ID: 4, Name: Sanjay, Grade: A-

After updating Bob's details:
ID: 2, Name: Robert, Grade: A+
ID: 3, Name: Ramesh, Grade: B+
ID: 4, Name: Sanjay, Grade: A-

Searching for Ramesh:
ID: 3, Name: Ramesh, Grade: B+

After deleting Alice:
ID: 2, Name: Robert, Grade: A+
ID: 3, Name: Ramesh, Grade: B+
ID: 4, Name: Sanjay, Grade: A-

Student Creation: We add four students (Alice, Bob, Ramesh, and Sanjay) to our system. 

Displaying Students: We view the list of students. 

Updating a Student: Bob's details are updated to Robert with a grade of A+. 

Searching for a Student: We search for a student named Ramesh. 

Deleting a Student: We remove Alice from the system. 

Finally, throughout the execution, we print the results at each step to observe the actions.

Summary 

This Student Management System is a basic in-memory CRUD application for student data. It's a foundational project that can be expanded with features like persistence (using a database), validation of input, and a graphical user interface (GUI).

Related Java Projects

Comments