Java Project - Student Management System

In this post, we'll build a basic Student Management System using Java. This console application will allow users to perform CRUD operations (Create, Read, Update, and Delete) for managing student records.

Structure: 

A Student class to represent individual student details. 

A StudentManagementSystem class to manage multiple students. 

Main class to provide the user interface.

Source Code:

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

class Student {
    private int rollNo;
    private String name;
    private double marks;

    // Constructor
    public Student(int rollNo, String name, double marks) {
        this.rollNo = rollNo;
        this.name = name;
        this.marks = marks;
    }

    // Getters and Setters
    public int getRollNo() {
        return rollNo;
    }

    public String getName() {
        return name;
    }

    public double getMarks() {
        return marks;
    }

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

    public void setMarks(double marks) {
        this.marks = marks;
    }

    @Override
    public String toString() {
        return "Roll No: " + rollNo + ", Name: " + name + ", Marks: " + marks;
    }
}

class StudentManagementSystem {
    private ArrayList<Student> students = new ArrayList<>();

    // Method to add student
    public void addStudent(int rollNo, String name, double marks) {
        students.add(new Student(rollNo, name, marks));
    }

    // Method to display all students
    public void displayStudents() {
        for (Student student : students) {
            System.out.println(student);
        }
    }

    // Method to update a student's details
    public void updateStudent(int rollNo, String newName, double newMarks) {
        for (Student student : students) {
            if (student.getRollNo() == rollNo) {
                student.setName(newName);
                student.setMarks(newMarks);
                return;
            }
        }
        System.out.println("Student not found!");
    }

    // Method to delete a student
    public void deleteStudent(int rollNo) {
        Iterator<Student> it = students.iterator();
        while (it.hasNext()) {
            if (it.next().getRollNo() == rollNo) {
                it.remove();
                return;
            }
        }
        System.out.println("Student not found!");
    }
}

public class Main {
    public static void main(String[] args) {
        StudentManagementSystem sms = new StudentManagementSystem();
        Scanner scanner = new Scanner(System.in);
        int choice;

        do {
            System.out.println("\n1. Add Student");
            System.out.println("2. Display Students");
            System.out.println("3. Update Student");
            System.out.println("4. Delete Student");
            System.out.println("5. Exit");
            System.out.print("Enter your choice: ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.print("Enter Roll Number: ");
                    int rollNo = scanner.nextInt();

                    scanner.nextLine();  // Consume newline

                    System.out.print("Enter Name: ");
                    String name = scanner.nextLine();

                    System.out.print("Enter Marks: ");
                    double marks = scanner.nextDouble();

                    sms.addStudent(rollNo, name, marks);
                    break;

                case 2:
                    sms.displayStudents();
                    break;

                case 3:
                    System.out.print("Enter Roll Number to Update: ");
                    rollNo = scanner.nextInt();
                    scanner.nextLine();  // Consume newline

                    System.out.print("Enter New Name: ");
                    name = scanner.nextLine();

                    System.out.print("Enter New Marks: ");
                    marks = scanner.nextDouble();

                    sms.updateStudent(rollNo, name, marks);
                    break;

                case 4:
                    System.out.print("Enter Roll Number to Delete: ");
                    rollNo = scanner.nextInt();
                    sms.deleteStudent(rollNo);
                    break;
            }
        } while (choice != 5);
    }
}

Explanation: 

We've structured our code into classes for modularity and clarity. 

The Student class represents an individual student. It contains private fields for the roll number, name, and marks, along with the necessary getters and setters. 

The toString method provides a convenient way to print student details. 

The StudentManagementSystem class maintains a list of students. It provides methods to add, display, update, and delete students. 

The Main class provides a console-based interface for users to interact with the system. 

Sample Output:

1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 1
Enter Roll Number: 101
Enter Name: Alice
Enter Marks: 85.5

1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 2
Roll No: 101, Name: Alice, Marks: 85.5

... and so on ...

With this basic structure in place, you can easily expand and add more features, like search functionality or input validation. Enjoy coding!


Comments