[POO] aula11 ex2 added
This commit is contained in:
parent
f371606140
commit
ec833ad896
|
@ -0,0 +1,57 @@
|
|||
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 {
|
||||
private final LinkedList<Student> students = new LinkedList<>();
|
||||
|
||||
public void load(String filename) {
|
||||
LinkedList<String> lines = new LinkedList<>();
|
||||
Path path = Paths.get(filename);
|
||||
try {
|
||||
lines = new LinkedList<>(Files.readAllLines(path));
|
||||
} catch (IOException e) {
|
||||
System.out.printf("Certifique-se que o ficheiro \"%s\" está na raiz da pasta do projeto", path);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
for (String line : lines) {
|
||||
String[] data = line.split("\\|");
|
||||
String name = data[0];
|
||||
LinkedList<Double> grades = new LinkedList<>(Arrays.stream(data).toList().subList(1, data.length).stream().map(Double::parseDouble).toList());
|
||||
students.add(new Student(name, grades));
|
||||
}
|
||||
}
|
||||
|
||||
public void addStudent(Student newStudent) {
|
||||
students.add(newStudent);
|
||||
}
|
||||
|
||||
public void removeStudent(String name) {
|
||||
students.removeIf(student -> student.getName().equals(name));
|
||||
}
|
||||
|
||||
public Student getStudent(String name) {
|
||||
return students.stream().filter(student -> student.getName().equals(name)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
public double calculateAverageGrade(String name) {
|
||||
return calculate(getStudent(name).getGrades());
|
||||
}
|
||||
|
||||
@Override
|
||||
public double calculate(LinkedList<Double> grades) {
|
||||
return MathTools.media(grades);
|
||||
}
|
||||
|
||||
public void printAllStudents() {
|
||||
for (Student student : students)
|
||||
System.out.printf("Nome: %s%nNota Final: %.2f%n%n", student.getName(), calculate(student.getGrades()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package aula11.ex2;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class GradebookTester {
|
||||
public static void main(String[] args) {
|
||||
// Create a new Gradebook instance
|
||||
Gradebook gradebook = new Gradebook();
|
||||
|
||||
// Load the student data from a text file
|
||||
gradebook.load("datafiles/aula11/ex2/alunos.txt");
|
||||
|
||||
// Add a new student to the collection
|
||||
Student newStudent = new Student("Johny May", new LinkedList<>(List.of(10.0, 15.0, 19.0)));
|
||||
gradebook.addStudent(newStudent);
|
||||
|
||||
// Remove a student from the collection
|
||||
gradebook.removeStudent("Jane Smith");
|
||||
|
||||
// Retrieve a student from the collection
|
||||
Student student = gradebook.getStudent("John Doe");
|
||||
|
||||
// Calculate the average grade for a specific student
|
||||
double averageGrade = gradebook.calculateAverageGrade("John Doe");
|
||||
System.out.println("Average grade for John Doe: " + averageGrade);
|
||||
|
||||
// Print a summary of all students in the collection
|
||||
gradebook.printAllStudents();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package aula11.ex2;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public interface IGradeCalculator {
|
||||
double calculate(LinkedList<Double> grades);
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package aula11.ex2;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Student {
|
||||
|
||||
private String name;
|
||||
private LinkedList<Double> grades;
|
||||
|
||||
public Student(String name, LinkedList<Double> grades) {
|
||||
this.name = name;
|
||||
this.grades = grades;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public LinkedList<Double> getGrades() {
|
||||
return grades;
|
||||
}
|
||||
|
||||
public void setGrades(LinkedList<Double> grades) {
|
||||
this.grades = grades;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue