Opzione 4
This commit is contained in:
@@ -33,6 +33,7 @@ public class ContoCorrente {
|
|||||||
this.saldo = saldo;
|
this.saldo = saldo;
|
||||||
this.numeroContoCorrente = numeroContoCorrente;
|
this.numeroContoCorrente = numeroContoCorrente;
|
||||||
numeriContiCorrenti.add(numeroContoCorrente);
|
numeriContiCorrenti.add(numeroContoCorrente);
|
||||||
|
MyBank.log("Apertura del conto con saldo iniziale di " + this.saldo, numeroContoCorrente);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Integer> getNumeriContiCorrenti() {
|
public static List<Integer> getNumeriContiCorrenti() {
|
||||||
@@ -45,14 +46,17 @@ public class ContoCorrente {
|
|||||||
|
|
||||||
public void versa(double quantita){
|
public void versa(double quantita){
|
||||||
this.saldo += 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{
|
public void preleva(double quantita) throws IllegalArgumentException{
|
||||||
if (quantita >= this.saldo) {
|
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.");
|
throw new IllegalArgumentException("La quantità desiderata eccede il saldo corrente.");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.saldo -= quantita;
|
this.saldo -= quantita;
|
||||||
|
MyBank.log("Prelievo di " + quantita + " effettuato con successo.\nSaldo corrente: " + this.saldo, this.numeroContoCorrente);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@ import java.io.BufferedWriter;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.FilenameFilter;
|
import java.io.IOException;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -82,7 +82,7 @@ public class MyBank {
|
|||||||
if (conti.isEmpty()) {
|
if (conti.isEmpty()) {
|
||||||
System.out.println(ERRORE_CONTI_VUOTO);
|
System.out.println(ERRORE_CONTI_VUOTO);
|
||||||
} else {
|
} else {
|
||||||
|
stampaLog(conti);
|
||||||
}
|
}
|
||||||
pausa();
|
pausa();
|
||||||
break;
|
break;
|
||||||
@@ -241,7 +241,7 @@ public class MyBank {
|
|||||||
new FileWriter(PATH_CONTI + "conto_" + conto.getNumeroContoCorrente() + ".json"))) {
|
new FileWriter(PATH_CONTI + "conto_" + conto.getNumeroContoCorrente() + ".json"))) {
|
||||||
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
|
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
|
||||||
bw.write(ow.writeValueAsString(conto));
|
bw.write(ow.writeValueAsString(conto));
|
||||||
} catch (Exception _) {
|
} catch (IOException _) {
|
||||||
System.out.println("Errore: impossibile salvare il conto corrente.");
|
System.out.println("Errore: impossibile salvare il conto corrente.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -292,4 +292,29 @@ public class MyBank {
|
|||||||
} while (error);
|
} while (error);
|
||||||
return contoCorrente;
|
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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user