Merge branch 'correzioni'

This commit is contained in:
La Programmatrice Verde
2026-01-13 23:12:27 +01:00
4 changed files with 75 additions and 21 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -8,6 +8,8 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonFormat;
/**
*
* @author Verde
@@ -16,12 +18,18 @@ public class ContoCorrente {
private String nome;
private String cognome;
private String codiceFiscale;
private Date dataDiNascita;
private double saldo;
private int numeroContoCorrente;
private static ArrayList<Integer> numeriContiCorrenti = new ArrayList<>();
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
private Date dataDiNascita;
public ContoCorrente() {
}
public ContoCorrente(String nome, String cognome, String codiceFiscale, Date dataDiNascita, double saldo,
int numeroContoCorrente) {
this.nome = nome;
@@ -48,17 +56,43 @@ public class ContoCorrente {
public void versa(double quantita){
this.saldo += quantita;
MyBank.log("Versamento di " + quantita + " effettuato con successo.\nSaldo corrente: " + this.saldo, this.numeroContoCorrente);
MyBank.log("Versamento di " + quantita + " effettuato con successo.", this.numeroContoCorrente);
logSaldoCorrente();
}
public void preleva(double quantita) throws IllegalArgumentException{
if (quantita >= this.saldo) {
MyBank.log("Tentato prelievo di " + quantita + " fallito per superamento saldo.\nSaldo corrente: " + this.saldo, this.numeroContoCorrente);
if (quantita > this.saldo) {
MyBank.log("Tentato prelievo di " + quantita + " fallito per superamento saldo.", this.numeroContoCorrente);
logSaldoCorrente();
throw new IllegalArgumentException("La quantità desiderata eccede il saldo corrente.");
}
else {
this.saldo -= quantita;
MyBank.log("Prelievo di " + quantita + " effettuato con successo.\nSaldo corrente: " + this.saldo, this.numeroContoCorrente);
MyBank.log("Prelievo di " + quantita + " effettuato con successo.", this.numeroContoCorrente);
logSaldoCorrente();
}
}
private void logSaldoCorrente() {
MyBank.log("Saldo corrente: " + this.saldo + "\n", this.numeroContoCorrente);
}
public String getNome() {
return nome;
}
public String getCognome() {
return cognome;
}
public String getCodiceFiscale() {
return codiceFiscale;
}
public Date getDataDiNascita() {
return dataDiNascita;
}
public double getSaldo() {
return saldo;
}
}

View File

@@ -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.");