diff --git a/build.xml b/build.xml deleted file mode 100644 index 358789b..0000000 --- a/build.xml +++ /dev/null @@ -1,73 +0,0 @@ - - - - - - - - - - - Builds, tests, and runs the project GUIMyBank. - - - diff --git a/lib/jackson-annotations-3.0-rc5.jar b/lib/jackson-annotations-3.0-rc5.jar new file mode 100644 index 0000000..833505a Binary files /dev/null and b/lib/jackson-annotations-3.0-rc5.jar differ diff --git a/lib/jackson-core-2.20.1.jar b/lib/jackson-core-2.20.1.jar new file mode 100644 index 0000000..10216b4 Binary files /dev/null and b/lib/jackson-core-2.20.1.jar differ diff --git a/lib/jackson-databind-2.20.1.jar b/lib/jackson-databind-2.20.1.jar new file mode 100644 index 0000000..67e574d Binary files /dev/null and b/lib/jackson-databind-2.20.1.jar differ diff --git a/lib/jackson-datatype-jsr310-2.20.1.jar b/lib/jackson-datatype-jsr310-2.20.1.jar new file mode 100644 index 0000000..171c447 Binary files /dev/null and b/lib/jackson-datatype-jsr310-2.20.1.jar differ diff --git a/src/mybank/ContoCorrente.java b/src/mybank/ContoCorrente.java new file mode 100644 index 0000000..2b54c04 --- /dev/null +++ b/src/mybank/ContoCorrente.java @@ -0,0 +1,98 @@ +/* + * 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 ContoCorrente { + private String nome; + private String cognome; + private String codiceFiscale; + private double saldo; + private int numeroContoCorrente; + + private static ArrayList numeriContiCorrenti = new ArrayList<>(); + + @JsonFormat + (shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") + private Date dataDiNascita; + + public ContoCorrente() { + } + + public ContoCorrente(String nome, String cognome, String codiceFiscale, Date dataDiNascita, double saldo, + int numeroContoCorrente) { + this.nome = nome; + this.cognome = cognome; + this.codiceFiscale = codiceFiscale; + this.dataDiNascita = dataDiNascita; + this.saldo = saldo; + this.numeroContoCorrente = numeroContoCorrente; + numeriContiCorrenti.add(numeroContoCorrente); + MyBank.log("Apertura del conto con saldo iniziale di " + this.saldo, numeroContoCorrente); + } + + public static List getNumeriContiCorrenti() { + return numeriContiCorrenti; + } + + public static void setNumeriContiCorrenti(List numeriContiCorrenti) { + ContoCorrente.numeriContiCorrenti = (ArrayList)numeriContiCorrenti; + } + + public int getNumeroContoCorrente() { + return numeroContoCorrente; + } + + public void versa(double quantita){ + this.saldo += quantita; + MyBank.log("Versamento di " + quantita + " effettuato con successo.", this.numeroContoCorrente); + logSaldoCorrente(); + } + + public void preleva(double quantita) throws IllegalArgumentException{ + if (quantita > this.saldo) { + MyBank.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); + logSaldoCorrente(); + } + } + + private void logSaldoCorrente() { + 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; + } +} \ No newline at end of file diff --git a/src/mybank/MyBank.java b/src/mybank/MyBank.java new file mode 100644 index 0000000..51d7494 --- /dev/null +++ b/src/mybank/MyBank.java @@ -0,0 +1,338 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.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.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.InputMismatchException; +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 + */ +public class MyBank { + + /** + * @param args the command line arguments + */ + + 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 conti = importaConti(); + + do { + System.out.println("Scegliere un'opzione:"); + System.out.println("1. Aprire conto corrente"); + System.out.println("2. Versamento"); + System.out.println("3. Prelievo"); + System.out.println("4. Mostra movimenti"); + System.out.println("0. Esci"); + System.out.print("Opzione: "); + + try { + scelta = sc.nextInt(); + sc.nextLine(); + + switch (scelta) { + case 0: + break; + case 1: + aggiungiConto(conti); + System.out.println("Numero conto: " + conti.getLast().getNumeroContoCorrente()); + System.out.println("Conto corrente aggiunto con successo."); + pausa(); + break; + case 2: + if (conti.isEmpty()) { + System.out.println(ERRORE_CONTI_VUOTO); + } else { + versa(conti); + System.out.println("Versamento effettuato con successo."); + } + pausa(); + break; + case 3: + if (conti.isEmpty()) { + System.out.println(ERRORE_CONTI_VUOTO); + } else { + preleva(conti); + System.out.println("Prelievo effettuato con successo."); + } + pausa(); + break; + case 4: + if (conti.isEmpty()) { + System.out.println(ERRORE_CONTI_VUOTO); + } else { + stampaLog(conti); + } + pausa(); + break; + default: + System.out.println(ERRORE_GENERICO); + pausa(); + break; + } + } catch (InputMismatchException _) { + System.out.println(ERRORE_GENERICO); + pausa(); + } + } while (scelta != 0); + } + + public static void pausa() { + System.out.println("Premere un tasto per continuare. . ."); + sc.nextLine(); + } + + static ArrayList importaConti() { + ArrayList 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 numeriContiCorrenti = (ArrayList) ContoCorrente + .getNumeriContiCorrenti(); + numeriContiCorrenti.add(conti.getLast().getNumeroContoCorrente()); + ContoCorrente.setNumeriContiCorrenti(numeriContiCorrenti); + + } catch (Exception e) { + System.out.println("Errore nella lettura del file di conto corrente."); + System.out.println(e.getMessage()); + System.out.println(e.getStackTrace()); + } + } + } + } + return conti; + } + + static void aggiungiConto(ArrayList conti) { + String nome; + String cognome; + String codiceFiscale; + Date dataDiNascita; + double saldoIniziale; + int numeroContoCorrente; + boolean error; + ContoCorrente contoCorrente; + + do { + error = false; + System.out.print("Inserire il proprio nome: "); + nome = sc.nextLine().trim(); + + System.out.print("Inserire il proprio cognome: "); + cognome = sc.nextLine().trim(); + + codiceFiscale = codiceFiscale(); + + dataDiNascita = dataDiNascita(); + + saldoIniziale = quantita("del saldo iniziale"); + + 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() { + String codiceFiscale; + boolean error; + Pattern pattern = Pattern.compile("[A-Z]{6}[ABCDEHLMPRST]{3}\\d{2}[A-Z]\\d{3}[A-Z]"); + Matcher matcher; + + do { + error = false; + System.out.print("Inserire il proprio codice fiscale: "); + codiceFiscale = sc.nextLine().trim().toUpperCase(); + matcher = pattern.matcher(codiceFiscale); + if (!matcher.find()) { + System.out.println(ERRORE_GENERICO); + pausa(); + error = true; + } + } while (error); + return codiceFiscale; + } + + static Date dataDiNascita() { + Date dataDiNascita = null; + boolean error; + final String FORMATO_DATA = "dd/MM/yyyy"; + SimpleDateFormat sdf = new SimpleDateFormat(FORMATO_DATA); + sdf.setLenient(false); + + do { + error = false; + System.out.print("Inserire la propria data di nascita nel formato " + FORMATO_DATA + ": "); + try { + dataDiNascita = sdf.parse(sc.nextLine()); + } catch (ParseException _) { + System.out.println("Errore: la data inserita non è valida."); + pausa(); + error = true; + } + } while (error); + return dataDiNascita; + } + + static double quantita(String diCheCosa) { + double quantita = 0; + boolean error; + + do { + error = false; + System.out.print("Inserire la quantità " + diCheCosa + ": "); + try { + quantita = sc.nextDouble(); + sc.nextLine(); + + if (quantita <= 0) { + System.out.println("Errore: la quantità non può essere minore o uguale a zero."); + pausa(); + error = true; + } + } catch (InputMismatchException _) { + System.out.println(ERRORE_GENERICO); + pausa(); + error = true; + } + } while (error); + + 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 conti) { + selezionaConto(conti).versa(quantita("da versare")); + } + + static void preleva(ArrayList conti) { + boolean error; + + do { + error = false; + try { + selezionaConto(conti).preleva(quantita("da prelevare")); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + pausa(); + error = true; + } + } while (error); + + } + + static ContoCorrente selezionaConto(ArrayList conti) { + ContoCorrente contoCorrente = null; + int numeroContoCorrente; + boolean error; + + do { + error = false; + System.out.print("Inserire il proprio numero di conto: "); + numeroContoCorrente = sc.nextInt(); + sc.nextLine(); + + if (!ContoCorrente.getNumeriContiCorrenti().contains(numeroContoCorrente)) { + System.out.println("Errore: il conto corrente specificato non esiste."); + pausa(); + error = true; + } else { + for (ContoCorrente conto : conti) { + if (conto.getNumeroContoCorrente() == numeroContoCorrente) { + contoCorrente = conto; + break; + } + } + } + } 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 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."); + } + } +} diff --git a/src/mybank/build.xml b/src/mybank/build.xml new file mode 100644 index 0000000..9d9fbae --- /dev/null +++ b/src/mybank/build.xml @@ -0,0 +1,5 @@ + + + Builds, tests, and runs the project DIRDIR. + + diff --git a/nbproject/build-impl.xml b/src/mybank/nbproject/build-impl.xml similarity index 99% rename from nbproject/build-impl.xml rename to src/mybank/nbproject/build-impl.xml index bcf51a4..c285611 100644 --- a/nbproject/build-impl.xml +++ b/src/mybank/nbproject/build-impl.xml @@ -19,7 +19,7 @@ is divided into following sections: - cleanup --> - + @@ -619,7 +619,7 @@ is divided into following sections: - + @@ -716,7 +716,7 @@ is divided into following sections: - + @@ -1057,7 +1057,7 @@ is divided into following sections: - + @@ -1728,7 +1728,7 @@ is divided into following sections: - + diff --git a/nbproject/private/private.properties b/src/mybank/nbproject/private/private.properties similarity index 98% rename from nbproject/private/private.properties rename to src/mybank/nbproject/private/private.properties index 77d0f52..d48d617 100644 --- a/nbproject/private/private.properties +++ b/src/mybank/nbproject/private/private.properties @@ -1,2 +1,3 @@ compile.on.save=true user.properties.file=/home/Verde/.netbeans/28/build.properties + diff --git a/nbproject/project.properties b/src/mybank/nbproject/project.properties similarity index 95% rename from nbproject/project.properties rename to src/mybank/nbproject/project.properties index f54516e..60bc7e9 100644 --- a/nbproject/project.properties +++ b/src/mybank/nbproject/project.properties @@ -28,10 +28,10 @@ debug.test.modulepath=\ dist.archive.excludes= # This directory is removed when the project is cleaned: dist.dir=dist -dist.jar=${dist.dir}/GUIMyBank.jar +dist.jar=${dist.dir}/mybank.jar dist.javadoc.dir=${dist.dir}/javadoc dist.jlink.dir=${dist.dir}/jlink -dist.jlink.output=${dist.jlink.dir}/GUIMyBank +dist.jlink.output=${dist.jlink.dir}/mybank excludes= includes=** jar.compress=false @@ -70,8 +70,8 @@ jlink.additionalmodules= # The jlink additional command line parameters jlink.additionalparam= jlink.launcher=true -jlink.launcher.name=GUIMyBank -main.class=guimybank.GUIMyBank +jlink.launcher.name=mybank +main.class=mybank.mybank manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF mkdist.disabled=false diff --git a/nbproject/project.xml b/src/mybank/nbproject/project.xml similarity index 93% rename from nbproject/project.xml rename to src/mybank/nbproject/project.xml index b665739..01d13e8 100644 --- a/nbproject/project.xml +++ b/src/mybank/nbproject/project.xml @@ -3,7 +3,7 @@ org.netbeans.modules.java.j2seproject - GUIMyBank + mybank @@ -13,3 +13,4 @@ +