Practice Day: Build a Student Management System

Java Practice Project: Build a Student Management System using classes, ArrayList, and user input. Manage students with add, view, search, and remove
Practice Day: Build a Student Management System

On Day 29, we practice what we’ve learned by building a simple Student Management System. This project uses classes, ArrayLists, methods, loops, and exception handling. Users can add students, view all students, search by ID, and remove students.

1. Student Class


class Student {
    int id;
    String name;
    int age;

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

    void display() {
        System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
    }
}

2. Student Management System (Main Program)


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

public class StudentManagementSystem {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> students = new ArrayList<>();

        while (true) {
            System.out.println("\n--- Student Management System ---");
            System.out.println("1. Add Student");
            System.out.println("2. View All Students");
            System.out.println("3. Search Student by ID");
            System.out.println("4. Remove Student by ID");
            System.out.println("5. Exit");
            System.out.print("Enter your choice: ");
            
            int choice = sc.nextInt();
            sc.nextLine(); // consume newline

            switch (choice) {
                case 1:
                    System.out.print("Enter ID: ");
                    int id = sc.nextInt();
                    sc.nextLine();
                    System.out.print("Enter Name: ");
                    String name = sc.nextLine();
                    System.out.print("Enter Age: ");
                    int age = sc.nextInt();

                    students.add(new Student(id, name, age));
                    System.out.println("Student added successfully!");
                    break;

                case 2:
                    System.out.println("--- All Students ---");
                    for (Student s : students) {
                        s.display();
                    }
                    break;

                case 3:
                    System.out.print("Enter ID to search: ");
                    int searchId = sc.nextInt();
                    boolean found = false;
                    for (Student s : students) {
                        if (s.id == searchId) {
                            s.display();
                            found = true;
                        }
                    }
                    if (!found) {
                        System.out.println("Student not found!");
                    }
                    break;

                case 4:
                    System.out.print("Enter ID to remove: ");
                    int removeId = sc.nextInt();
                    students.removeIf(s -> s.id == removeId);
                    System.out.println("Student removed (if existed).");
                    break;

                case 5:
                    System.out.println("Exiting... Goodbye!");
                    sc.close();
                    return;

                default:
                    System.out.println("Invalid choice! Try again.");
            }
        }
    }
}

3. How It Works

  • We define a Student class with ID, name, and age.
  • We use an ArrayList to store student objects.
  • Users interact with the menu to add, view, search, or remove students.
  • switch handles menu selection, and loops keep the program running until exit.

4. Features

  • Add new students with details.
  • View all stored students.
  • Search students by ID.
  • Remove students by ID.

Summary

This project practices Java fundamentals: classes, objects, ArrayList, loops, conditionals, and user input. It builds a foundation for larger applications like library systems, employee managers, or inventory systems.

Post a Comment