Salva saldo aggiornato
This commit is contained in:
@@ -10,14 +10,21 @@ import java.io.File;
|
||||
import java.io.FileReader;
|
||||
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.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Verde
|
||||
*/
|
||||
public class ScriviLeggiFile {
|
||||
private static final String PATH_CODICI_FISCALI = "./src/mybank/codiciFiscali.txt";
|
||||
private static final String PATH_CONTI = "./src/mybank/conti/";
|
||||
|
||||
private ScriviLeggiFile() {
|
||||
}
|
||||
@@ -46,4 +53,80 @@ public class ScriviLeggiFile {
|
||||
|
||||
return codiciFiscali;
|
||||
}
|
||||
|
||||
public static List<ContoCorrente> importaConti() {
|
||||
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")) {
|
||||
try (BufferedReader bf = new BufferedReader(new FileReader(conto))) {
|
||||
sb = new StringBuilder();
|
||||
riga = bf.readLine();
|
||||
|
||||
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();
|
||||
numeriContiCorrenti.add(conti.getLast().getNumeroContoCorrente());
|
||||
ContoCorrente.setNumeriContiCorrenti(numeriContiCorrenti);
|
||||
|
||||
} catch (IOException _) {
|
||||
System.out.println("Errore nella lettura del file di conto corrente.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return conti;
|
||||
}
|
||||
|
||||
public static void salvaContoCorrente(ContoCorrente conto) {
|
||||
File percorsoConti = new File(PATH_CONTI);
|
||||
if (!percorsoConti.exists()) {
|
||||
percorsoConti.mkdir();
|
||||
}
|
||||
|
||||
try (BufferedWriter bw = new BufferedWriter(
|
||||
new FileWriter(PATH_CONTI + "conto_" + conto.getNumeroContoCorrente() + ".json"))) {
|
||||
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
|
||||
bw.write(ow.writeValueAsString(conto));
|
||||
} catch (IOException _) {
|
||||
System.out.println("Errore: impossibile salvare il conto corrente.");
|
||||
}
|
||||
}
|
||||
|
||||
public 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", true))) {
|
||||
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.");
|
||||
}
|
||||
}
|
||||
|
||||
public static void stampaLog(ContoCorrente conto) {
|
||||
try (BufferedReader br = new BufferedReader(
|
||||
new FileReader(PATH_CONTI + "movimenti_" + conto.getNumeroContoCorrente() + ".txt"))) {
|
||||
System.out.println(br.readAllAsString());
|
||||
} catch (IOException _) {
|
||||
System.out.println("Errore nella lettura dei movimenti.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user