Tengo que realizar la siguiente actividad:
7.- showDifferentSubjectsOrderedAlphabetically()
Debe mostrar las asignaturas de las que hay algún alumno matriculado, ordenadas alfabéticamente. La salida sería: Asignaturas: LM PROGR
Para ello, he tenido que crear previamente diferentes clases, estas:
package classes;
import java.util.List;
public class Student {
private final long id;
private final String name;
private byte age;
private String group;
private int grant; // Beca.
private List<Grade> grades; // Lista de notas.
public Student(long id, String name, byte age, String group, int grant, Grade... grades) {
this.id = id;
this.name = name;
this.age = age;
this.group = group;
this.grant = grant;
this.grades = List.of(grades);
}
/*
Getters and Setters
*/
public long getID() {
return id;
}
public String getName() {
return name;
}
public byte getAge() {
return age;
}
public void setAge(byte age) {
this.age = age;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public int getGrant() {
return grant;
}
public void setGrant(int grant) {
this.grant = grant;
}
public List<Grade> getGrades() {
return grades;
}
public void setGrades(List<Grade> grades) {
this.grades = grades;
}
} // end class
ackage classes;
import java.util.Objects;
public class Grade {
private final String subject;
private float mark;
public Grade(String subject, float mark) {
this.subject = subject;
this.mark = mark;
}
public String getSubject() {
return subject;
}
public float getMark() {
return mark;
}
public void setMark(float mark) {
this.mark = mark;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Grade grade = (Grade) o;
return subject.equals(grade.subject);
}
@Override
public int hashCode() {
return Objects.hash(subject);
}
} // end class
package classes;
import java.util.ArrayList;
import java.util.List;
public class Database {
private List<Student> students = new ArrayList<>();
public Database() {
// ong id, String name, byte age, String group, int grant, Grade... grades
students.add(new Student(1, "Germán Ginés", (byte) 23, "1o CFGS DAM", 2000, new Grade("PROGR", 8), new Grade("LM", 3)));
students.add(new Student(2, "Baldomero", (byte) 21, "1o CFGS DAM", 0, new Grade("PROGR", 5), new Grade("LM", 4)));
students.add(new Student(3, "Ana Guerra", (byte) 17, "1o CFGS SMR", 4000, new Grade("PROGR", 8)));
}
public List<Student> queryAllStudents() {
return students;
}
}
He intentado:
public String[] showDifferentSubjectsOrderedAlphabetically() {
return database.queryAllStudents().stream()
.map(Student::getGrades)
}
Y
public String[] showDifferentSubjectsOrderedAlphabetically() {
return database.queryAllStudents().stream()
.flatMap(Student::getGrades)
}
En la primera me devuelve un tipo super raro: Stream> y en la segunda me devuelve una lista de object... En cualquier de los dos casos, no puedo seguir.
¿Alguien me puede echar una mano?
Muchas gracias.