-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentService.java
More file actions
49 lines (43 loc) · 1.31 KB
/
Copy pathStudentService.java
File metadata and controls
49 lines (43 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
49
import java.util.ArrayList;
public class StudentService {
ArrayList<Student> students = new ArrayList<>();
public void addStudent(Student s) {
students.add(s);
System.out.println("Student Added Successfully!");
}
public void viewStudents() {
if (students.isEmpty()) {
System.out.println("No Students Available!");
return;
}
for (Student s : students) {
System.out.println(s);
}
}
public Student searchStudent(int id) {
for (Student s : students) {
if (s.getId() == id) return s;
}
return null;
}
public void updateStudent(int id, String newName, int newAge, String newCourse) {
Student s = searchStudent(id);
if (s != null) {
s.setName(newName);
s.setAge(newAge);
s.setCourse(newCourse);
System.out.println("Student Updated Successfully!");
} else {
System.out.println("Student Not Found!");
}
}
public void deleteStudent(int id) {
Student s = searchStudent(id);
if (s != null) {
students.remove(s);
System.out.println("Student Deleted Successfully!");
} else {
System.out.println("Student Not Found!");
}
}
}