-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentManager.java
More file actions
65 lines (52 loc) · 1.56 KB
/
StudentManager.java
File metadata and controls
65 lines (52 loc) · 1.56 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Business Logic Layer
import java.util.ArrayList;
public class StudentManager {
private ArrayList<Student> students;
// Constructor
public StudentManager(){
students = new ArrayList<>();
}
// CRUD operations
public void addStudent(Student student){
students.add(student);
System.out.println("Student added successfully");
}
public void viewStudents(){
if(students.isEmpty()){
System.out.println("No Students found");
return;
}
for(Student student: students){
System.out.println(student);
}
}
// Linear Search O(n)
public Student searchStudentById(int id){
for (Student student : students){
if(student.getId() == id){
return student;
}
}
return null;
}
public void deleteStudent(int id){
Student student = searchStudentById(id);
if (student == null){
System.out.println("Student Not Found");
return;
}
students.remove(student); // O(n)
System.out.println("Student deleted successfully");
}
public void updateStudent(int id, String newName, int newAge, String newCourse){
Student student = searchStudentById(id);
if(student == null){
System.out.println("Student not found");
return;
}
student.setName(newName);
student.setAge(newAge);
student.setCourse(newCourse);
System.out.println("Student updated successfully");
}
}