Esportazione ed importazione conti correnti corretta

This commit is contained in:
La Programmatrice Verde
2026-03-03 23:00:00 +01:00
parent 8dca146c03
commit de989d43f5
18 changed files with 209 additions and 194 deletions

View File

@@ -4,9 +4,7 @@
*/
package logic;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
@@ -30,15 +28,16 @@ public class ContoCorrente {
private static ArrayList<Integer> numeriContiCorrenti = new ArrayList<>();
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
private Date dataDiNascita;
StringBuilder sb = new StringBuilder();
public ContoCorrente() {
}
public ContoCorrente(String nome, String cognome, String codiceFiscale, Date dataDiNascita, double saldo,
int numeroContoCorrente) throws IOException{
int numeroContoCorrente) {
this.nome = nome;
this.cognome = cognome;
this.codiceFiscale = codiceFiscale;
@@ -48,67 +47,67 @@ public class ContoCorrente {
numeriContiCorrenti.add(numeroContoCorrente);
log("Apertura del conto con saldo iniziale di " + this.saldo);
}
public static List<Integer> getNumeriContiCorrenti() {
return numeriContiCorrenti;
}
public static void setNumeriContiCorrenti(List<Integer> numeriContiCorrenti) {
ContoCorrente.numeriContiCorrenti = (ArrayList<Integer>)numeriContiCorrenti;
ContoCorrente.numeriContiCorrenti = (ArrayList<Integer>) numeriContiCorrenti;
}
public int getNumeroContoCorrente() {
return numeroContoCorrente;
}
public void versa(double quantita) throws IOException{
public void delNumeroContoCorrente() {
numeriContiCorrenti.removeLast();
}
public void versa(double quantita) {
this.saldo += quantita;
log("Versamento di " + quantita + " effettuato con successo.");
logSaldoCorrente();
}
public void preleva(double quantita) throws IllegalArgumentException, IOException{
public void preleva(double quantita) throws IllegalArgumentException {
if (quantita > this.saldo) {
log("Tentato prelievo di " + quantita + " fallito per superamento saldo.");
logSaldoCorrente();
throw new IllegalArgumentException("La quantità desiderata eccede il saldo corrente.");
}
else {
} else {
this.saldo -= quantita;
log("Prelievo di " + quantita + " effettuato con successo.");
logSaldoCorrente();
}
}
private void logSaldoCorrente() throws IOException{
private void logSaldoCorrente() {
log("Saldo corrente: " + this.saldo);
}
private void log(String messaggio) throws IOException{
StringBuilder sb = new StringBuilder();
private void log(String messaggio) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.sss dd/MM/yyyy");
try (BufferedWriter bw = new BufferedWriter(
new FileWriter(MyBank.PATH_CONTI + "movimenti_" + this.numeroContoCorrente + ".txt", true))) {
sb.append("[");
sb.append(sdf.format(new Date()));
sb.append("] ");
sb.append(messaggio);
sb.append("\n");
sb.append("[");
sb.append(sdf.format(new Date()));
sb.append("] ");
sb.append(messaggio);
sb.append("\n");
}
bw.write(sb.toString());
public void salvaLog() throws IOException {
try (BufferedWriter bw = new BufferedWriter(
new FileWriter(MyBank.PATH_CONTI + "movimenti_" + this.numeroContoCorrente + ".txt"))) {
bw.write(this.sb.toString());
} catch (IOException _) {
throw new IOException("Errore nella scrittura del movimento.");
}
}
@JsonIgnore
public String getLog() throws IOException{
try (BufferedReader br = new BufferedReader(
new FileReader(MyBank.PATH_CONTI + "movimenti_" + this.numeroContoCorrente + ".txt"))) {
return br.readAllAsString();
} catch (IOException _) {
throw new IOException("Errore nella lettura dei movimenti.");
}
public String getLogAsString() {
return this.sb.toString();
}
public String getNome() {
@@ -130,4 +129,8 @@ public class ContoCorrente {
public double getSaldo() {
return saldo;
}
public StringBuilder getSb() {
return this.sb;
}
}