[POO] aula11: implemented SimpleGradeCalculator

This commit is contained in:
TiagoRG 2023-05-12 21:06:42 +01:00
parent bdb5fffaec
commit f85ff5ee93
2 changed files with 17 additions and 10 deletions

View File

@ -1,15 +1,14 @@
package aula11.ex2; package aula11.ex2;
import utils.MathTools;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.*; import java.util.*;
public class Gradebook implements IGradeCalculator { public class Gradebook {
private final LinkedList<Student> students = new LinkedList<>(); private final LinkedList<Student> students = new LinkedList<>();
private final IGradeCalculator gradeCalculator = new SimpleGradeCalculator();
public void load(String filename) { public void load(String filename) {
LinkedList<String> lines = new LinkedList<>(); LinkedList<String> lines = new LinkedList<>();
@ -42,16 +41,11 @@ public class Gradebook implements IGradeCalculator {
} }
public double calculateAverageGrade(String name) { public double calculateAverageGrade(String name) {
return calculate(getStudent(name).getGrades()); return gradeCalculator.calculate(getStudent(name).getGrades());
}
@Override
public double calculate(LinkedList<Double> grades) {
return MathTools.media(grades);
} }
public void printAllStudents() { public void printAllStudents() {
for (Student student : students) 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();
}
}