Skip to content

Commit e9eff36

Browse files
author
Semen Darienko
committed
Add comparatod for sort Students by average score
1 parent 82ba59e commit e9eff36

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Students/src/Main.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import groupStudents.Group;
22
import student.Student;
3+
import student.StudentComparatorByName;
34

45
public class Main {
56

@@ -42,7 +43,16 @@ public static void main(String[] args) {
4243
groupA.addAll(groupB);
4344

4445
/* Alphabetical sorting */
45-
groupA.sort();
46+
groupA.sort(new StudentComparatorByName());
47+
48+
/* Sorting by best ball*/
49+
groupA.sort((o1, o2) -> {
50+
double averageScore1 = o1.getAverageScore();
51+
double averageScore2 = o2.getAverageScore();
52+
if (averageScore1 > averageScore1) return 1;
53+
else if (averageScore1 == averageScore2) return 0;
54+
else return -1;
55+
});
4656

4757
/* Check if group contains in other group */
4858
System.out.println("Group " + groupB.getName() + (groupA.containsAll(groupB) ? " is contains" : " not contains") + " in group " + groupA.getName());

Students/src/groupStudents/Group.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import student.StudentComparatorByName;
55

66
import java.util.ArrayList;
7+
import java.util.Comparator;
78
import java.util.function.Predicate;
89

910
public class Group extends ArrayList<Student> {

Students/src/student/Student.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ public Student(String surename){
2121
this.surename = surename;
2222
this.subjects = Subject.init();
2323
}
24+
25+
public double getAverageScore() {
26+
double result = 0;
27+
for (Subject s : subjects) {
28+
result += s.getAverageBall();
29+
}
30+
return result / subjects.size();
31+
}
2432
}

0 commit comments

Comments
 (0)