Java Student Management Project
100%

Java Student Management System Project

In this project, we will create a simple Student Management System using Java. This project helps you understand classes, objects, constructors, ArrayList, loops, methods, and conditional statements.

Project Level: Beginner
Technology: Core Java
Storage: ArrayList (temporary memory)
Features: Add, view, search, and delete students
1. Project Objectives
After completing this project, you will learn how to:
  • Create a Java class and objects.
  • Use constructors to initialize student details.
  • Store multiple students in an ArrayList.
  • Use Scanner to accept input from users.
  • Search for a student using a student ID.
  • Delete a student record.
  • Build a menu-driven console application.
2. Project Features
Option Function
1 Add a new student
2 Display all students
3 Search for a student by ID
4 Delete a student by ID
5 Exit the application
3. Create the Project File

Create a Java file named StudentProject.java.

We will write two classes in the same file:

  • Student: Stores student information.
  • StudentProject: Runs the application.
Save the file as StudentProject.java because the public class is named StudentProject.
4. Complete Java Source Code

Copy the following code into StudentProject.java.

import java.util.ArrayList;
import java.util.Scanner;

class Student {
    int id;
    String name;
    int age;
    String course;

    Student(int id, String name, int age, String course) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.course = course;
    }

    void display() {
        System.out.println("----------------------------");
        System.out.println("Student ID : " + id);
        System.out.println("Name       : " + name);
        System.out.println("Age        : " + age);
        System.out.println("Course     : " + course);
    }
}

public class StudentProject {

    static ArrayList<Student> students = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        int choice;

        do {
            System.out.println("\n===== STUDENT MANAGEMENT SYSTEM =====");
            System.out.println("1. Add Student");
            System.out.println("2. View All Students");
            System.out.println("3. Search Student");
            System.out.println("4. Delete Student");
            System.out.println("5. Exit");
            System.out.print("Enter your choice: ");

            choice = readInt();

            switch (choice) {
                case 1:
                    addStudent();
                    break;

                case 2:
                    viewStudents();
                    break;

                case 3:
                    searchStudent();
                    break;

                case 4:
                    deleteStudent();
                    break;

                case 5:
                    System.out.println("Thank you for using the system.");
                    break;

                default:
                    System.out.println("Invalid choice. Try again.");
            }

        } while (choice != 5);

        sc.close();
    }

    static int readInt() {
        while (!sc.hasNextInt()) {
            System.out.print("Please enter a valid number: ");
            sc.next();
        }
        int number = sc.nextInt();
        sc.nextLine();
        return number;
    }

    static void addStudent() {

        System.out.print("Enter Student ID: ");
        int id = readInt();

        if (findStudent(id) != null) {
            System.out.println("This Student ID already exists.");
            return;
        }

        System.out.print("Enter Student Name: ");
        String name = sc.nextLine().trim();

        System.out.print("Enter Student Age: ");
        int age = readInt();

        System.out.print("Enter Course Name: ");
        String course = sc.nextLine().trim();

        if (name.isEmpty() || course.isEmpty() || age <= 0) {
            System.out.println("Please enter valid student details.");
            return;
        }

        Student s = new Student(id, name, age, course);
        students.add(s);

        System.out.println("Student added successfully!");
    }

    static void viewStudents() {

        if (students.isEmpty()) {
            System.out.println("No student records found.");
            return;
        }

        System.out.println("\n===== STUDENT RECORDS =====");

        for (Student s : students) {
            s.display();
        }
    }

    static void searchStudent() {

        System.out.print("Enter Student ID to search: ");
        int id = readInt();

        Student s = findStudent(id);

        if (s != null) {
            System.out.println("Student found:");
            s.display();
        } else {
            System.out.println("Student not found.");
        }
    }

    static void deleteStudent() {

        System.out.print("Enter Student ID to delete: ");
        int id = readInt();

        Student s = findStudent(id);

        if (s != null) {
            students.remove(s);
            System.out.println("Student deleted successfully!");
        } else {
            System.out.println("Student not found.");
        }
    }

    static Student findStudent(int id) {

        for (Student s : students) {
            if (s.id == id) {
                return s;
            }
        }

        return null;
    }
}
5. Understanding the Student Class

