Controllo età + rimoszione controllo eccezione inutile

This commit is contained in:
La Programmatrice Verde 2025-10-17 21:56:14 +02:00
parent 33a4618a18
commit 8221c2e0f3

View File

@ -109,8 +109,8 @@ public class ereditarieta {
if (opzione1) { if (opzione1) {
if (professioneEsiste(persone, "Studente")) { if (professioneEsiste(persone, "Studente")) {
Studente studenti[] = trovaStudenti(persone); Studente studenti[] = trovaStudenti(persone);
int minimo = Integer.MAX_VALUE, j = 0; int minimo = Integer.MAX_VALUE, j = 0;
for (int i = 0; i < studenti.length; i++) { for (int i = 0; i < studenti.length; i++) {
if (Integer.parseInt(studenti[i].getEtà()) < minimo) { if (Integer.parseInt(studenti[i].getEtà()) < minimo) {
minimo = Integer.parseInt(studenti[i].getEtà()); minimo = Integer.parseInt(studenti[i].getEtà());
@ -164,7 +164,7 @@ public class ereditarieta {
String nome; String nome;
String cognome; String cognome;
String sesso; String sesso;
String età; String età = "";
String professione; String professione;
String classe; String classe;
String indirizzoDiStudio; String indirizzoDiStudio;
@ -210,8 +210,25 @@ public class ereditarieta {
System.out.print("Inserire il sesso: "); System.out.print("Inserire il sesso: ");
sesso = sc.nextLine(); sesso = sc.nextLine();
System.out.print("Inserire l'età: "); do {
età = sc.nextLine(); error = true;
int etàInt;
System.out.print("Inserire l'età: ");
try {
etàInt = sc.nextInt();
sc.nextLine();
if (etàInt <= 0) {
System.out.println("Errore: l'età deve essere maggiore di 0.");
pausa();
} else {
error = false;
età = String.valueOf(etàInt);
}
} catch (InputMismatchException e) {
System.out.println("Errore: età non valida.");
pausa();
}
} while (error);
do { do {
error = true; error = true;
@ -223,54 +240,49 @@ public class ereditarieta {
System.out.print("Inserire la professione: "); System.out.print("Inserire la professione: ");
professione = sc.nextLine().toLowerCase(); professione = sc.nextLine().toLowerCase();
try { switch (professione) {
switch (professione) { case "nessuna":
case "nessuna": ritorno[arrayNuovo ? i : p_persone.length + i] = new Persona(nome, cognome, sesso, età);
ritorno[arrayNuovo ? i : p_persone.length + i] = new Persona(nome, cognome, sesso, età); error = false;
error = false; System.out.println("Persona inserita con successo.");
System.out.println("Persona inserita con successo."); break;
break; case "studente":
case "studente": System.out.print("Inserire la classe: ");
System.out.print("Inserire la classe: "); classe = sc.nextLine();
classe = sc.nextLine();
System.out.print("Inserire l'indirizzo di studio: "); System.out.print("Inserire l'indirizzo di studio: ");
indirizzoDiStudio = sc.nextLine(); indirizzoDiStudio = sc.nextLine();
ritorno[arrayNuovo ? i : p_persone.length + i] = new Studente(nome, cognome, sesso, età, ritorno[arrayNuovo ? i : p_persone.length + i] = new Studente(nome, cognome, sesso, età,
classe, indirizzoDiStudio); classe, indirizzoDiStudio);
error = false; error = false;
System.out.println("Studente inserito con successo."); System.out.println("Studente inserito con successo.");
break; break;
case "calciatore": case "calciatore":
System.out.print("Inserire la squadra: "); System.out.print("Inserire la squadra: ");
squadra = sc.nextLine(); squadra = sc.nextLine();
System.out.print("Inserire il numero di maglia: "); System.out.print("Inserire il numero di maglia: ");
numeroMaglia = sc.nextLine(); numeroMaglia = sc.nextLine();
ritorno[arrayNuovo ? i : p_persone.length + i] = new Calciatore(nome, cognome, sesso, età, ritorno[arrayNuovo ? i : p_persone.length + i] = new Calciatore(nome, cognome, sesso, età,
squadra, numeroMaglia); squadra, numeroMaglia);
error = false; error = false;
System.out.println("Calciatore inserito con successo."); System.out.println("Calciatore inserito con successo.");
break; break;
case "influencer": case "influencer":
System.out.print("Inserire il numero di follower: "); System.out.print("Inserire il numero di follower: ");
numeroFollower = sc.nextLine(); numeroFollower = sc.nextLine();
System.out.print("Inserire l'handle: "); System.out.print("Inserire l'handle: ");
handle = sc.nextLine(); handle = sc.nextLine();
ritorno[arrayNuovo ? i : p_persone.length + i] = new Influencer(nome, cognome, sesso, età, ritorno[arrayNuovo ? i : p_persone.length + i] = new Influencer(nome, cognome, sesso, età,
numeroFollower, handle); numeroFollower, handle);
error = false; error = false;
System.out.println("Influencer inserito con successo."); System.out.println("Influencer inserito con successo.");
break; break;
default: default:
System.out.println("Opzione non valida."); System.out.println("Opzione non valida.");
pausa(); pausa();
break; break;
}
} catch (InputMismatchException e) {
System.out.println("Errore: scelta non valida.");
pausa();
} }
} while (error); } while (error);