-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathUniversity.java
More file actions
61 lines (49 loc) · 1.46 KB
/
University.java
File metadata and controls
61 lines (49 loc) · 1.46 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
import java.util.TreeSet;
class Student implements Comparable<Student> {
private int id;
private String name;
private double moadel;
public Student(int id, String name, double moadel) {
this.id = id;
this.name = name;
this.moadel = moadel;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getMoadel() {
return moadel;
}
@Override
public int compareTo(Student other) {
return Integer.compare(this.id, other.id);
}
}
public class University {
private TreeSet<Student> students;
public University() {
students = new TreeSet<>();
}
public void addStudent(int id, String name, double moadel) {
students.add(new Student(id, name, moadel));
}
public String searchStudentById(int id) {
for (Student student : students) {
if (student.getId() == id) {
return "Name: " + student.getName() + ", Moadel: " + student.getMoadel();
}
}
return "Student not found.";
}
public static void main(String[] args) {
University university = new University();
university.addStudent(1, "Hasan", 3.5);
university.addStudent(2, "Shima", 3.8);
university.addStudent(3, "Amir", 3.9);
System.out.println(university.searchStudentById(2));
System.out.println(university.searchStudentById(4));
}
}