In this tutorial, we will build a simple Student Management System project in Java. This project allows you to add students, view all students, view single student details, delete students, and search for students by their ID. We use an in-memory object to store the student objects.
Please note that this is a simplified version, and a real-world student management system would involve more complex features and database integration.
This Student class represents individual students and their attributes. Each student has an id, a name, and a grade.
The class also contains:
Here is the code for the Student class:
public class Student < private int id; private String name; private String grade; public Student(int id, String name, String grade) < this.id = id; this.name = name; this.grade = grade; >// Getters and setters public int getId() < return id; >public void setId(int id) < this.id = id; >public String getName() < return name; >public void setName(String name) < this.name = name; >public String getGrade() < return grade; >public void setGrade(String grade) < this.grade = grade; >@Override public String toString() < return "ID: " + id + ", Name: " + name + ", Grade: " + grade; >>
import java.util.ArrayList; import java.util.List; public class StudentManagementSystem < private Liststudents = new ArrayList<>(); // Create (Add) a student public void addStudent(Student student) < students.add(student); >// Read (View) all students public void viewStudents() < students.forEach(System.out::println); >// Update a student's details public void updateStudent(int id, String newName, String newGrade) < for (Student s : students) < if (s.getId() == id) < s.setName(newName); s.setGrade(newGrade); break; >> > // Delete a student by ID public void deleteStudent(int id) < students.removeIf(s ->s.getId() == id); > // Search student by name public Student searchStudentByName(String name) < for (Student s : students) < if (s.getName().equalsIgnoreCase(name)) < return s; >> return null; > >
This class manages a list of students and provides various operations:
public class Main < public static void main(String[] args) < StudentManagementSystem sms = new StudentManagementSystem(); // Create new students sms.addStudent(new Student(1, "Alice", "A")); sms.addStudent(new Student(2, "Bob", "B")); sms.addStudent(new Student(3, "Ramesh", "B+")); sms.addStudent(new Student(4, "Sanjay", "A-")); // Read and display students System.out.println("Students List:"); sms.viewStudents(); // Update student's details sms.updateStudent(2, "Robert", "A+"); System.out.println("\nAfter updating Bob's details:"); sms.viewStudents(); // Search for a student by name System.out.println("\nSearching for Ramesh:"); Student foundStudent = sms.searchStudentByName("Ramesh"); System.out.println(foundStudent != null ? foundStudent : "Student not found."); // Delete a student sms.deleteStudent(1); System.out.println("\nAfter deleting Alice:"); sms.viewStudents(); >>
Students List: ID: 1, Name: Alice, Grade: A ID: 2, Name: Bob, Grade: B ID: 3, Name: Ramesh, Grade: B+ ID: 4, Name: Sanjay, Grade: A- After updating Bob's details: ID: 2, Name: Robert, Grade: A+ ID: 3, Name: Ramesh, Grade: B+ ID: 4, Name: Sanjay, Grade: A- Searching for Ramesh: ID: 3, Name: Ramesh, Grade: B+ After deleting Alice: ID: 2, Name: Robert, Grade: A+ ID: 3, Name: Ramesh, Grade: B+ ID: 4, Name: Sanjay, Grade: A-
Student Creation: We add four students (Alice, Bob, Ramesh, and Sanjay) to our system.
Displaying Students: We view the list of students.
Updating a Student: Bob's details are updated to Robert with a grade of A+.
Searching for a Student: We search for a student named Ramesh.
Deleting a Student: We remove Alice from the system.
Finally, throughout the execution, we print the results at each step to observe the actions.
This Student Management System is a basic in-memory CRUD application for student data. It's a foundational project that can be expanded with features like persistence (using a database), validation of input, and a graphical user interface (GUI).