Opzione 4

This commit is contained in:
La Programmatrice Verde
2026-01-13 21:12:25 +01:00
parent 6476d94a3b
commit 341eef7714
2 changed files with 32 additions and 3 deletions

View File

@@ -33,6 +33,7 @@ public class ContoCorrente {
this.saldo = saldo;
this.numeroContoCorrente = numeroContoCorrente;
numeriContiCorrenti.add(numeroContoCorrente);
MyBank.log("Apertura del conto con saldo iniziale di " + this.saldo, numeroContoCorrente);
}
public static List<Integer> getNumeriContiCorrenti() {
@@ -45,14 +46,17 @@ 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);
}
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);
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);
}
}
}

View File

@@ -9,7 +9,7 @@ import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -82,7 +82,7 @@ public class MyBank {
if (conti.isEmpty()) {
System.out.println(ERRORE_CONTI_VUOTO);
} else {
stampaLog(conti);
}
pausa();
break;
@@ -241,7 +241,7 @@ public class MyBank {
new FileWriter(PATH_CONTI + "conto_" + conto.getNumeroContoCorrente() + ".json"))) {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
bw.write(ow.writeValueAsString(conto));
} catch (Exception _) {
} catch (IOException _) {
System.out.println("Errore: impossibile salvare il conto corrente.");
}
}
@@ -292,4 +292,29 @@ public class MyBank {
} while (error);
return contoCorrente;
}
static void log(String messaggio, int numeroContoCorrente) {
StringBuilder sb = new StringBuilder();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.sss dd/MM/yyyy");
try (BufferedWriter bw = new BufferedWriter(
new FileWriter(PATH_CONTI + "movimenti_" + numeroContoCorrente + ".txt"))) {
sb.append("[");
sb.append(sdf.format(new Date()));
sb.append("] ");
sb.append(messaggio);
sb.append("\n");
bw.write(sb.toString());
} catch (IOException _) {
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"))) {
System.out.println(br.readAllAsString());
} catch (IOException _) {
System.out.println("Errore nella lettura dei movimenti.");
}
}
}