Files
GUIMyBank/src/logic/ContoCorrente.java
2026-03-03 23:00:00 +01:00

136 lines
3.8 KiB
Java

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package logic;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
*
* @author Verde
*/
public class ContoCorrente {
private String nome;
private String cognome;
private String codiceFiscale;
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;
StringBuilder sb = new StringBuilder();
public ContoCorrente() {
}
public ContoCorrente(String nome, String cognome, String codiceFiscale, Date dataDiNascita, double saldo,
int numeroContoCorrente) {
this.nome = nome;
this.cognome = cognome;
this.codiceFiscale = codiceFiscale;
this.dataDiNascita = dataDiNascita;
this.saldo = saldo;
this.numeroContoCorrente = numeroContoCorrente;
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;
}
public int getNumeroContoCorrente() {
return numeroContoCorrente;
}
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 {
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 {
this.saldo -= quantita;
log("Prelievo di " + quantita + " effettuato con successo.");
logSaldoCorrente();
}
}
private void logSaldoCorrente() {
log("Saldo corrente: " + this.saldo);
}
private void log(String messaggio) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.sss dd/MM/yyyy");
sb.append("[");
sb.append(sdf.format(new Date()));
sb.append("] ");
sb.append(messaggio);
sb.append("\n");
}
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 getLogAsString() {
return this.sb.toString();
}
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;
}
public StringBuilder getSb() {
return this.sb;
}
}