import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class PromedioNotas {
public static void main(String[] args) {
Map<Integer, Integer> notas = new HashMap<Integer, Integer>();
for (int i = 1; i <= 20; i++) {
Random r = new Random();
notas.put(i, r.nextInt(i));
}
// calculando promedio de notas general
Integer noteRange = getNoteRange(notas);
printRangeMessage(noteRange);
// calculando la nota para cada alumno
Map<Integer, Integer> ap = getAproved(notas);
Map<Integer, Integer> ad = getDesAproved(notas);
// obteniendo los aprovados
List<Integer> nMax = getListaOrdenada(ap.values().iterator());
pintAprovedMessage(nMax);
// obteniendo los desaprovados
List<Integer> nMin = getListaOrdenada(ad.values().iterator());
pintDesAprovedMessage(nMin);
System.out.println("FIN");
}
private static void printRangeMessage(Integer noteRange) {
String msg = String.format("Promedio de notas: %1$d", noteRange);
System.out.println(msg);
System.out.println();
}
private static void pintDesAprovedMessage(List<Integer> nMin) {
String msg = String.format("Las notas aprovadas son %1$s",
Arrays.asList(nMin));
System.out.println(msg);
msg = String.format("La mayor nota aprovada es %1$d",
nMin.get(nMin.size() - 1));
System.out.println(msg);
}
private static void pintAprovedMessage(List<Integer> nMax) {
String msg = String.format("Las notas aprovadas son %1$s",
Arrays.asList(nMax));
System.out.println(msg);
msg = String.format("La mayor nota aprovada es %1$d",
nMax.get(nMax.size() - 1));
System.out.println(msg);
}
private static Map<Integer, Integer> getAproved(Map<Integer, Integer> notas) {
Map<Integer, Integer> ap = new HashMap<Integer, Integer>();
for (int i = 1; i <= notas.size(); i++)
if (notas.get(i) >= 6)
ap.put(i, notas.get(i));
return ap;
}
private static Map<Integer, Integer> getDesAproved(
Map<Integer, Integer> notas) {
Map<Integer, Integer> ad = new HashMap<Integer, Integer>();
for (int i = 1; i <= notas.size(); i++)
if (notas.get(i) < 6)
ad.put(i, notas.get(i));
return ad;
}
private static Integer getNoteRange(Map<Integer, Integer> notas) {
Integer noteRange = 0;
for (int i = 1; i <= notas.size(); i++)
noteRange += notas.get(i);
return noteRange / notas.size();
}
private static List<Integer> getListaOrdenada(Iterator<Integer> iterator) {
List<Integer> nMax = new LinkedList<Integer>();
while (iterator.hasNext()) {
nMax.add(iterator.next());
}
Collections.sort(nMax);
return nMax;
}
}