Mostra studente più giovane

This commit is contained in:
La Programmatrice Verde 2025-10-17 21:05:29 +02:00
parent 5bac8d0f44
commit cb8c67abfb
2 changed files with 39 additions and 1 deletions

View File

@ -32,4 +32,8 @@ public class Persona {
public String getNome() { public String getNome() {
return this.nome; return this.nome;
} }
public String getEtà() {
return this.età;
}
} }

View File

@ -105,7 +105,28 @@ public class ereditarieta {
Pausa(); Pausa();
break; break;
case 5: case 5:
if (opzione1) {
if (ProfessioneEsiste(persone, "Studente")) {
int minimo = Integer.MAX_VALUE, j = 0;
Studente studenti[] = TrovaStudenti(persone);
for (int i = 0; i < studenti.length; i++) {
if (Integer.parseInt(studenti[i].getEtà()) < minimo) {
minimo = Integer.parseInt(studenti[i].getEtà());
j = i;
}
}
System.out.println("Lo studente più giovane è:");
studenti[j].MostraPersona();
} else {
System.out
.println(
"Errore: è necessario aggiungere almeno uno studente prima di mostrare la sua età.");
}
} else {
System.out
.println(
"Errore: è necessario aggiungere almeno una persona prima di mostrare informazioni su di esse.");
}
Pausa(); Pausa();
break; break;
default: default:
@ -273,4 +294,17 @@ public class ereditarieta {
return ritorno; return ritorno;
} }
static Studente[] TrovaStudenti(Persona[] p_persone) {
Studente ritorno[] = new Studente[0];
for (int i = 0; i < p_persone.length; i++) {
if (p_persone[i].getClass().getSimpleName().equals("Studente")) {
ritorno = Arrays.copyOf(ritorno, ritorno.length + 1, ritorno.getClass());
ritorno[i] = (Studente) p_persone[i];
}
}
return ritorno;
}
} }