Aggiungi conto corrente
This commit is contained in:
@@ -36,10 +36,14 @@ public class MyBank {
|
||||
static final String ERRORE_GENERICO = "Errore: opzione non valida.";
|
||||
static final String ERRORE_CONTI_VUOTO = "Errore: è necessario aggiungere almeno un conto corrente prima di proseguire.";
|
||||
static final String PATH_CONTI = "./src/logic/conti/";
|
||||
private static ArrayList<ContoCorrente> conti = importaConti();
|
||||
|
||||
public static ArrayList<ContoCorrente> getConti() {
|
||||
return conti;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int scelta = -1;
|
||||
ArrayList<ContoCorrente> conti = importaConti();
|
||||
|
||||
do {
|
||||
System.out.println("Scegliere un'opzione:");
|
||||
@@ -58,7 +62,6 @@ public class MyBank {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
aggiungiConto(conti);
|
||||
System.out.println("Numero conto: " + conti.getLast().getNumeroContoCorrente());
|
||||
System.out.println("Conto corrente aggiunto con successo.");
|
||||
pausa();
|
||||
@@ -143,114 +146,56 @@ public class MyBank {
|
||||
return conti;
|
||||
}
|
||||
|
||||
static void aggiungiConto(ArrayList<ContoCorrente> conti) {
|
||||
String nome;
|
||||
String cognome;
|
||||
String codiceFiscale;
|
||||
Date dataDiNascita;
|
||||
double saldoIniziale;
|
||||
public static ContoCorrente aggiungiConto(String nome, String cognome, String codiceFiscale, Date dataDiNascita,
|
||||
double saldoIniziale) {
|
||||
int numeroContoCorrente;
|
||||
boolean error;
|
||||
ContoCorrente contoCorrente;
|
||||
boolean exit = false;
|
||||
int i = 0;
|
||||
|
||||
do {
|
||||
error = false;
|
||||
System.out.print("Inserire il proprio nome: ");
|
||||
nome = sc.nextLine().trim();
|
||||
numeroContoCorrente = Math.abs(codiceFiscale.hashCode());
|
||||
|
||||
System.out.print("Inserire il proprio cognome: ");
|
||||
cognome = sc.nextLine().trim();
|
||||
|
||||
codiceFiscale = codiceFiscale();
|
||||
|
||||
dataDiNascita = dataDiNascita();
|
||||
|
||||
saldoIniziale = quantita("del saldo iniziale");
|
||||
|
||||
numeroContoCorrente = Math.abs(codiceFiscale.hashCode());
|
||||
|
||||
if (ContoCorrente.getNumeriContiCorrenti().contains(numeroContoCorrente)) {
|
||||
System.out.println("Errore: esiste già un conto corrente per questo codice fiscale, riprovare.");
|
||||
pausa();
|
||||
error = true;
|
||||
} else {
|
||||
File percorsoConti = new File(PATH_CONTI);
|
||||
if (!percorsoConti.exists()) {
|
||||
percorsoConti.mkdir();
|
||||
}
|
||||
contoCorrente = new ContoCorrente(nome, cognome, codiceFiscale, dataDiNascita, saldoIniziale,
|
||||
numeroContoCorrente);
|
||||
conti.add(contoCorrente);
|
||||
salvaContoCorrente(contoCorrente);
|
||||
if (ContoCorrente.getNumeriContiCorrenti().contains(numeroContoCorrente)) {
|
||||
for (; i < conti.size() && exit; i++) {
|
||||
exit = conti.get(i).getNumeroContoCorrente() == numeroContoCorrente;
|
||||
}
|
||||
} while (error);
|
||||
contoCorrente = conti.get(i);
|
||||
} else {
|
||||
File percorsoConti = new File(PATH_CONTI);
|
||||
if (!percorsoConti.exists()) {
|
||||
percorsoConti.mkdir();
|
||||
}
|
||||
contoCorrente = new ContoCorrente(nome, cognome, codiceFiscale, dataDiNascita, saldoIniziale,
|
||||
numeroContoCorrente);
|
||||
conti.add(contoCorrente);
|
||||
salvaContoCorrente(contoCorrente);
|
||||
}
|
||||
|
||||
return contoCorrente;
|
||||
}
|
||||
|
||||
static String codiceFiscale() {
|
||||
String codiceFiscale;
|
||||
boolean error;
|
||||
public static boolean isCodiceFiscaleValid(String codiceFiscale) {
|
||||
Pattern pattern = Pattern.compile("[A-Z]{6}[ABCDEHLMPRST]{3}\\d{2}[A-Z]\\d{3}[A-Z]");
|
||||
Matcher matcher;
|
||||
|
||||
do {
|
||||
error = false;
|
||||
System.out.print("Inserire il proprio codice fiscale: ");
|
||||
codiceFiscale = sc.nextLine().trim().toUpperCase();
|
||||
matcher = pattern.matcher(codiceFiscale);
|
||||
if (!matcher.find()) {
|
||||
System.out.println(ERRORE_GENERICO);
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} while (error);
|
||||
return codiceFiscale;
|
||||
return pattern.matcher(codiceFiscale).find();
|
||||
}
|
||||
|
||||
static Date dataDiNascita() {
|
||||
Date dataDiNascita = null;
|
||||
boolean error;
|
||||
public static Date dataDiNascita(String dataDiNascita) throws ParseException {
|
||||
final String FORMATO_DATA = "dd/MM/yyyy";
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(FORMATO_DATA);
|
||||
sdf.setLenient(false);
|
||||
|
||||
do {
|
||||
error = false;
|
||||
System.out.print("Inserire la propria data di nascita nel formato " + FORMATO_DATA + ": ");
|
||||
try {
|
||||
dataDiNascita = sdf.parse(sc.nextLine());
|
||||
} catch (ParseException _) {
|
||||
System.out.println("Errore: la data inserita non è valida.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} while (error);
|
||||
return dataDiNascita;
|
||||
return sdf.parse(dataDiNascita);
|
||||
}
|
||||
|
||||
static double quantita(String diCheCosa) {
|
||||
double quantita = 0;
|
||||
boolean error;
|
||||
public static double valoreDouble(String numeroDouble) throws NumberFormatException {
|
||||
double valoreDouble;
|
||||
|
||||
do {
|
||||
error = false;
|
||||
System.out.print("Inserire la quantità " + diCheCosa + ": ");
|
||||
try {
|
||||
quantita = sc.nextDouble();
|
||||
sc.nextLine();
|
||||
valoreDouble = Double.parseDouble(numeroDouble);
|
||||
|
||||
if (quantita <= 0) {
|
||||
System.out.println("Errore: la quantità non può essere minore o uguale a zero.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} catch (InputMismatchException _) {
|
||||
System.out.println(ERRORE_GENERICO);
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} while (error);
|
||||
if (valoreDouble <= 0) {
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
|
||||
return quantita;
|
||||
return valoreDouble;
|
||||
}
|
||||
|
||||
static void salvaContoCorrente(ContoCorrente conto) {
|
||||
|
||||
Reference in New Issue
Block a user