-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentService.java
More file actions
48 lines (42 loc) · 1.31 KB
/
StudentService.java
File metadata and controls
48 lines (42 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.*;
public class StudentService {
private final List<Student> students = new ArrayList<>();
public void addStudent(Student student) {
students.add(student);
System.out.println("✔ Student added successfully.");
}
public void viewAllStudents() {
if (students.isEmpty()) {
System.out.println("⚠ No student records found.");
return;
}
System.out.println("\n--- Student List ---");
for (Student student : students) {
System.out.println(student);
}
}
public Student searchStudentById(int id) {
return students.stream()
.filter(s -> s.getId() == id)
.findFirst()
.orElse(null);
}
public boolean updateStudentById(int id, String name, int age, String course) {
Student student = searchStudentById(id);
if (student != null) {
student.setName(name);
student.setAge(age);
student.setCourse(course);
return true;
}
return false;
}
public boolean deleteStudentById(int id) {
Student student = searchStudentById(id);
if (student != null) {
students.remove(student);
return true;
}
return false;
}
}