Implementing a Basic Console Student Information Management System in Java
System Requirements
Build a console-based student information management system supporting add, delete, and query operations for student records, with the following specifications:
- Define a
Studentclass to store student data, with all attributes set to private access. Required attributes: student ID, full name, gender. - Implement core operations: add new student, delete existing student, query student by ID.
- 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:
- Add student
- Delete student
- Query student
- Exit
- When adding: prompt user to input student ID, name, gender sequetnially, wrap into a
Studentobject 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();
}
}