Student Management System Using Code Java


Let’s create a comprehensive Java project for class 10 & 12 students that covers core Java concepts and features. We’ll develop a simple “Student Management System” which can manage student records, including adding, viewing, and deleting student information. This project will include:

  1. Requirement Gathering and Feature Discussion
  2. Design Discussion
  3. Code Implementation
  4. Testing Plan


Part 1: Requirement Gathering and Feature Discussion


Requirements:

  1. Add Student: Ability to add new student records with details such as ID, name, grade, and age.
  2. View Students: Ability to view all student records.
  3. Delete Student: Ability to delete a student record by ID.
  4. Search Student: Ability to search for a student by ID or name.


Part 2: Design Discussion


Design Components:

  1. Student Class: Represents a student entity.
  2. StudentDatabase Class: Manages the student records.
  3. StudentManagementSystem Class: Main class to interact with the user.


UML Diagram:


+----------------------+
|      Student         |
+----------------------+
| - id: int            |
| - name: String       |
| - grade: String      |
| - age: int           |
+----------------------+
| + getId(): int       |
| + getName(): String  |
| + getGrade(): String |
| + getAge(): int      |
| + setId(id: int)     |
| + setName(name: String)|
| + setGrade(grade: String)|
| + setAge(age: int)   |
+----------------------+

+----------------------+
|  StudentDatabase     |
+----------------------+
| - students: List<Student>|
+----------------------+
| + addStudent(student: Student)|
| + viewStudents(): List<Student>|
| + deleteStudent(id: int): boolean|
| + searchStudentById(id: int): Student|
| + searchStudentByName(name: String): List<Student>|
+----------------------+

+----------------------------+
| StudentManagementSystem    |
+----------------------------+
| - main(args: String[]): void|
+----------------------------+
| + displayMenu(): void       |
| + handleUserInput(): void   |
+----------------------------+


Part 3: Code Implementation


1. Student Class

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

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

    // 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;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", grade=" + grade + ", age=" + age + "]";
    }
}

2. StudentDatabase Class

import java.util.ArrayList;
import java.util.List;

public class StudentDatabase {
    private List<Student> students = new ArrayList<>();

    public void addStudent(Student student) {
        students.add(student);
    }

    public List<Student> viewStudents() {
        return new ArrayList<>(students);
    }

    public boolean deleteStudent(int id) {
        return students.removeIf(student -> student.getId() == id);
    }

    public Student searchStudentById(int id) {
        for (Student student : students) {
            if (student.getId() == id) {
                return student;
            }
        }
        return null;
    }

    public List<Student> searchStudentByName(String name) {
        List<Student> result = new ArrayList<>();
        for (Student student : students) {
            if (student.getName().equalsIgnoreCase(name)) {
                result.add(student);
            }
        }
        return result;
    }
}

3. StudentManagementSystem Class

import java.util.List;
import java.util.Scanner;

public class StudentManagementSystem {
    private static StudentDatabase studentDatabase = new StudentDatabase();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            displayMenu();
            handleUserInput();
        }
    }

    private static void displayMenu() {
        System.out.println("\nStudent Management System");
        System.out.println("1. Add Student");
        System.out.println("2. View Students");
        System.out.println("3. Delete Student");
        System.out.println("4. Search Student by ID");
        System.out.println("5. Search Student by Name");
        System.out.println("6. Exit");
        System.out.print("Enter your choice: ");
    }

    private static void handleUserInput() {
        int choice = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        switch (choice) {
            case 1:
                addStudent();
                break;
            case 2:
                viewStudents();
                break;
            case 3:
                deleteStudent();
                break;
            case 4:
                searchStudentById();
                break;
            case 5:
                searchStudentByName();
                break;
            case 6:
                System.exit(0);
                break;
            default:
                System.out.println("Invalid choice. Please try again.");
        }
    }

    private static void addStudent() {
        System.out.print("Enter Student ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // Consume newline
        System.out.print("Enter Student Name: ");
        String name = scanner.nextLine();
        System.out.print("Enter Student Grade: ");
        String grade = scanner.nextLine();
        System.out.print("Enter Student Age: ");
        int age = scanner.nextInt();

        Student student = new Student(id, name, grade, age);
        studentDatabase.addStudent(student);
        System.out.println("Student added successfully.");
    }

    private static void viewStudents() {
        List<Student> students = studentDatabase.viewStudents();
        if (students.isEmpty()) {
            System.out.println("No students found.");
        } else {
            for (Student student : students) {
                System.out.println(student);
            }
        }
    }

    private static void deleteStudent() {
        System.out.print("Enter Student ID to delete: ");
        int id = scanner.nextInt();
        boolean isDeleted = studentDatabase.deleteStudent(id);
        if (isDeleted) {
            System.out.println("Student deleted successfully.");
        } else {
            System.out.println("Student not found.");
        }
    }

    private static void searchStudentById() {
        System.out.print("Enter Student ID to search: ");
        int id = scanner.nextInt();
        Student student = studentDatabase.searchStudentById(id);
        if (student != null) {
            System.out.println(student);
        } else {
            System.out.println("Student not found.");
        }
    }

    private static void searchStudentByName() {
        System.out.print("Enter Student Name to search: ");
        String name = scanner.nextLine();
        List<Student> students = studentDatabase.searchStudentByName(name);
        if (students.isEmpty()) {
            System.out.println("Student not found.");
        } else {
            for (Student student : students) {
                System.out.println(student);
            }
        }
    }
}


Part 4: Testing Plan


  1. Manual Testing:

    • Run the program and manually test each feature (add, view, delete, search by ID, search by name).
    • Verify that the system behaves as expected and handles edge cases (e.g., deleting a non-existent student, searching for a non-existent student).

  2. Unit Testing:

    • Write unit tests for the StudentDatabase class to ensure each method works correctly.
    • Use a testing framework like JUnit for structured testing.


Example Unit Test


import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class StudentDatabaseTest {
    private StudentDatabase studentDatabase;

    @BeforeEach
    public void setUp() {
        studentDatabase = new StudentDatabase();
    }

    @Test
    public void testAddAndViewStudent() {
        Student student = new Student(1, "Alice", "10", 15);
        studentDatabase.addStudent(student);
        assertEquals(1, studentDatabase.viewStudents().size());
        assertEquals("Alice", studentDatabase.viewStudents().get(0).getName());
    }

    @Test
    public void testDeleteStudent() {
        Student student = new Student(1, "Bob", "12", 17);
        studentDatabase.addStudent(student);
        assertTrue(studentDatabase.deleteStudent(1));
        assertFalse(studentDatabase.deleteStudent(2));
    }

    @Test
    public void testSearchStudentById() {
        Student student = new Student(1, "Charlie", "11", 16);
        studentDatabase.addStudent(student);
        assertNotNull(studentDatabase.searchStudentById(1));
        assertNull(studentDatabase.searchStudentById(2));
    }

    @Test
    public void testSearchStudentByName() {
        Student student1 = new Student(1, "David", "10", 15);
        Student student2 = new Student(2, "David", "11", 16);
        studentDatabase.addStudent(student1);
        studentDatabase.addStudent(student2);
        assertEquals(2, studentDatabase.searchStudentByName("David").size());
    }
}


Conclusion


This project provides a comprehensive introduction to core Java concepts, including object-oriented programming, collections, and basic I/O operations. By following these steps, students can gain hands-on experience with Java programming and develop a functional student management system. This project can be further extended with more features, such as updating student records, storing data persistently using files or databases, and creating a graphical user interface (GUI).


Leave a Reply

Your email address will not be published. Required fields are marked *