Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing a Basic Console Student Information Management System in Java

Tech 2

System Requirements

Build a console-based student information management system supporting add, delete, and query operations for student records, with the following specifications:

  1. Define a Student class to store student data, with all attributes set to private access. Required attributes: student ID, full name, gender.
  2. Implement core operations: add new student, delete existing student, query student by ID.
  3. Store all student objects in a fixed-size array with a maximum capacity of 30.

After startup, the program displays a menu for user operation selection:

  1. Add student
  2. Delete student
  3. Query student
  4. Exit
  • When adding: prompt user to input student ID, name, gender sequetnially, wrap into a Student object and store in the array.
  • When deleting: require input of student ID, remove the matching student from the array if it exists.
  • When querying: require input of student ID, output the full student information if found.

Student Clas

package io.example.studentmanager;

public class Student {
    private int studentId;
    private String fullName;
    private String gender;

    public int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Student [ID: " + studentId + ", Name: " + fullName + ", Gender: " + gender + "]";
    }
}

Student Management Core Class

package io.example.studentmanager;

import java.util.Scanner;

public class StudentManager {
    private static Scanner input = new Scanner(System.in);
    private final int MAX_CAPACITY = 30;
    private Student[] studentRecords = new Student[MAX_CAPACITY];
    private int currentRecordCount = 0;

    public void showMainMenu() {
        mainLoop: while (true) {
            System.out.println("Please select an operation: 1. Add Student  2. Delete Student  3. Query Student  4. Exit");
            int selection = input.nextInt();
            switch (selection) {
                case 1 -> addNewStudent();
                case 2 -> deleteStudentById();
                case 3 -> queryStudentById();
                case 4 -> { break mainLoop; }
            }
        }
    }

    private void addNewStudent() {
        if (currentRecordCount == MAX_CAPACITY) {
            System.out.println("System is full, cannot add more students.");
            return;
        }

        Student newStudent = new Student();
        System.out.println("Enter student ID:");
        newStudent.setStudentId(input.nextInt());
        System.out.println("Enter student name:");
        newStudent.setFullName(input.next());
        System.out.println("Enter student gender:");
        newStudent.setGender(input.next());

        studentRecords[currentRecordCount++] = newStudent;
        System.out.println("Student added successfully.");
    }

    private void deleteStudentById() {
        System.out.println("Enter the ID of the student to delete:");
        int targetId = input.nextInt();
        int foundIndex = -1;

        for (int i = 0; i < currentRecordCount; i++) {
            if (studentRecords[i].getStudentId() == targetId) {
                foundIndex = i;
                // Shift all subsequent records forward to fill the gap
                for (int j = foundIndex; j < currentRecordCount - 1; j++) {
                    studentRecords[j] = studentRecords[j + 1];
                }
                currentRecordCount--;
                break;
            }
        }

        if (foundIndex == -1) {
            System.out.println("No student matching the provided ID was found.");
        } else {
            System.out.println("Student deleted successfully.");
        }
    }

    private void queryStudentById() {
        System.out.println("Enter the student ID to query:");
        int targetId = input.nextInt();

        // Only traverse existing records to avoid null pointer exceptions
        for (int i = 0; i < currentRecordCount; i++) {
            if (studentRecords[i].getStudentId() == targetId) {
                System.out.println(studentRecords[i]);
                return;
            }
        }

        System.out.println("No student matching the provided ID was found.");
    }
}

Program Entry Point

package io.example.studentmanager;

public class Main {
    public static void main(String[] args) {
        StudentManager manager = new StudentManager();
        manager.showMainMenu();
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.