Salva saldo aggiornato
This commit is contained in:
@@ -27,7 +27,7 @@ public class ContoCorrente {
|
||||
this.saldo = saldo;
|
||||
this.numeroContoCorrente = totaleNumeriCorrenti++;
|
||||
numeriContiCorrenti.add(numeroContoCorrente);
|
||||
MyBank.log("Apertura del conto con saldo iniziale di " + this.saldo, numeroContoCorrente);
|
||||
ScriviLeggiFile.log("Apertura del conto con saldo iniziale di " + this.saldo, numeroContoCorrente);
|
||||
}
|
||||
|
||||
public static List<Integer> getNumeriContiCorrenti() {
|
||||
@@ -52,24 +52,26 @@ public class ContoCorrente {
|
||||
|
||||
public void versa(double quantita) {
|
||||
this.saldo += quantita;
|
||||
MyBank.log("Versamento di " + quantita + " effettuato con successo.", this.numeroContoCorrente);
|
||||
ScriviLeggiFile.log("Versamento di " + quantita + " effettuato con successo.", this.numeroContoCorrente);
|
||||
ScriviLeggiFile.salvaContoCorrente(this);
|
||||
logSaldoCorrente();
|
||||
}
|
||||
|
||||
public void preleva(double quantita) throws IllegalArgumentException {
|
||||
if (quantita > this.saldo) {
|
||||
MyBank.log("Tentato prelievo di " + quantita + " fallito per superamento saldo.", this.numeroContoCorrente);
|
||||
ScriviLeggiFile.log("Tentato prelievo di " + quantita + " fallito per superamento saldo.", this.numeroContoCorrente);
|
||||
logSaldoCorrente();
|
||||
throw new IllegalArgumentException("La quantità desiderata eccede il saldo corrente.");
|
||||
} else {
|
||||
this.saldo -= quantita;
|
||||
MyBank.log("Prelievo di " + quantita + " effettuato con successo.", this.numeroContoCorrente);
|
||||
ScriviLeggiFile.log("Prelievo di " + quantita + " effettuato con successo.", this.numeroContoCorrente);
|
||||
ScriviLeggiFile.salvaContoCorrente(this);
|
||||
logSaldoCorrente();
|
||||
}
|
||||
}
|
||||
|
||||
private void logSaldoCorrente() {
|
||||
MyBank.log("Saldo corrente: " + this.saldo + "\n", this.numeroContoCorrente);
|
||||
ScriviLeggiFile.log("Saldo corrente: " + this.saldo + "\n", this.numeroContoCorrente);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,11 +4,6 @@
|
||||
*/
|
||||
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.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -20,9 +15,6 @@ 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
|
||||
@@ -36,11 +28,10 @@ public class MyBank {
|
||||
static Scanner sc = new Scanner(System.in);
|
||||
static final String ERRORE_GENERICO = "Errore: opzione non valida.";
|
||||
static final String ERRORE_CONTI_VUOTO = "Errore: è necessario aggiungere almeno un conto corrente prima di proseguire.";
|
||||
static final String PATH_CONTI = "./src/mybank/conti/";
|
||||
|
||||
public static void main(String[] args) {
|
||||
int scelta = -1;
|
||||
ArrayList<ContoCorrente> conti = importaConti();
|
||||
ArrayList<ContoCorrente> conti = (ArrayList<ContoCorrente>) ScriviLeggiFile.importaConti();
|
||||
|
||||
do {
|
||||
System.out.println("Scegliere un'opzione:");
|
||||
@@ -95,7 +86,7 @@ public class MyBank {
|
||||
if (conti.isEmpty()) {
|
||||
System.out.println(ERRORE_CONTI_VUOTO);
|
||||
} else {
|
||||
stampaLog(conti);
|
||||
ScriviLeggiFile.stampaLog(selezionaConto(conti));
|
||||
}
|
||||
pausa();
|
||||
break;
|
||||
@@ -116,41 +107,6 @@ public class MyBank {
|
||||
sc.nextLine();
|
||||
}
|
||||
|
||||
static ArrayList<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 (Exception _) {
|
||||
System.out.println("Errore nella lettura del file di conto corrente.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return conti;
|
||||
}
|
||||
|
||||
static void aggiungiConto(ArrayList<ContoCorrente> conti) {
|
||||
Persona persona;
|
||||
boolean error;
|
||||
@@ -170,13 +126,9 @@ public class MyBank {
|
||||
error = true;
|
||||
} else {
|
||||
ScriviLeggiFile.salvaCodiceFiscale(persona.getCodiceFiscale());
|
||||
File percorsoConti = new File(PATH_CONTI);
|
||||
if (!percorsoConti.exists()) {
|
||||
percorsoConti.mkdir();
|
||||
}
|
||||
contoCorrente = new ContoCorrente(persona, quantita("del saldo iniziale"));
|
||||
conti.add(contoCorrente);
|
||||
salvaContoCorrente(contoCorrente);
|
||||
ScriviLeggiFile.salvaContoCorrente(contoCorrente);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println(e.getMessage());
|
||||
@@ -271,16 +223,6 @@ public class MyBank {
|
||||
return quantita;
|
||||
}
|
||||
|
||||
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 (IOException _) {
|
||||
System.out.println("Errore: impossibile salvare il conto corrente.");
|
||||
}
|
||||
}
|
||||
|
||||
static void versa(ArrayList<ContoCorrente> conti) {
|
||||
selezionaConto(conti).versa(quantita("da versare"));
|
||||
}
|
||||
@@ -302,7 +244,7 @@ public class MyBank {
|
||||
}
|
||||
|
||||
static ContoCorrente selezionaConto(ArrayList<ContoCorrente> conti) {
|
||||
ContoCorrente contoCorrente = null;
|
||||
ContoCorrente contoCorrente = new ContoCorrente();
|
||||
int numeroContoCorrente;
|
||||
boolean error;
|
||||
|
||||
@@ -327,30 +269,4 @@ public class MyBank {
|
||||
} while (error);
|
||||
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", 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.");
|
||||
}
|
||||
}
|
||||
|
||||
static void stampaLog(ArrayList<ContoCorrente> conti) {
|
||||
try (BufferedReader br = new BufferedReader(
|
||||
new FileReader(PATH_CONTI + "movimenti_" + selezionaConto(conti).getNumeroContoCorrente() + ".txt"))) {
|
||||
System.out.println(br.readAllAsString());
|
||||
} catch (IOException _) {
|
||||
System.out.println("Errore nella lettura dei movimenti.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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