Mostra tutti studenti che hanno l'età minima

This commit is contained in:
La Programmatrice Verde 2025-10-17 21:49:24 +02:00
parent a15480da14
commit 33a4618a18

View File

@ -27,6 +27,7 @@ public class ereditarieta {
String nome;
Influencer influencerDaMostrare = null;
Calciatore calciatoreDaMostrare = null;
Studente studentiDaMostrare[] = null;
do {
System.out.println("Scegliere un'opzione:");
@ -107,16 +108,26 @@ public class ereditarieta {
case 5:
if (opzione1) {
if (professioneEsiste(persone, "Studente")) {
int minimo = Integer.MAX_VALUE, j = 0;
Studente studenti[] = trovaStudenti(persone);
int minimo = Integer.MAX_VALUE, j = 0;
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();
studentiDaMostrare = trovaStudentiPerEtà(persone, String.valueOf(minimo));
if (studentiDaMostrare.length == 1) {
System.out.println("Lo studente più giovane è:");
studenti[j].mostraPersona();
} else {
for (Studente studente : studentiDaMostrare) {
studente.mostraPersona();
System.out.println();
}
}
} else {
System.out
.println(
@ -225,7 +236,8 @@ public class ereditarieta {
System.out.print("Inserire l'indirizzo di studio: ");
indirizzoDiStudio = sc.nextLine();
ritorno[arrayNuovo ? i : p_persone.length + i] = new Studente(nome, cognome, sesso, età, classe, indirizzoDiStudio);
ritorno[arrayNuovo ? i : p_persone.length + i] = new Studente(nome, cognome, sesso, età,
classe, indirizzoDiStudio);
error = false;
System.out.println("Studente inserito con successo.");
break;
@ -235,7 +247,8 @@ public class ereditarieta {
System.out.print("Inserire il numero di maglia: ");
numeroMaglia = sc.nextLine();
ritorno[arrayNuovo ? i : p_persone.length + i] = new Calciatore(nome, cognome, sesso, età, squadra, numeroMaglia);
ritorno[arrayNuovo ? i : p_persone.length + i] = new Calciatore(nome, cognome, sesso, età,
squadra, numeroMaglia);
error = false;
System.out.println("Calciatore inserito con successo.");
break;
@ -245,7 +258,8 @@ public class ereditarieta {
System.out.print("Inserire l'handle: ");
handle = sc.nextLine();
ritorno[arrayNuovo ? i : p_persone.length + i] = new Influencer(nome, cognome, sesso, età, numeroFollower, handle);
ritorno[arrayNuovo ? i : p_persone.length + i] = new Influencer(nome, cognome, sesso, età,
numeroFollower, handle);
error = false;
System.out.println("Influencer inserito con successo.");
break;
@ -300,10 +314,25 @@ public class ereditarieta {
static Studente[] trovaStudenti(Persona[] p_persone) {
Studente ritorno[] = new Studente[0];
for (int i = 0; i < p_persone.length; i++) {
for (int i = 0, j = 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];
ritorno[j] = (Studente) p_persone[i];
j++;
}
}
return ritorno;
}
static Studente[] trovaStudentiPerEtà(Persona[] p_persone, String p_età) {
Studente ritorno[] = new Studente[0];
for (int i = 0, j = 0; i < p_persone.length; i++) {
if (p_persone[i].getEtà().equals(p_età)) {
ritorno = Arrays.copyOf(ritorno, ritorno.length + 1, ritorno.getClass());
ritorno[j] = (Studente) p_persone[i];
j++;
}
}