127 lines
4.7 KiB
Java
127 lines
4.7 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 mybank;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedWriter;
|
|
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() {
|
|
}
|
|
|
|
public static void salvaCodiceFiscale(String codiceFiscale) throws IOException {
|
|
try (BufferedWriter bw = new BufferedWriter(
|
|
new FileWriter(PATH_CODICI_FISCALI, true))) {
|
|
bw.write(codiceFiscale + "\n");
|
|
} catch (IOException _) {
|
|
throw new IOException("Errore nella scrittura del codice fiscale.");
|
|
}
|
|
}
|
|
|
|
public static List<String> leggiCodiciFiscali() throws IOException {
|
|
List<String> codiciFiscali = null;
|
|
|
|
File percorsoCodiciFiscali = new File(PATH_CODICI_FISCALI);
|
|
if (percorsoCodiciFiscali.exists()) {
|
|
try (BufferedReader bf = new BufferedReader(
|
|
new FileReader(PATH_CODICI_FISCALI))) {
|
|
codiciFiscali = bf.readAllLines();
|
|
} catch (IOException _) {
|
|
throw new IOException("Errore nella lettura dei codici fiscali.");
|
|
}
|
|
}
|
|
|
|
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()));
|
|
|
|
} 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.");
|
|
}
|
|
}
|
|
} |