207 lines
7.3 KiB
Java
207 lines
7.3 KiB
Java
/*
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
|
|
*/
|
|
package ereditarieta;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.InputMismatchException;
|
|
import java.util.Scanner;
|
|
|
|
/**
|
|
*
|
|
* @author Verde
|
|
*/
|
|
public class ereditarieta {
|
|
|
|
/**
|
|
* @param args the command line arguments
|
|
*/
|
|
|
|
static Scanner sc = new Scanner(System.in);
|
|
|
|
public static void main(String[] args) {
|
|
int scelta = -1;
|
|
Persona persone[] = null;
|
|
boolean opzione1 = false;
|
|
|
|
do {
|
|
System.out.println("Scegliere un'opzione:");
|
|
System.out.println("1. Inserire le persone");
|
|
System.out.println("2. Mostra persone");
|
|
System.out.println("3. ");
|
|
System.out.println("0. Esci");
|
|
System.out.print("Opzione: ");
|
|
|
|
try {
|
|
scelta = sc.nextInt();
|
|
sc.nextLine();
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
persone = InserisciPersone(persone);
|
|
opzione1 = true;
|
|
Pausa();
|
|
break;
|
|
case 2:
|
|
if (opzione1) {
|
|
MostraPersone(persone);
|
|
} else {
|
|
System.out.println("Errore: è necessario aggiungere almeno una persona prima di mostrarle.");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 3:
|
|
|
|
Pausa();
|
|
break;
|
|
default:
|
|
System.out.println("Opzione non valida.");
|
|
Pausa();
|
|
break;
|
|
}
|
|
} catch (InputMismatchException e) {
|
|
System.out.println("Errore: scelta non valida.");
|
|
Pausa();
|
|
}
|
|
} while (scelta != 0);
|
|
}
|
|
|
|
public static void Pausa() {
|
|
System.out.println("Premere un tasto per continuare. . .");
|
|
sc.nextLine();
|
|
}
|
|
|
|
static Persona[] InserisciPersone(Persona[] p_persone) {
|
|
Persona ritorno[] = null;
|
|
int numeroPersone = 0;
|
|
boolean error;
|
|
String nome;
|
|
String cognome;
|
|
String sesso;
|
|
String età;
|
|
String professione;
|
|
String classe;
|
|
String indirizzoDiStudio;
|
|
String squadra;
|
|
String numeroMaglia;
|
|
String numeroFollower;
|
|
String handle; // il nome sui social con cui trovare l'influencer
|
|
|
|
do {
|
|
error = true;
|
|
try {
|
|
System.out.print("Inserire il numero di persone da aggiungere: ");
|
|
numeroPersone = sc.nextInt();
|
|
sc.nextLine();
|
|
if (numeroPersone <= 0) {
|
|
System.out.println("Errore: è necessario inserire almeno una persona.");
|
|
Pausa();
|
|
} else {
|
|
error = false;
|
|
}
|
|
} catch (InputMismatchException e) {
|
|
System.out.println("Errore nella lettura del numero.");
|
|
Pausa();
|
|
}
|
|
|
|
} while (error);
|
|
|
|
if (p_persone == null) {
|
|
ritorno = new Persona[numeroPersone];
|
|
}
|
|
else {
|
|
ritorno = Arrays.copyOf(p_persone, p_persone.length + numeroPersone, p_persone.getClass());
|
|
}
|
|
|
|
for (int i = 0; i < numeroPersone; i++) {
|
|
System.out.print("Inserire il nome della persona: ");
|
|
nome = sc.nextLine();
|
|
|
|
System.out.print("Inserire il cognome della persona: ");
|
|
cognome = sc.nextLine();
|
|
|
|
System.out.print("Inserire il sesso: ");
|
|
sesso = sc.nextLine();
|
|
|
|
System.out.print("Inserire l'età: ");
|
|
età = sc.nextLine();
|
|
|
|
do {
|
|
error = true;
|
|
System.out.println("Opzioni ammesse:");
|
|
System.out.println("Nessuna");
|
|
System.out.println("Studente");
|
|
System.out.println("Calciatore");
|
|
System.out.println("Influencer");
|
|
System.out.print("Inserire la professione: ");
|
|
professione = sc.nextLine().toLowerCase();
|
|
|
|
try {
|
|
switch (professione) {
|
|
case "nessuna":
|
|
ritorno[i] = new Persona(nome, cognome, sesso, età);
|
|
error = false;
|
|
System.out.println("Persona inserita con successo.");
|
|
break;
|
|
case "studente":
|
|
System.out.print("Inserire la classe: ");
|
|
classe = sc.nextLine();
|
|
sc.nextLine();
|
|
|
|
System.out.print("Inserire l'indirizzo di studio: ");
|
|
indirizzoDiStudio = sc.nextLine();
|
|
sc.nextLine();
|
|
ritorno[i] = new Studente(nome, cognome, sesso, età, classe, indirizzoDiStudio);
|
|
error = false;
|
|
System.out.println("Studente inserito con successo.");
|
|
break;
|
|
case "calciatore":
|
|
System.out.print("Inserire la squadra: ");
|
|
squadra = sc.nextLine();
|
|
sc.nextLine();
|
|
|
|
System.out.print("Inserire il numero di maglia: ");
|
|
numeroMaglia = sc.nextLine();
|
|
sc.nextLine();
|
|
ritorno[i] = new Calciatore(nome, cognome, sesso, età, squadra, numeroMaglia);
|
|
error = false;
|
|
System.out.println("Calciatore inserito con successo.");
|
|
break;
|
|
case "influencer":
|
|
System.out.print("Inserire il numero di follower: ");
|
|
numeroFollower = sc.nextLine();
|
|
sc.nextLine();
|
|
|
|
System.out.print("Inserire l'handle: ");
|
|
handle = sc.nextLine();
|
|
sc.nextLine();
|
|
ritorno[i] = new Influencer(nome, cognome, sesso, età, numeroFollower, handle);
|
|
error = false;
|
|
System.out.println("Influencer inserito con successo.");
|
|
break;
|
|
default:
|
|
System.out.println("Opzione non valida.");
|
|
Pausa();
|
|
break;
|
|
}
|
|
} catch (InputMismatchException e) {
|
|
System.out.println("Errore: scelta non valida.");
|
|
Pausa();
|
|
}
|
|
} while (error);
|
|
|
|
}
|
|
return ritorno;
|
|
}
|
|
|
|
static void MostraPersone(Persona[] p_persone) {
|
|
for (Persona persona : p_persone) {
|
|
persona.MostraPersona();
|
|
System.out.println();
|
|
}
|
|
}
|
|
}
|