Resolvi o último exercício utilizando map, achei que ficaria mais simples assim. Na tentativa com listas observei que deveria retornar as listas, mas aparentemente a passagem de parâmetros é feita por valor;
import 'dart:io';
void main() {
Map<String, List<double>> alunos = <String, List<double>>{};
int opcao = menu();
while(opcao != 3) {
switch(opcao) {
case 1:
alunos = registraAlunosENotas(alunos);
case 2:
listarAlunosNotasEMedias(alunos);
}
opcao = menu();
}
print("Finalizando o programa!!");
print("Até logo");
}
Map<String, List<double>> registraAlunosENotas(Map<String, List<double>> alunos) {
List<double> notas = <double>[];
String? entrada;
print("Digite o nome do aluno");
String? nome = stdin.readLineSync();
if(nome == null || nome.isEmpty) {
print("O nome não pode ser vazio!");
registraAlunosENotas(alunos);
}
do {
print("Digite uma nota para o aluno (ou \"fim\" para terminar)");
entrada = stdin.readLineSync();
if(entrada != null || entrada!.isNotEmpty) {
if(entrada.toLowerCase() != "fim"){
double nota = double.parse(entrada);
notas.add(nota);
}
} else {
print("Entrada não pode ser vazia!");
}
}while(entrada != "fim");
alunos[nome!] = notas;
return alunos;
}
double calculaMedia(List<double> notas) {
double somatorio = 0;
for(double nota in notas) {
somatorio += nota;
}
return somatorio / notas.length;
}
void listarAlunosNotasEMedias(Map<String, List<double>> alunos) {
alunos.entries.forEach((aluno) {
print("");
print("=============================");
print("Aluno: ${aluno.key}");
print("Notas: ${aluno.value}");
print("Media: ${calculaMedia(aluno.value).toStringAsFixed(2)}");
print("=============================");
print("");
});
}
int menu() {
List<String> opcoes = <String>["1", "2", "3"];
print("");
cabecalho();
print("");
print("+-------------------------+");
print("| 1........Cadastrar Aluno|");
print("| 2..........Listar Alunos|");
print("| 3...................Sair|");
print("+-------------------------+");
print("");
print("Selecione uma opção");
String? entrada = stdin.readLineSync();
if(entrada == null || entrada.isEmpty) {
print("Entrada não pode ser vazia!");
menu();
}
if(!opcoes.contains(entrada)) {
print("Opção não disponível escolher uma opção válida");
menu();
}
return int.parse(entrada!);
}
void cabecalho() {
print( " _ _ _ ");
print( " | \\ | | | | ");
print( " | \\| | ___ | |_ __ _ ___ ");
print( " | . \\` |/ _ \\| __/ _\\` / __|");
print( " | |\\ | (_) | || (_| \\__ \\");
print( " |_| \\_|\\___/ \\__\\__,_|___/");
print( " ");
print( " ");
}