Repository navigation
Expand file tree
/
Copy pathStudentManagement.java
More file actions
92 lines (76 loc) · 2.38 KB
/
Copy pathStudentManagement.java
File metadata and controls
92 lines (76 loc) · 2.38 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
class Student {
int id;
String name;
int age;
Set<String> courses;
public Student(int id, String name, int age, Set<String> courses) {
this.id = id;
this.name = name;
this.age = age;
this.courses = courses;
}
public String toString() {
return "Student{id=" + id + ", name='" + name + "', age=" + age + ", courses=" + courses + '}';
}
}
class StudentManager {
private HashMap<Integer, Student> students = new HashMap<>();
public void addStudent(Student student) {
students.put(student.id, student);
}
public void removeStudent(int id) {
students.remove(id);
}
public void updateStudent(int id, String name, int age, Set<String> courses) {
Student student = students.get(id);
if (student != null) {
student.name = name;
student.age = age;
student.courses = courses;
}
}
public void displayStudents() {
for (Student student : students.values()) {
System.out.println(student);
}
}
public void searchStudent(int id) {
System.out.println(students.get(id));
}
public void listStudentsByCourse(String course) {
for (Student student : students.values()) {
for (String studentCourse : student.courses) {
if (studentCourse.equals(course)) {
System.out.println(student);
break;
}
}
}
}
public static void main(String[] args) {
StudentManager m = new StudentManager();
Set<String> c1 = new HashSet<>();
c1.add("java");
c1.add("python");
Set<String> c2 = new HashSet<>();
c2.add("java");
c2.add("cs");
Set<String> c3 = new HashSet<>();
c3.add("python");
c3.add("cs");
Student s1 = new Student(1, "Salma", 20, c1);
Student s2 = new Student(2, "Gulnura", 21, c2);
Student s3 = new Student(3, "Azamzamzam", 22, c3);
m.addStudent(s1);
m.addStudent(s2);
m.addStudent(s3);
m.removeStudent(2);
m.updateStudent(1, "Salma", 17, new HashSet<>(Set.of("java", "cs")));
m.displayStudents();
m.searchStudent(1);
m.listStudentsByCourse("java");
}
}