-
-
Save lucasDRR/28df7990eecc832a744be1c3cd97f236 to your computer and use it in GitHub Desktop.
StackOverflow respuesta, agregar el package correspondiente
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package stackHelp; | |
public class Alumno implements Comparable<Alumno>{ | |
private int idAlumno; | |
private String nombre; | |
public Alumno(int idAlumno, String nombre) { | |
super(); | |
this.idAlumno = idAlumno; | |
this.nombre = nombre; | |
} | |
public int getIdAlumno() { | |
return idAlumno; | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public void setNombre(String nombre) { | |
this.nombre = nombre; | |
} | |
@Override | |
public String toString() { | |
return idAlumno+" "+nombre; | |
} | |
@Override | |
public int compareTo(Alumno o) { | |
return Integer.compare(idAlumno, o.getIdAlumno()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package stackHelp; | |
import java.util.ArrayList; | |
import java.util.Comparator; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
public class Entrada { | |
public static void main(String[] args) { | |
List<Alumno> alumnos = new ArrayList<>(); | |
List<Modulo> modulos = new ArrayList<>(); | |
List<Nota> notas = new ArrayList<>(); | |
// Agregando mudulos y alumnos | |
modulos.add(new Modulo(1,"Biología")); | |
modulos.add(new Modulo(2,"Física")); | |
modulos.add(new Modulo(3,"Química")); | |
modulos.add(new Modulo(4,"Matemática")); | |
alumnos.add(new Alumno(1,"Maria")); | |
alumnos.add(new Alumno(2,"Mario")); | |
alumnos.add(new Alumno(3,"Lorena")); | |
alumnos.add(new Alumno(4,"Mariano")); | |
alumnos.add(new Alumno(5,"Carlos")); | |
alumnos.add(new Alumno(6,"Natalia")); | |
// agregar notas notas | |
// idAlumno, idModulo, nota | |
notas.add(new Nota(3,2,5)); | |
notas.add(new Nota(3,4,6)); | |
notas.add(new Nota(4,1,10)); | |
notas.add(new Nota(2,1,8)); | |
notas.add(new Nota(2,3,9)); | |
notas.add(new Nota(3,1,7)); | |
notas.add(new Nota(5,4,8)); | |
notas.add(new Nota(6,2,7)); | |
notas.add(new Nota(1,1,8)); | |
notas.add(new Nota(1,2,7)); | |
notas.add(new Nota(1,3,6)); | |
Comparator<Nota> byModule = new Comparator<>() { | |
@Override | |
public int compare(Nota o1, Nota o2) { | |
int dif = Integer.compare(o1.getIdModulo(), o2.getIdModulo()); | |
if(dif != 0) { | |
return dif; | |
}else { | |
return Integer.compare(o1.getIdAlumno(), o2.getIdAlumno()); | |
} | |
} | |
}; | |
notas.sort(byModule); | |
/* // VERSION CON STREAM | |
Map<Integer, String> mapaModulos ; | |
Map<Integer, String> mapaAlumnos ; | |
mapaAlumnos = alumnos.stream() | |
.collect(Collectors | |
.toMap(Alumno::getIdAlumno, Alumno::getNombre)); | |
mapaModulos = modulos.stream() | |
.collect(Collectors | |
.toMap(Modulo::getIdModulo, Modulo::getNombreModulo)); | |
for (int i = 0 ; i < notas.size() ; i++) { | |
Nota nota = notas.get(i); | |
System.out.println("MODULO = "+mapaModulos.get(nota.getIdModulo()) +" | ALUMNO = "+ mapaAlumnos.get( nota.getIdAlumno())); | |
} | |
*/ | |
/* VERSION ITERATIVA */ | |
// ordenando los arrays solo para la versión Iterativa | |
alumnos.sort(null); | |
modulos.sort(null); | |
int indexNotas = 0; // indice para recorrer el array de notas | |
int indexAlumno = 0; // indice para recorrer el array de alumnos | |
for(int modIndex = 0; modIndex < modulos.size(); modIndex++) { | |
Modulo temp = modulos.get(modIndex); | |
while(indexNotas < notas.size() && notas.get(indexNotas).getIdModulo() == temp.getIdModulo()) { | |
// si idAlumno == idAlumno indicado en Nota muestro el mensaje | |
if(alumnos.get(indexAlumno).getIdAlumno() == notas.get(indexNotas).getIdAlumno()) { | |
System.out.println("MODULO = "+temp.getNombreModulo()+" | NOMBRE ALUMNO = " | |
+alumnos.get(indexAlumno).getNombre()+" | NOTA = "+notas.get(indexNotas).getNota()); | |
// aumento el indice de notas | |
indexNotas++; | |
} | |
// aumento el indice de alumnos | |
indexAlumno++; | |
} | |
// reinicio el indice de alumnos para buscar en el siguiete modulo | |
indexAlumno = 0; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package stackHelp; | |
public class Modulo implements Comparable<Modulo>{ | |
private int idModulo; | |
private String nombreModulo; | |
public Modulo(int idModulo, String nombreModulo) { | |
super(); | |
this.idModulo = idModulo; | |
this.nombreModulo = nombreModulo; | |
} | |
public String getNombreModulo() { | |
return nombreModulo; | |
} | |
public void setNombreModulo(String nombreModulo) { | |
this.nombreModulo = nombreModulo; | |
} | |
public int getIdModulo() { | |
return idModulo; | |
} | |
@Override | |
public String toString() { | |
return "Modulo [ " + idModulo + ", " + nombreModulo + "]"; | |
} | |
@Override | |
public int compareTo(Modulo o) { | |
return Integer.compare(this.idModulo, o.getIdModulo() ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package stackHelp; | |
public class Nota { | |
private int idAlumno; | |
private int idModulo; | |
private int nota; | |
public Nota(int idAlumno, int idModulo, int nota) { | |
super(); | |
this.idAlumno = idAlumno; | |
this.idModulo = idModulo; | |
this.nota = nota; | |
} | |
public int getIdAlumno() { | |
return idAlumno; | |
} | |
public void setIdAlumno(int idAlumno) { | |
this.idAlumno = idAlumno; | |
} | |
public int getIdModulo() { | |
return idModulo; | |
} | |
public void setIdModulo(int idModulo) { | |
this.idModulo = idModulo; | |
} | |
public int getNota() { | |
return nota; | |
} | |
public void setNota(int nota) { | |
this.nota = nota; | |
} | |
@Override | |
public String toString() { | |
return "Nota(" + idAlumno + ", " + idModulo + ")"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment