Salvataggio conti correnti
This commit is contained in:
BIN
lib/jackson-core-2.20.1.jar
Normal file
BIN
lib/jackson-core-2.20.1.jar
Normal file
Binary file not shown.
BIN
lib/jackson-databind-2.20.1.jar
Normal file
BIN
lib/jackson-databind-2.20.1.jar
Normal file
Binary file not shown.
@@ -6,6 +6,7 @@ package mybank;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -32,7 +33,11 @@ public class ContoCorrente {
|
||||
numeriContiCorrenti.add(numeroContoCorrente);
|
||||
}
|
||||
|
||||
public static ArrayList<Integer> getNumeriContiCorrenti() {
|
||||
public static List<Integer> getNumeriContiCorrenti() {
|
||||
return numeriContiCorrenti;
|
||||
}
|
||||
|
||||
public int getNumeroContoCorrente() {
|
||||
return numeroContoCorrente;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,12 @@
|
||||
*/
|
||||
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.FilenameFilter;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
@@ -13,6 +19,9 @@ import java.util.Scanner;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Verde
|
||||
@@ -25,14 +34,15 @@ public class MyBank {
|
||||
|
||||
static Scanner sc = new Scanner(System.in);
|
||||
static final String ERRORE_GENERICO = "Errore: opzione non valida.";
|
||||
static final String PATH_CONTI = "./src/MyBank/conti/";
|
||||
|
||||
public static void main(String[] args) {
|
||||
int scelta = -1;
|
||||
ArrayList<ContoCorrente> conti = new ArrayList<>();
|
||||
ArrayList<ContoCorrente> conti = importaConti();
|
||||
|
||||
do {
|
||||
System.out.println("Scegliere un'opzione:");
|
||||
System.out.println("1. Aggiungi conto corrente");
|
||||
System.out.println("1. Aprire conto corrente");
|
||||
System.out.println("2. ");
|
||||
System.out.println("3. ");
|
||||
System.out.println("4. ");
|
||||
@@ -95,6 +105,29 @@ public class MyBank {
|
||||
sc.nextLine();
|
||||
}
|
||||
|
||||
static ArrayList<ContoCorrente> importaConti() {
|
||||
ArrayList<ContoCorrente> conti = new ArrayList<>();
|
||||
File percorsoConti = new File(PATH_CONTI);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
if (percorsoConti.exists() && percorsoConti.listFiles().length != 0) {
|
||||
for (File conto : percorsoConti.listFiles()) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (conto.getName().substring(conto.getName().lastIndexOf(".")).equals("json")) {
|
||||
try (BufferedReader bf = new BufferedReader(new FileReader(conto))) {
|
||||
for (String rigaJSON : bf.readAllLines()) {
|
||||
sb.append(rigaJSON);
|
||||
}
|
||||
conti.add(mapper.readValue(sb.toString(), ContoCorrente.class));
|
||||
} catch (Exception _) {
|
||||
System.out.println("Errore nella lettura del file di conto corrente.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return conti;
|
||||
}
|
||||
|
||||
static void aggiungiConto(ArrayList<ContoCorrente> conti) {
|
||||
String nome;
|
||||
String cognome;
|
||||
@@ -103,6 +136,7 @@ public class MyBank {
|
||||
double saldoIniziale;
|
||||
int numeroContoCorrente;
|
||||
boolean error;
|
||||
ContoCorrente contoCorrente;
|
||||
|
||||
do {
|
||||
error = false;
|
||||
@@ -124,9 +158,15 @@ public class MyBank {
|
||||
System.out.println("Errore: esiste già un conto corrente per questo codice fiscale, riprovare.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
else {
|
||||
conti.add(new ContoCorrente(nome, cognome, codiceFiscale, dataDiNascita, saldoIniziale, numeroContoCorrente));
|
||||
} else {
|
||||
contoCorrente = new ContoCorrente(nome, cognome, codiceFiscale, dataDiNascita, saldoIniziale,
|
||||
numeroContoCorrente);
|
||||
conti.add(contoCorrente);
|
||||
File percorsoConti = new File(PATH_CONTI);
|
||||
if (!percorsoConti.exists()) {
|
||||
percorsoConti.mkdir();
|
||||
}
|
||||
salvaContoCorrente(contoCorrente);
|
||||
}
|
||||
} while (error);
|
||||
}
|
||||
@@ -196,4 +236,16 @@ public class MyBank {
|
||||
|
||||
return saldoIniziale;
|
||||
}
|
||||
|
||||
static void salvaContoCorrente(ContoCorrente conto) {
|
||||
|
||||
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 (Exception _) {
|
||||
System.out.println("Errore: impossibile salvare il conto corrente.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user