In this project, we will create a simple Student Management System using Java. This project helps you understand classes, objects, constructors, ArrayList, loops, methods, and conditional statements.
| Option | Function |
|---|---|
| 1 | Add a new student |
| 2 | Display all students |
| 3 | Search for a student by ID |
| 4 | Delete a student by ID |
| 5 | Exit the application |
Create a Java file named StudentProject.java.
We will write two classes in the same file:
StudentProject.java because
the public class is named StudentProject.
Copy the following code into StudentProject.java.
import java.util.ArrayList;
import java.util.Scanner;
class Student {
int id;
String name;
int age;
String course;
Student(int id, String name, int age, String course) {
this.id = id;
this.name = name;
this.age = age;
this.course = course;
}
void display() {
System.out.println("----------------------------");
System.out.println("Student ID : " + id);
System.out.println("Name : " + name);
System.out.println("Age : " + age);
System.out.println("Course : " + course);
}
}
public class StudentProject {
static ArrayList<Student> students = new ArrayList<>();
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int choice;
do {
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");
System.out.println("4. Delete Student");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = readInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
viewStudents();
break;
case 3:
searchStudent();
break;
case 4:
deleteStudent();
break;
case 5:
System.out.println("Thank you for using the system.");
break;
default:
System.out.println("Invalid choice. Try again.");
}
} while (choice != 5);
sc.close();
}
static int readInt() {
while (!sc.hasNextInt()) {
System.out.print("Please enter a valid number: ");
sc.next();
}
int number = sc.nextInt();
sc.nextLine();
return number;
}
static void addStudent() {
System.out.print("Enter Student ID: ");
int id = readInt();
if (findStudent(id) != null) {
System.out.println("This Student ID already exists.");
return;
}
System.out.print("Enter Student Name: ");
String name = sc.nextLine().trim();
System.out.print("Enter Student Age: ");
int age = readInt();
System.out.print("Enter Course Name: ");
String course = sc.nextLine().trim();
if (name.isEmpty() || course.isEmpty() || age <= 0) {
System.out.println("Please enter valid student details.");
return;
}
Student s = new Student(id, name, age, course);
students.add(s);
System.out.println("Student added successfully!");
}
static void viewStudents() {
if (students.isEmpty()) {
System.out.println("No student records found.");
return;
}
System.out.println("\n===== STUDENT RECORDS =====");
for (Student s : students) {
s.display();
}
}
static void searchStudent() {
System.out.print("Enter Student ID to search: ");
int id = readInt();
Student s = findStudent(id);
if (s != null) {
System.out.println("Student found:");
s.display();
} else {
System.out.println("Student not found.");
}
}
static void deleteStudent() {
System.out.print("Enter Student ID to delete: ");
int id = readInt();
Student s = findStudent(id);
if (s != null) {
students.remove(s);
System.out.println("Student deleted successfully!");
} else {
System.out.println("Student not found.");
}
}
static Student findStudent(int id) {
for (Student s : students) {
if (s.id == id) {
return s;
}
}
return null;
}
}
The Student class represents one student.
It stores four pieces of information.
class Student {
int id;
String name;
int age;
String course;
}
| Variable | Purpose |
|---|---|
| id | Stores the student's ID. |
| name | Stores the student's name. |
| age | Stores the student's age. |
| course | Stores the student's course. |
Student(int id, String name, int age, String course) {
this.id = id;
this.name = name;
this.age = age;
this.course = course;
}
A constructor initializes an object when it is created.
The this keyword refers to the current object.
Student s = new Student(101, "Rahul", 20, "Java");
This statement creates a student with ID 101.
static ArrayList<Student> students = new ArrayList<>();
An ArrayList stores multiple Student objects. Unlike a fixed-size array, it can grow or shrink as records are added or removed.
students.add(s) adds a student.students.isEmpty() checks whether it is empty.students.remove(s) removes a student.The addStudent() method follows these steps:
Student s = new Student(id, name, age, course);
students.add(s);
for (Student s : students) {
s.display();
}
The enhanced for loop visits each Student object in the ArrayList and displays its information.
static Student findStudent(int id) {
for (Student s : students) {
if (s.id == id) {
return s;
}
}
return null;
}
This method compares the entered ID with each stored
student's ID. It returns the matching Student object or
null if no student is found.
Student s = findStudent(id);
if (s != null) {
students.remove(s);
System.out.println("Student deleted successfully!");
}
First, the program searches for the student. If the record exists, it removes that object from the ArrayList.
Follow these steps to run the program.
javac StudentProject.java
java StudentProject
===== STUDENT MANAGEMENT SYSTEM =====
1. Add Student
2. View All Students
3. Search Student
4. Delete Student
5. Exit
Enter your choice: 1
Enter Student ID: 101
Enter Student Name: Rahul Kumar
Enter Student Age: 20
Enter Course Name: Java
Student added successfully!
===== STUDENT MANAGEMENT SYSTEM =====
1. Add Student
2. View All Students
3. Search Student
4. Delete Student
5. Exit
Enter your choice: 2
===== STUDENT RECORDS =====
----------------------------
Student ID : 101
Name : Rahul Kumar
Age : 20
Course : Java
The example shows adding and displaying one student. The program continues showing the menu until you select Exit.
Complete these tasks to improve your programming skills.
Choose the correct answer for each question.