Correzioni
This commit is contained in:
@@ -19,8 +19,12 @@ import java.util.Scanner;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.w3c.dom.events.Event;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -35,7 +39,7 @@ public class MyBank {
|
||||
static Scanner sc = new Scanner(System.in);
|
||||
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/MyBank/conti/";
|
||||
static final String PATH_CONTI = "./src/mybank/conti/";
|
||||
|
||||
public static void main(String[] args) {
|
||||
int scelta = -1;
|
||||
@@ -46,7 +50,7 @@ public class MyBank {
|
||||
System.out.println("1. Aprire conto corrente");
|
||||
System.out.println("2. Versamento");
|
||||
System.out.println("3. Prelievo");
|
||||
System.out.println("4. ");
|
||||
System.out.println("4. Mostra movimenti");
|
||||
System.out.println("0. Esci");
|
||||
System.out.print("Opzione: ");
|
||||
|
||||
@@ -59,6 +63,7 @@ public class MyBank {
|
||||
break;
|
||||
case 1:
|
||||
aggiungiConto(conti);
|
||||
System.out.println("Numero conto: " + conti.getLast().getNumeroContoCorrente());
|
||||
System.out.println("Conto corrente aggiunto con successo.");
|
||||
pausa();
|
||||
break;
|
||||
@@ -67,6 +72,7 @@ public class MyBank {
|
||||
System.out.println(ERRORE_CONTI_VUOTO);
|
||||
} else {
|
||||
versa(conti);
|
||||
System.out.println("Versamento effettuato con successo.");
|
||||
}
|
||||
pausa();
|
||||
break;
|
||||
@@ -75,6 +81,7 @@ public class MyBank {
|
||||
System.out.println(ERRORE_CONTI_VUOTO);
|
||||
} else {
|
||||
preleva(conti);
|
||||
System.out.println("Prelievo effettuato con successo.");
|
||||
}
|
||||
pausa();
|
||||
break;
|
||||
@@ -107,20 +114,32 @@ public class MyBank {
|
||||
ArrayList<ContoCorrente> conti = new ArrayList<>();
|
||||
File percorsoConti = new File(PATH_CONTI);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setDateFormat(new SimpleDateFormat("dd/MM/yyyy"));
|
||||
StringBuilder sb;
|
||||
String riga;
|
||||
|
||||
if (percorsoConti.exists() && percorsoConti.listFiles().length != 0) {
|
||||
for (File conto : percorsoConti.listFiles()) {
|
||||
if (conto.getName().substring(conto.getName().lastIndexOf(".")).equals("json")) {
|
||||
if (conto.getName().substring(conto.getName().lastIndexOf(".")).equals(".json")) {
|
||||
try (BufferedReader bf = new BufferedReader(new FileReader(conto))) {
|
||||
sb = new StringBuilder();
|
||||
riga = bf.readLine();
|
||||
|
||||
conti.add(mapper.readValue(bf.readAllAsString(), ContoCorrente.class));
|
||||
while (riga != null) {
|
||||
sb.append(riga);
|
||||
riga = bf.readLine();
|
||||
}
|
||||
conti.add(mapper.readerFor(ContoCorrente.class).readValue(sb.toString()));
|
||||
|
||||
ArrayList<Integer> numeriContiCorrenti = (ArrayList<Integer>) ContoCorrente.getNumeriContiCorrenti();
|
||||
ArrayList<Integer> numeriContiCorrenti = (ArrayList<Integer>) ContoCorrente
|
||||
.getNumeriContiCorrenti();
|
||||
numeriContiCorrenti.add(conti.getLast().getNumeroContoCorrente());
|
||||
ContoCorrente.setNumeriContiCorrenti(numeriContiCorrenti);
|
||||
|
||||
} catch (Exception _) {
|
||||
} catch (Exception e) {
|
||||
System.out.println("Errore nella lettura del file di conto corrente.");
|
||||
System.out.println(e.getMessage());
|
||||
System.out.println(e.getStackTrace());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -152,20 +171,20 @@ public class MyBank {
|
||||
|
||||
saldoIniziale = quantita("del saldo iniziale");
|
||||
|
||||
numeroContoCorrente = codiceFiscale.hashCode();
|
||||
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 {
|
||||
contoCorrente = new ContoCorrente(nome, cognome, codiceFiscale, dataDiNascita, saldoIniziale,
|
||||
numeroContoCorrente);
|
||||
conti.add(contoCorrente);
|
||||
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);
|
||||
}
|
||||
} while (error);
|
||||
@@ -179,7 +198,7 @@ public class MyBank {
|
||||
|
||||
do {
|
||||
error = false;
|
||||
System.out.print("Inserire la propria data di nascita: ");
|
||||
System.out.print("Inserire il proprio codice fiscale: ");
|
||||
codiceFiscale = sc.nextLine().trim().toUpperCase();
|
||||
matcher = pattern.matcher(codiceFiscale);
|
||||
if (!matcher.find()) {
|
||||
@@ -194,12 +213,13 @@ public class MyBank {
|
||||
static Date dataDiNascita() {
|
||||
Date dataDiNascita = null;
|
||||
boolean error;
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
|
||||
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: ");
|
||||
System.out.print("Inserire la propria data di nascita nel formato " + FORMATO_DATA + ": ");
|
||||
try {
|
||||
dataDiNascita = sdf.parse(sc.nextLine());
|
||||
} catch (ParseException _) {
|
||||
@@ -238,7 +258,6 @@ public class MyBank {
|
||||
}
|
||||
|
||||
static void salvaContoCorrente(ContoCorrente conto) {
|
||||
|
||||
try (BufferedWriter bw = new BufferedWriter(
|
||||
new FileWriter(PATH_CONTI + "conto_" + conto.getNumeroContoCorrente() + ".json"))) {
|
||||
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
|
||||
@@ -311,9 +330,10 @@ public class MyBank {
|
||||
System.out.println("Errore nella scrittura del movimento.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void stampaLog(ArrayList<ContoCorrente> conti) {
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(PATH_CONTI + "movimenti_" + selezionaConto(conti) + ".txt"))) {
|
||||
try (BufferedReader br = new BufferedReader(
|
||||
new FileReader(PATH_CONTI + "movimenti_" + selezionaConto(conti).getNumeroContoCorrente() + ".txt"))) {
|
||||
System.out.println(br.readAllAsString());
|
||||
} catch (IOException _) {
|
||||
System.out.println("Errore nella lettura dei movimenti.");
|
||||
|
||||
Reference in New Issue
Block a user