[POO] aula11: implemented SimpleGradeCalculator

This commit is contained in:
TiagoRG 2023-05-12 21:06:42 +01:00
parent f71c1b4f9f
commit 4f3c1a9f6d
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
2 changed files with 17 additions and 10 deletions

View File

@ -1,15 +1,14 @@
package aula11.ex2;
import utils.MathTools;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
public class Gradebook implements IGradeCalculator {
public class Gradebook {
private final LinkedList<Student> students = new LinkedList<>();
private final IGradeCalculator gradeCalculator = new SimpleGradeCalculator();
public void load(String filename) {
LinkedList<String> lines = new LinkedList<>();
@ -42,16 +41,11 @@ public class Gradebook implements IGradeCalculator {
}
public double calculateAverageGrade(String name) {
return calculate(getStudent(name).getGrades());
}
@Override
public double calculate(LinkedList<Double> grades) {
return MathTools.media(grades);
return gradeCalculator.calculate(getStudent(name).getGrades());
}
public void printAllStudents() {
for (Student student : students)
System.out.printf("Nome: %s%nNota Final: %.2f%n%n", student.getName(), calculate(student.getGrades()));
System.out.printf("Nome: %s%nNota Final: %.2f%n%n", student.getName(), gradeCalculator.calculate(student.getGrades()));
}
}

View File

@ -0,0 +1,13 @@
package aula11.ex2;
import java.util.LinkedList;
public class SimpleGradeCalculator implements IGradeCalculator {
@Override
public double calculate(LinkedList<Double> grades) {
double sum = 0;
for (double n : grades)
sum += n;
return sum / grades.size();
}
}