Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Building a Simple Console-Based Student Record System

Tech 1

Let’s start with core requirements and breakdown:

Core Specifications

  • Display Interface: Outputs all interactions via the console
  • Menu Options:
    --------------------------------------------------------
                   Student Record Management Console
    --------------------------------------------------------
    1. Add New Student
    2. Remove Existing Student
    3. Edit Student Details
    4. Display All Students
    5. Terminate Program
    --------------------------------------------------------
    Enter your choice: 
    
  • Student Record Structure: Unique student ID, full name, age, academic program
  • Functional Breakdown:
    • Add: Accepts user input, validates unique student ID before storing
    • Remove: Checks for valid ID; deletes record if found, shows error otherwise
    • Edit: Finds student by valid ID, updates non-ID fields
    • Display: Prints formatted list of all record, or empty message if no data exists

Implementation Steps

First, define the Student model class with private fields, getters, setters, and a no-arg constructor.

Step 1: Build the Main Loop & Menu Handler

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

public class StudentRecordApp {
    private static final Scanner INPUT = new Scanner(System.in);
    private static final ArrayList<Student> STUDENT_DB = new ArrayList<>();

    public static void main(String[] args) {
        while (true) {
            showMainMenu();
            String selection = INPUT.nextLine();
            switch (selection) {
                case "1":
                    addNewStudent();
                    break;
                case "2":
                    removeStudentById();
                    break;
                case "3":
                    modifyStudentDetails();
                    break;
                case "4":
                    printAllRecords();
                    break;
                case "5":
                    System.out.println("Program terminated successfully.");
                    INPUT.close();
                    System.exit(0);
                default:
                    System.out.println("Invalid selection. Please try again.");
            }
        }
    }

    private static void showMainMenu() {
        System.out.println("\n--------------------------------------------------------");
        System.out.println("               Student Record Management Console");
        System.out.println("--------------------------------------------------------");
        System.out.println("1. Add New Student");
        System.out.println("2. Remove Existing Student");
        System.out.println("3. Edit Student Details");
        System.out.println("4. Display All Students");
        System.out.println("5. Terminate Program");
        System.out.println("--------------------------------------------------------");
        System.out.print("Enter your choice: ");
    }
}

Step 2: Add Unique Student ID Validation & Adding Logic

private static boolean idIsDuplicate(String studentId) {
    for (Student s : STUDENT_DB) {
        if (s.getStudentId().equals(studentId)) {
            return true;
        }
    }
    return false;
}

private static void addNewStudent() {
    Student newEntry = new Student();
    String tempId;
    do {
        System.out.print("Enter unique student ID: ");
        tempId = INPUT.nextLine();
        if (idIsDuplicate(tempId)) {
            System.out.println("ID already taken. Please choose another.");
        } else {
            newEntry.setStudentId(tempId);
            break;
        }
    } while (true);

    System.out.print("Enter full name: ");
    newEntry.setFullName(INPUT.nextLine());

    System.out.print("Enter age: ");
    int tempAge = Integer.parseInt(INPUT.nextLine());
    newEntry.setAge(tempAge);

    System.out.print("Enter academic program: ");
    newEntry.setProgram(INPUT.nextLine());

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

Step 3: Delete by Validated Student ID

private static int findStudentIndex(String studentId) {
    for (int i = 0; i < STUDENT_DB.size(); i++) {
        if (STUDENT_DB.get(i).getStudentId().equals(studentId)) {
            return i;
        }
    }
    return -1;
}

private static void removeStudentById() {
    System.out.print("Enter ID of student to remove: ");
    String targetId = INPUT.nextLine();
    int position = findStudentIndex(targetId);
    if (position != -1) {
        STUDENT_DB.remove(position);
        System.out.println("Student with ID " + targetId + " removed successfully!");
    } else {
        System.out.println("No student found with ID " + targetId + ".");
    }
}

Step 4: Modify Non-ID Student Fields

private static void modifyStudentDetails() {
    System.out.print("Enter ID of student to edit: ");
    String targetId = INPUT.nextLine();
    int position = findStudentIndex(targetId);
    if (position == -1) {
        System.out.println("No student found with ID " + targetId + ".");
        return;
    }

    Student target = STUDENT_DB.get(position);

    System.out.print("Enter new full name (leave empty to keep current): ");
    String newName = INPUT.nextLine();
    if (!newName.isBlank()) {
        target.setFullName(newName);
    }

    System.out.print("Enter new age (leave empty to keep current): ");
    String ageInput = INPUT.nextLine();
    if (!ageInput.isBlank()) {
        target.setAge(Integer.parseInt(ageInput));
    }

    System.out.print("Enter new academic program (leave empty to keep current): ");
    String newProgram = INPUT.nextLine();
    if (!newProgram.isBlank()) {
        target.setProgram(newProgram);
    }

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

Step 5: Formatted Record Display

private static void printAllRecords() {
    if (STUDENT_DB.isEmpty()) {
        System.out.println("No student records are currently stored.");
        return;
    }

    System.out.printf("\n%-10s %-20s %-5s %-15s%n", "ID", "Full Name", "Age", "Program");
    System.out.println("------------------------------------------------------------");
    for (Student s : STUDENT_DB) {
        System.out.printf("%-10s %-20s %-5d %-15s%n", 
            s.getStudentId(), s.getFullName(), s.getAge(), s.getProgram());
    }
}

Finally, add the Student class:

class Student {
    private String studentId;
    private String fullName;
    private int age;
    private String program;

    public Student() {}

    public String getStudentId() { return studentId; }
    public void setStudentId(String studentId) { this.studentId = studentId; }
    public String getFullName() { return fullName; }
    public void setFullName(String fullName) { this.fullName = fullName; }
    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }
    public String getProgram() { return program; }
    public void setProgram(String program) { this.program = program; }
}

Related Articles

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...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

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