Compare commits
4 Commits
main
...
61a485c92b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
61a485c92b | ||
|
|
34cd28cfe7 | ||
|
|
3e8f99e2d9 | ||
|
|
0229a21f3b |
@@ -5,68 +5,63 @@
|
|||||||
package mybank;
|
package mybank;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Verde
|
* @author Verde
|
||||||
*/
|
*/
|
||||||
public class ContoCorrente {
|
public class ContoCorrente {
|
||||||
private String nome;
|
private Persona intestatario;
|
||||||
private String cognome;
|
|
||||||
private String codiceFiscale;
|
|
||||||
private double saldo;
|
private double saldo;
|
||||||
private int numeroContoCorrente;
|
private int numeroContoCorrente;
|
||||||
|
|
||||||
|
private static int totaleNumeriCorrenti;
|
||||||
private static ArrayList<Integer> numeriContiCorrenti = new ArrayList<>();
|
private static ArrayList<Integer> numeriContiCorrenti = new ArrayList<>();
|
||||||
|
|
||||||
@JsonFormat
|
|
||||||
(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
|
|
||||||
private Date dataDiNascita;
|
|
||||||
|
|
||||||
public ContoCorrente() {
|
public ContoCorrente() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public ContoCorrente(String nome, String cognome, String codiceFiscale, Date dataDiNascita, double saldo,
|
public ContoCorrente(Persona intestatario, double saldo) {
|
||||||
int numeroContoCorrente) {
|
this.intestatario = intestatario;
|
||||||
this.nome = nome;
|
|
||||||
this.cognome = cognome;
|
|
||||||
this.codiceFiscale = codiceFiscale;
|
|
||||||
this.dataDiNascita = dataDiNascita;
|
|
||||||
this.saldo = saldo;
|
this.saldo = saldo;
|
||||||
this.numeroContoCorrente = numeroContoCorrente;
|
this.numeroContoCorrente = totaleNumeriCorrenti++;
|
||||||
numeriContiCorrenti.add(numeroContoCorrente);
|
numeriContiCorrenti.add(numeroContoCorrente);
|
||||||
MyBank.log("Apertura del conto con saldo iniziale di " + this.saldo, numeroContoCorrente);
|
MyBank.log("Apertura del conto con saldo iniziale di " + this.saldo, numeroContoCorrente);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Integer> getNumeriContiCorrenti() {
|
public static List<Integer> getNumeriContiCorrenti() {
|
||||||
return numeriContiCorrenti;
|
return numeriContiCorrenti;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setNumeriContiCorrenti(List<Integer> numeriContiCorrenti) {
|
public static void setNumeriContiCorrenti(List<Integer> numeriContiCorrenti) {
|
||||||
ContoCorrente.numeriContiCorrenti = (ArrayList<Integer>)numeriContiCorrenti;
|
ContoCorrente.numeriContiCorrenti = (ArrayList<Integer>) numeriContiCorrenti;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNumeroContoCorrente() {
|
public int getNumeroContoCorrente() {
|
||||||
return numeroContoCorrente;
|
return numeroContoCorrente;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void versa(double quantita){
|
public Persona getIntestatario() {
|
||||||
|
return intestatario;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getSaldo() {
|
||||||
|
return saldo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void versa(double quantita) {
|
||||||
this.saldo += quantita;
|
this.saldo += quantita;
|
||||||
MyBank.log("Versamento di " + quantita + " effettuato con successo.", this.numeroContoCorrente);
|
MyBank.log("Versamento di " + quantita + " effettuato con successo.", this.numeroContoCorrente);
|
||||||
logSaldoCorrente();
|
logSaldoCorrente();
|
||||||
}
|
}
|
||||||
|
|
||||||
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.", this.numeroContoCorrente);
|
MyBank.log("Tentato prelievo di " + quantita + " fallito per superamento saldo.", this.numeroContoCorrente);
|
||||||
logSaldoCorrente();
|
logSaldoCorrente();
|
||||||
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.", this.numeroContoCorrente);
|
MyBank.log("Prelievo di " + quantita + " effettuato con successo.", this.numeroContoCorrente);
|
||||||
logSaldoCorrente();
|
logSaldoCorrente();
|
||||||
@@ -76,23 +71,4 @@ public class ContoCorrente {
|
|||||||
private void logSaldoCorrente() {
|
private void logSaldoCorrente() {
|
||||||
MyBank.log("Saldo corrente: " + this.saldo + "\n", this.numeroContoCorrente);
|
MyBank.log("Saldo corrente: " + this.saldo + "\n", this.numeroContoCorrente);
|
||||||
}
|
}
|
||||||
public String getNome() {
|
|
||||||
return nome;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getCognome() {
|
|
||||||
return cognome;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getCodiceFiscale() {
|
|
||||||
return codiceFiscale;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Date getDataDiNascita() {
|
|
||||||
return dataDiNascita;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double getSaldo() {
|
|
||||||
return saldo;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -15,6 +15,7 @@ import java.text.SimpleDateFormat;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.InputMismatchException;
|
import java.util.InputMismatchException;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
@@ -132,10 +133,8 @@ public class MyBank {
|
|||||||
numeriContiCorrenti.add(conti.getLast().getNumeroContoCorrente());
|
numeriContiCorrenti.add(conti.getLast().getNumeroContoCorrente());
|
||||||
ContoCorrente.setNumeriContiCorrenti(numeriContiCorrenti);
|
ContoCorrente.setNumeriContiCorrenti(numeriContiCorrenti);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception _) {
|
||||||
System.out.println("Errore nella lettura del file di conto corrente.");
|
System.out.println("Errore nella lettura del file di conto corrente.");
|
||||||
System.out.println(e.getMessage());
|
|
||||||
System.out.println(e.getStackTrace());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -144,46 +143,56 @@ public class MyBank {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void aggiungiConto(ArrayList<ContoCorrente> conti) {
|
static void aggiungiConto(ArrayList<ContoCorrente> conti) {
|
||||||
|
Persona persona;
|
||||||
|
boolean error;
|
||||||
|
ContoCorrente contoCorrente;
|
||||||
|
List<String> listaCodiciFiscali;
|
||||||
|
|
||||||
|
do {
|
||||||
|
error = false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
persona = persona();
|
||||||
|
listaCodiciFiscali = ScriviLeggiFile.leggiCodiciFiscali();
|
||||||
|
|
||||||
|
if (listaCodiciFiscali != null && listaCodiciFiscali.contains(persona.getCodiceFiscale())) {
|
||||||
|
System.out.println("Errore: esiste già un conto corrente per questo codice fiscale, riprovare.");
|
||||||
|
pausa();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
} while (error);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Persona persona() {
|
||||||
String nome;
|
String nome;
|
||||||
String cognome;
|
String cognome;
|
||||||
String codiceFiscale;
|
String codiceFiscale;
|
||||||
Date dataDiNascita;
|
Date dataDiNascita;
|
||||||
double saldoIniziale;
|
|
||||||
int numeroContoCorrente;
|
|
||||||
boolean error;
|
|
||||||
ContoCorrente contoCorrente;
|
|
||||||
|
|
||||||
do {
|
System.out.print("Inserire il proprio nome: ");
|
||||||
error = false;
|
nome = sc.nextLine().trim();
|
||||||
System.out.print("Inserire il proprio nome: ");
|
|
||||||
nome = sc.nextLine().trim();
|
|
||||||
|
|
||||||
System.out.print("Inserire il proprio cognome: ");
|
System.out.print("Inserire il proprio cognome: ");
|
||||||
cognome = sc.nextLine().trim();
|
cognome = sc.nextLine().trim();
|
||||||
|
|
||||||
codiceFiscale = codiceFiscale();
|
codiceFiscale = codiceFiscale();
|
||||||
|
|
||||||
dataDiNascita = dataDiNascita();
|
dataDiNascita = dataDiNascita();
|
||||||
|
|
||||||
saldoIniziale = quantita("del saldo iniziale");
|
return new Persona(nome, cognome, codiceFiscale, dataDiNascita);
|
||||||
|
|
||||||
numeroContoCorrente = Math.abs(codiceFiscale.hashCode());
|
|
||||||
|
|
||||||
if (ContoCorrente.getNumeriContiCorrenti().contains(numeroContoCorrente)) {
|
|
||||||
System.out.println("Errore: esiste già un conto corrente per questo codice fiscale, riprovare.");
|
|
||||||
pausa();
|
|
||||||
error = true;
|
|
||||||
} else {
|
|
||||||
File percorsoConti = new File(PATH_CONTI);
|
|
||||||
if (!percorsoConti.exists()) {
|
|
||||||
percorsoConti.mkdir();
|
|
||||||
}
|
|
||||||
contoCorrente = new ContoCorrente(nome, cognome, codiceFiscale, dataDiNascita, saldoIniziale,
|
|
||||||
numeroContoCorrente);
|
|
||||||
conti.add(contoCorrente);
|
|
||||||
salvaContoCorrente(contoCorrente);
|
|
||||||
}
|
|
||||||
} while (error);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static String codiceFiscale() {
|
static String codiceFiscale() {
|
||||||
|
|||||||
58
src/mybank/Persona.java
Normal file
58
src/mybank/Persona.java
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* 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.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Verde
|
||||||
|
*/
|
||||||
|
public class Persona {
|
||||||
|
|
||||||
|
private String nome;
|
||||||
|
private String cognome;
|
||||||
|
private String codiceFiscale;
|
||||||
|
|
||||||
|
@JsonFormat
|
||||||
|
(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy",
|
||||||
|
timezone = "Europe/Rome")
|
||||||
|
private Date dataDiNascita;
|
||||||
|
|
||||||
|
private static ArrayList<String> codiciFiscali = new ArrayList<>();
|
||||||
|
|
||||||
|
public Persona(){}
|
||||||
|
|
||||||
|
public Persona(String nome, String cognome, String codiceFiscale, Date dataDiNascita) {
|
||||||
|
this.nome = nome;
|
||||||
|
this.cognome = cognome;
|
||||||
|
this.codiceFiscale = codiceFiscale;
|
||||||
|
this.dataDiNascita = dataDiNascita;
|
||||||
|
codiciFiscali.add(codiceFiscale);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCodiceFiscale() {
|
||||||
|
return codiceFiscale;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> getCodiciFiscali() {
|
||||||
|
return codiciFiscali;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setCodiciFiscali(List<String> codiciFiscali) {
|
||||||
|
Persona.codiciFiscali = (ArrayList<String>) codiciFiscali;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNome() {
|
||||||
|
return nome;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCognome() {
|
||||||
|
return cognome;
|
||||||
|
}
|
||||||
|
}
|
||||||
49
src/mybank/ScriviLeggiFile.java
Normal file
49
src/mybank/ScriviLeggiFile.java
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* 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.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Verde
|
||||||
|
*/
|
||||||
|
public class ScriviLeggiFile {
|
||||||
|
private static final String PATH_CODICI_FISCALI = "./src/mybank/codiciFiscali.txt";
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user