The Student class represents one student. It stores four pieces of information.

class Student {
    int id;
    String name;
    int age;
    String course;
}
Variable Purpose
id Stores the student's ID.
name Stores the student's name.
age Stores the student's age.
course Stores the student's course.
6. Understanding the Constructor
Student(int id, String name, int age, String course) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.course = course;
}

A constructor initializes an object when it is created. The this keyword refers to the current object.

Student s = new Student(101, "Rahul", 20, "Java");

This statement creates a student with ID 101.

7. Understanding ArrayList
static ArrayList<Student> students = new ArrayList<>();

An ArrayList stores multiple Student objects. Unlike a fixed-size array, it can grow or shrink as records are added or removed.

  • students.add(s) adds a student.
  • students.isEmpty() checks whether it is empty.
  • students.remove(s) removes a student.
This project stores records only in memory. Student records disappear when the program closes. A database or file is needed for permanent storage.
8. How to Add a Student

The addStudent() method follows these steps:

  1. Accept the student ID.
  2. Check whether the ID already exists.
  3. Accept the name, age, and course.
  4. Create a Student object.
  5. Add the object to the ArrayList.
Student s = new Student(id, name, age, course);
students.add(s);
9. How to View All Students
for (Student s : students) {
    s.display();
}

The enhanced for loop visits each Student object in the ArrayList and displays its information.

10. How to Search a Student
static Student findStudent(int id) {
    for (Student s : students) {
        if (s.id == id) {
            return s;
        }
    }
    return null;
}

This method compares the entered ID with each stored student's ID. It returns the matching Student object or null if no student is found.

11. How to Delete a Student
Student s = findStudent(id);

if (s != null) {
    students.remove(s);
    System.out.println("Student deleted successfully!");
}

First, the program searches for the student. If the record exists, it removes that object from the ArrayList.

12. How to Run the Project

Follow these steps to run the program.

  1. Install the Java Development Kit (JDK).
  2. Open a code editor or terminal.
  3. Save the code as StudentProject.java.
  4. Open the terminal in the file's directory.
  5. Compile and run the program.
javac StudentProject.java
java StudentProject
Make sure the Java compiler is installed and available through your system's PATH.
13. Sample Output
===== STUDENT MANAGEMENT SYSTEM =====
1. Add Student
2. View All Students
3. Search Student
4. Delete Student
5. Exit
Enter your choice: 1

Enter Student ID: 101
Enter Student Name: Rahul Kumar
Enter Student Age: 20
Enter Course Name: Java
Student added successfully!

===== STUDENT MANAGEMENT SYSTEM =====
1. Add Student
2. View All Students
3. Search Student
4. Delete Student
5. Exit
Enter your choice: 2

===== STUDENT RECORDS =====
----------------------------
Student ID : 101
Name       : Rahul Kumar
Age        : 20
Course     : Java

The example shows adding and displaying one student. The program continues showing the menu until you select Exit.

14. Practice Assignments

Complete these tasks to improve your programming skills.

  1. Add a new field for the student's email address.
  2. Display the total number of students.
  3. Search for students by name.
  4. Add an option to update student details.
  5. Prevent negative student IDs.
  6. Sort students by their names.
  7. Save student records to a file so they persist after closing the program.
15. Important Points to Remember
  • A class defines the structure of an object.
  • A constructor initializes an object.
  • An ArrayList stores multiple objects.
  • A method performs a particular task.
  • A loop can process every student record.
  • Conditional statements help make decisions.
  • ArrayList data is temporary unless saved externally.
Congratulations! You have learned the structure of a basic Java Student Management System.

Quick Quiz: Student Management Project

Choose the correct answer for each question.

1. Which collection stores multiple Student objects in this project?



2. What does the constructor do?



3. Where are student records stored in this project?