168 lines
4.8 KiB
Java
168 lines
4.8 KiB
Java
/*
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
|
*/
|
|
package appComune;
|
|
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.InputMismatchException;
|
|
import java.util.Scanner;
|
|
|
|
/**
|
|
*
|
|
* @author Verde
|
|
*/
|
|
public class MainComune {
|
|
static Scanner sc = new Scanner(System.in);
|
|
static final String PATTERN_DATA = "dd/MM/yyyy";
|
|
static final String[] sessiAmmessi = { "m", "f" };
|
|
|
|
public static void main(String[] args) {
|
|
int scelta = -1;
|
|
ArrayList<Anagrafica> cittadini = new ArrayList<>();
|
|
|
|
do {
|
|
System.out.println("Scegliere un'opzione:");
|
|
System.out.println("1. Inserisci dati");
|
|
System.out.println("2. Stampa dati");
|
|
System.out.println("0. Esci");
|
|
System.out.print("Opzione: ");
|
|
|
|
try {
|
|
scelta = sc.nextInt();
|
|
sc.nextLine();
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
aggiungiCittatino(cittadini);
|
|
pausa();
|
|
break;
|
|
case 2:
|
|
stampaCittadini(cittadini);
|
|
pausa();
|
|
break;
|
|
default:
|
|
System.out.println("Opzione non valida.");
|
|
pausa();
|
|
break;
|
|
}
|
|
} catch (InputMismatchException _) {
|
|
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 void aggiungiCittatino(ArrayList<Anagrafica> cittadini) {
|
|
String nome;
|
|
String cognome;
|
|
Date dataNascita;
|
|
String luogoNascita;
|
|
String sesso;
|
|
|
|
System.out.print("Inserire il nome, compresi eventuali secondi nomi: ");
|
|
nome = sc.nextLine().trim();
|
|
|
|
System.out.print("Inserire il cognome, compresi eventuali secondi cognomi: ");
|
|
cognome = sc.nextLine().trim();
|
|
|
|
dataNascita = dataNascita();
|
|
|
|
luogoNascita = luogoNascita();
|
|
|
|
sesso = sesso();
|
|
|
|
cittadini.add(new Anagrafica(nome, cognome, dataNascita, luogoNascita, sesso));
|
|
|
|
}
|
|
|
|
static Date dataNascita() {
|
|
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATA);
|
|
Date ritorno = null;
|
|
boolean error;
|
|
|
|
do {
|
|
error = false;
|
|
|
|
try {
|
|
System.out.print("Inserire la data di nascita nel formato gg/mm/aa: ");
|
|
ritorno = sdf.parse(sc.nextLine().trim());
|
|
} catch (ParseException _) {
|
|
System.out.println("Errore nella lettura della data di nascita.");
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} while (error);
|
|
|
|
return ritorno;
|
|
}
|
|
|
|
static String luogoNascita() {
|
|
String ritorno = null;
|
|
boolean error;
|
|
final String COMUNE = "varese";
|
|
|
|
do {
|
|
error = false;
|
|
|
|
System.out.print("Inserire il comune di nascita: ");
|
|
ritorno = sc.nextLine().toLowerCase().trim();
|
|
|
|
if (!ritorno.equals(COMUNE)) {
|
|
System.out.println("Errore: è consentito esclusivamente il comune di " + COMUNE);
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} while (error);
|
|
|
|
return ritorno;
|
|
}
|
|
|
|
static String sesso() {
|
|
String ritorno = null;
|
|
boolean error;
|
|
boolean exit = false;
|
|
|
|
do {
|
|
error = false;
|
|
|
|
System.out.println("Inserire il sesso tra le seguenti opzioni: ");
|
|
for (String sesso : sessiAmmessi) {
|
|
System.out.println(sesso);
|
|
}
|
|
System.out.print("Scelta: ");
|
|
ritorno = sc.nextLine().toLowerCase().trim();
|
|
|
|
for (int i = 0; i < sessiAmmessi.length && !exit; i++) {
|
|
exit = ritorno.equals(sessiAmmessi[i]);
|
|
}
|
|
|
|
if (!exit) {
|
|
System.out.println("Errore: il sesso può essere solo una di queste opzioni:");
|
|
for (String sesso : sessiAmmessi) {
|
|
System.out.println(sesso);
|
|
}
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} while (error);
|
|
|
|
return ritorno;
|
|
}
|
|
|
|
static void stampaCittadini(ArrayList<Anagrafica> cittadini) {
|
|
for (Anagrafica cittadino : cittadini) {
|
|
System.out.println(cittadino.toString());
|
|
}
|
|
}
|
|
} |