└── student.java /student.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Student { 4 | private int id; 5 | private String name; 6 | private String department; 7 | private double marks; 8 | 9 | public Student(int id, String name, String department, double marks) { 10 | this.id = id; 11 | this.name = name; 12 | this.department = department; 13 | this.marks = marks; 14 | } 15 | 16 | public int getId() { 17 | return id; 18 | } 19 | 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | 24 | public void setDepartment(String department) { 25 | this.department = department; 26 | } 27 | 28 | public void setMarks(double marks) { 29 | this.marks = marks; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "ID: " + id + 35 | ", Name: " + name + 36 | ", Dept: " + department + 37 | ", Marks: " + marks; 38 | } 39 | } 40 | 41 | public class StudentManagementSystem { 42 | 43 | private static final ArrayList students = new ArrayList<>(); 44 | private static final Scanner sc = new Scanner(System.in); 45 | 46 | public static void main(String[] args) { 47 | 48 | while (true) { 49 | System.out.println("\n===== Student Management System ====="); 50 | System.out.println("1. Add Student"); 51 | System.out.println("2. View Students"); 52 | System.out.println("3. Search Student"); 53 | System.out.println("4. Update Student"); 54 | System.out.println("5. Delete Student"); 55 | System.out.println("6. Exit"); 56 | 57 | System.out.print("Enter choice: "); 58 | int choice = sc.nextInt(); 59 | 60 | switch (choice) { 61 | case 1 -> addStudent(); 62 | case 2 -> viewStudents(); 63 | case 3 -> searchStudent(); 64 | case 4 -> updateStudent(); 65 | case 5 -> deleteStudent(); 66 | case 6 -> { 67 | System.out.println("Exiting program..."); 68 | return; 69 | } 70 | default -> System.out.println("Invalid choice! Try again."); 71 | } 72 | } 73 | } 74 | 75 | private static void addStudent() { 76 | System.out.print("Enter ID: "); 77 | int id = sc.nextInt(); 78 | sc.nextLine(); 79 | 80 | System.out.print("Enter Name: "); 81 | String name = sc.nextLine(); 82 | 83 | System.out.print("Enter Department: "); 84 | String dept = sc.nextLine(); 85 | 86 | System.out.print("Enter Marks: "); 87 | double marks = sc.nextDouble(); 88 | 89 | students.add(new Student(id, name, dept, marks)); 90 | System.out.println("Student added successfully!"); 91 | } 92 | 93 | private static void viewStudents() { 94 | if (students.isEmpty()) { 95 | System.out.println("No students available!"); 96 | return; 97 | } 98 | 99 | System.out.println("\n--- Student List ---"); 100 | for (Student s : students) { 101 | System.out.println(s); 102 | } 103 | } 104 | 105 | private static void searchStudent() { 106 | System.out.print("Enter student ID to search: "); 107 | int id = sc.nextInt(); 108 | 109 | for (Student s : students) { 110 | if (s.getId() == id) { 111 | System.out.println("Student Found: " + s); 112 | return; 113 | } 114 | } 115 | 116 | System.out.println("Student not found!"); 117 | } 118 | 119 | private static void updateStudent() { 120 | System.out.print("Enter student ID to update: "); 121 | int id = sc.nextInt(); 122 | sc.nextLine(); 123 | 124 | for (Student s : students) { 125 | if (s.getId() == id) { 126 | System.out.print("Enter new name: "); 127 | s.setName(sc.nextLine()); 128 | 129 | System.out.print("Enter new department: "); 130 | s.setDepartment(sc.nextLine()); 131 | 132 | System.out.print("Enter new marks: "); 133 | s.setMarks(sc.nextDouble()); 134 | 135 | System.out.println("Student updated successfully!"); 136 | return; 137 | } 138 | } 139 | 140 | System.out.println("Student not found!"); 141 | } 142 | 143 | private static void deleteStudent() { 144 | System.out.print("Enter student ID to delete: "); 145 | int id = sc.nextInt(); 146 | 147 | Iterator it = students.iterator(); 148 | 149 | while (it.hasNext()) { 150 | if (it.next().getId() == id) { 151 | it.remove(); 152 | System.out.println("Student deleted successfully!"); 153 | return; 154 | } 155 | } 156 | 157 | System.out.println("Student not found!"); 158 | } 159 | } 160 | --------------------------------------------------------------------------------