778 lines
26 KiB
Java
778 lines
26 KiB
Java
/*
|
|
* 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 fatturascontrini;
|
|
|
|
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.StringTokenizer;
|
|
|
|
/**
|
|
*
|
|
* @author Verde
|
|
*/
|
|
public class FatturaScontrini {
|
|
|
|
/**
|
|
* @param args the command line arguments
|
|
*/
|
|
|
|
static Scanner sc = new Scanner(System.in);
|
|
static final String PATH_MENU = "./src/fatturascontrini/menu.txt";
|
|
static final String PATH_SCONTRINI = "./src/fatturascontrini/scontrini/";
|
|
static final String PATH_FATTURA = "./src/fatturascontrini/fatture/";
|
|
static final String PATH_PROGRAM_DATA = "./src/fatturascontrini/programData.txt";
|
|
static final String ERRORE_DEFAULT = "Errore: opzione non valida.";
|
|
static final String ERRORE_FILE = "Errore nella lettura del file: ";
|
|
static final String ERRORE_MENU_VUOTO = "Errore: il menu è vuoto.";
|
|
static int codiceScontrino = getCodice(0);
|
|
|
|
public static void main(String[] args) {
|
|
int scelta = -1;
|
|
ArrayList<int[]> ordinazione = null;
|
|
|
|
do {
|
|
System.out.println("Scegliere un'opzione:");
|
|
System.out.println("1. Visualizza menu");
|
|
System.out.println("2. Apri comanda");
|
|
System.out.println("3. Modalità amministratore");
|
|
System.out.println("0. Esci");
|
|
System.out.print("Opzione: ");
|
|
|
|
try {
|
|
scelta = sc.nextInt();
|
|
sc.nextLine();
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
if (getFileSize(PATH_MENU) < 1) {
|
|
System.out.println();
|
|
} else {
|
|
stampaMenu();
|
|
}
|
|
pausa();
|
|
break;
|
|
case 2:
|
|
if (getFileSize(PATH_MENU) < 1) {
|
|
System.out.println(ERRORE_MENU_VUOTO);
|
|
} else {
|
|
ordinazione = placeOrdinazione(ordinazione);
|
|
}
|
|
pausa();
|
|
break;
|
|
case 3:
|
|
autenticazione();
|
|
adminMode();
|
|
break;
|
|
default:
|
|
System.out.println("Opzione non valida.");
|
|
pausa();
|
|
break;
|
|
}
|
|
} catch (InputMismatchException _) {
|
|
System.out.println("Errore: scelta non valida.");
|
|
pausa();
|
|
}
|
|
} while (scelta != 0);
|
|
}
|
|
|
|
public static void pausa() {
|
|
System.out.println("Premere un tasto per continuare. . .");
|
|
sc.nextLine();
|
|
}
|
|
|
|
static ArrayList<int[]> placeOrdinazione(ArrayList<int[]> ordinazione) {
|
|
boolean error;
|
|
String scelta;
|
|
|
|
if (ordinazione != null) {
|
|
do {
|
|
error = false;
|
|
System.out.print(
|
|
"Attenzione: verrà sovrascritta la precedente ordinazione. Continuare? [S/N] ");
|
|
scelta = sc.nextLine().trim().toLowerCase();
|
|
switch (scelta) {
|
|
case "s":
|
|
ordinazione = ordinazione();
|
|
System.out.println("Ordinazione effettuata con successo.");
|
|
break;
|
|
case "n":
|
|
System.out.println("L'ordinazione non è stata modificata.");
|
|
break;
|
|
default:
|
|
System.out.println(ERRORE_DEFAULT);
|
|
pausa();
|
|
error = true;
|
|
break;
|
|
}
|
|
} while (error);
|
|
} else {
|
|
ordinazione = ordinazione();
|
|
System.out.println("Ordinazione effettuata con successo.");
|
|
}
|
|
|
|
return ordinazione;
|
|
}
|
|
|
|
static ArrayList<int[]> ordinazione() {
|
|
ArrayList<int[]> ordinazione = new ArrayList<>();
|
|
boolean exit;
|
|
boolean error;
|
|
boolean isDuplicate = false;
|
|
String scelta;
|
|
|
|
do {
|
|
exit = true;
|
|
int[] ordine = { selezioneMenu(), quantita() };
|
|
|
|
isDuplicate = isOrdinazioneDuplicata(ordinazione, ordine);
|
|
|
|
if (!isDuplicate) {
|
|
ordinazione.add(ordine);
|
|
}
|
|
|
|
System.out.println(ordinazioneToString(ordinazione));
|
|
|
|
do {
|
|
error = false;
|
|
System.out.print("Modificare l'ordinazione? [S/N] ");
|
|
scelta = sc.nextLine().trim().toLowerCase();
|
|
switch (scelta) {
|
|
case "s":
|
|
ordinazione = cancellaInOrdinazione(ordinazione);
|
|
exit = false;
|
|
break;
|
|
case "n":
|
|
System.out.println("Stampa dello scontrino. . .");
|
|
pagare(ordinazione);
|
|
break;
|
|
default:
|
|
System.out.println(ERRORE_DEFAULT);
|
|
pausa();
|
|
error = true;
|
|
break;
|
|
}
|
|
} while (error);
|
|
} while (!exit);
|
|
|
|
return ordinazione;
|
|
}
|
|
|
|
static int selezioneMenu() {
|
|
boolean error;
|
|
int voceSelezionata = -1;
|
|
do {
|
|
error = false;
|
|
|
|
System.out.println("Selezionare un'opzione tra le presenti:");
|
|
stampaMenu();
|
|
System.out.print("Scelta: ");
|
|
|
|
try {
|
|
voceSelezionata = sc.nextInt();
|
|
sc.nextLine();
|
|
|
|
if (voceSelezionata < 0 || voceSelezionata >= getFileSize(PATH_MENU)) {
|
|
System.out.println(ERRORE_DEFAULT);
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} catch (InputMismatchException _) {
|
|
System.out.println(ERRORE_DEFAULT);
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} while (error);
|
|
|
|
return voceSelezionata;
|
|
}
|
|
|
|
static void stampaMenu() {
|
|
try (BufferedReader br = new BufferedReader(new FileReader(PATH_MENU))) {
|
|
StringTokenizer st;
|
|
String riga = br.readLine();
|
|
|
|
for (int i = 0; i < getFileSize(PATH_MENU); i++) {
|
|
st = new StringTokenizer(riga, ":");
|
|
System.out.println(i + ". " + st.nextToken() + ": " + st.nextToken().trim());
|
|
riga = br.readLine();
|
|
}
|
|
} catch (IOException e) {
|
|
System.out.println(ERRORE_FILE + e.getMessage());
|
|
}
|
|
}
|
|
|
|
static int getFileSize(String path) {
|
|
int dimensioneFile = 0;
|
|
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
|
|
String riga = br.readLine();
|
|
|
|
while (riga != null) {
|
|
dimensioneFile++;
|
|
riga = br.readLine();
|
|
}
|
|
} catch (IOException e) {
|
|
System.out.println(ERRORE_FILE + e.getMessage());
|
|
}
|
|
return dimensioneFile;
|
|
}
|
|
|
|
static int quantita() {
|
|
boolean error;
|
|
int quantita = -1;
|
|
do {
|
|
error = false;
|
|
|
|
System.out.print("Inserire la quantità da acquistare: ");
|
|
|
|
try {
|
|
quantita = sc.nextInt();
|
|
sc.nextLine();
|
|
|
|
if (quantita <= 0) {
|
|
System.out.println("Errore: non è possibile selezionare una quantità minore di zero.");
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} catch (InputMismatchException _) {
|
|
System.out.println(ERRORE_DEFAULT);
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} while (error);
|
|
|
|
return quantita;
|
|
}
|
|
|
|
static boolean isOrdinazioneDuplicata(ArrayList<int[]> ordinazione, int[] ordine) {
|
|
boolean isOrdinazioneDuplicata = false;
|
|
boolean error;
|
|
String scelta;
|
|
|
|
for (int[] ordini : ordinazione) {
|
|
if (ordini[0] == ordine[0]) {
|
|
isOrdinazioneDuplicata = true;
|
|
do {
|
|
error = false;
|
|
System.out
|
|
.print(
|
|
"L'opzione scelta è già presente nell'ordinazione. Aggiornare la quantità? [S/N] ");
|
|
scelta = sc.nextLine().trim().toLowerCase();
|
|
switch (scelta) {
|
|
case "s":
|
|
ordinazione.get(ordinazione.indexOf(ordini))[1] = ordine[1];
|
|
break;
|
|
case "n":
|
|
System.out.println("L'ordinazione non è stata modificata.");
|
|
break;
|
|
default:
|
|
System.out.println(ERRORE_DEFAULT);
|
|
pausa();
|
|
error = true;
|
|
break;
|
|
}
|
|
} while (error);
|
|
}
|
|
|
|
}
|
|
return isOrdinazioneDuplicata;
|
|
}
|
|
|
|
static String ordinazioneToString(ArrayList<int[]> ordinazione) {
|
|
StringBuilder sb = new StringBuilder();
|
|
String[][] menu = tabellaNomePrezzo();
|
|
double totale = 0;
|
|
|
|
// Larghezze colonne
|
|
int larghezzaNome = 50;
|
|
int larghezzaPrezzoQuantita = 10;
|
|
String formattatoreLarghezzaNome = "%-" + larghezzaNome + "s";
|
|
String formattatoreLarghezzaPrezzoQuantita = "%-" + larghezzaPrezzoQuantita + "s";
|
|
|
|
// Intestazione tabella
|
|
sb.append(String.format(formattatoreLarghezzaNome, "Nome"));
|
|
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, "Prezzo"));
|
|
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, "Quantità"));
|
|
sb.append("\n");
|
|
|
|
for (int i = 0; i < ordinazione.size(); i++) {
|
|
sb.append(String.format(formattatoreLarghezzaNome, menu[0][ordinazione.get(i)[0]].trim()));// nome
|
|
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, menu[1][ordinazione.get(i)[0]].trim()));// prezzo
|
|
sb.append(String.format(formattatoreLarghezzaPrezzoQuantita, Integer.toString(ordinazione.get(i)[1])));// quantità
|
|
sb.append("\n");
|
|
totale += Double.parseDouble(menu[1][ordinazione.get(i)[0]].replace("€", "").trim())
|
|
* ordinazione.get(i)[1];
|
|
}
|
|
|
|
sb.append("\nTotale: " + totale);
|
|
|
|
return sb.toString();
|
|
}
|
|
|
|
static String[][] tabellaNomePrezzo() {
|
|
String[][] tabellaNomePrezzo = new String[2][getFileSize(PATH_MENU)];
|
|
try (BufferedReader br = new BufferedReader(new FileReader(PATH_MENU))) {
|
|
String riga = br.readLine();
|
|
|
|
for (int i = 0; i < getFileSize(PATH_MENU); i++) {
|
|
tabellaNomePrezzo[0][i] = riga.split(":")[0];
|
|
tabellaNomePrezzo[1][i] = riga.split(":")[1].trim();
|
|
riga = br.readLine();
|
|
}
|
|
} catch (IOException e) {
|
|
System.out.println(ERRORE_FILE + e.getMessage());
|
|
}
|
|
return tabellaNomePrezzo;
|
|
}
|
|
|
|
static ArrayList<int[]> cancellaInOrdinazione(ArrayList<int[]> ordinazione) {
|
|
String scelta;
|
|
boolean error;
|
|
|
|
do {
|
|
error = false;
|
|
System.out.print("Eliminare un ordinazione? [S/N] ");
|
|
scelta = sc.nextLine().trim().toLowerCase();
|
|
switch (scelta) {
|
|
case "s":
|
|
ordinazione = cancellaOrdine(ordinazione);
|
|
System.out.println("Ordine cancellato con successo.");
|
|
pausa();
|
|
break;
|
|
case "n":
|
|
System.out.println("Nessuna ordinazione è stata cancellata.");
|
|
pausa();
|
|
break;
|
|
default:
|
|
System.out.println(ERRORE_DEFAULT);
|
|
pausa();
|
|
error = true;
|
|
break;
|
|
}
|
|
} while (error);
|
|
|
|
return ordinazione;
|
|
}
|
|
|
|
static ArrayList<int[]> cancellaOrdine(ArrayList<int[]> ordinazione) {
|
|
int scelta = -1;
|
|
String[][] menu = tabellaNomePrezzo();
|
|
boolean error;
|
|
|
|
do {
|
|
error = false;
|
|
|
|
System.out.println("Quale opzione eliminare tra le seguenti?");
|
|
for (int i = 0; i < ordinazione.size(); i++) {
|
|
System.out.println(i + ". " + menu[0][ordinazione.get(i)[0]].trim() + ": "
|
|
+ menu[1][ordinazione.get(i)[0]].trim());
|
|
}
|
|
System.out.print("Scelta: ");
|
|
|
|
try {
|
|
scelta = sc.nextInt();
|
|
sc.nextLine();
|
|
|
|
if (scelta < 0 || scelta >= getFileSize(PATH_MENU)) {
|
|
System.out.println(ERRORE_DEFAULT);
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} catch (InputMismatchException _) {
|
|
System.out.println(ERRORE_DEFAULT);
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} while (error);
|
|
|
|
ordinazione.remove(scelta);
|
|
|
|
return ordinazione;
|
|
}
|
|
|
|
static void pagare(ArrayList<int[]> ordinazione) {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
|
|
Date now = new Date();
|
|
String contenutoScontrino;
|
|
|
|
File f = new File(PATH_SCONTRINI);
|
|
|
|
if (!f.exists()) {
|
|
f.mkdir();
|
|
}
|
|
|
|
try (BufferedWriter bw = new BufferedWriter(
|
|
new FileWriter(PATH_SCONTRINI + "scontrino_" + sdf.format(now) + ".txt"))) {
|
|
|
|
sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
|
|
|
|
incCodice(0);
|
|
|
|
contenutoScontrino = "Codice scontrino: " + codiceScontrino +
|
|
"\nData e ora di emissione: " + sdf.format(now) +
|
|
"\n\n" +
|
|
ordinazioneToString(ordinazione);
|
|
|
|
System.out.println(contenutoScontrino);
|
|
bw.write(contenutoScontrino);
|
|
} catch (IOException e) {
|
|
System.out.println(ERRORE_FILE + e.getMessage());
|
|
}
|
|
}
|
|
|
|
static void incCodice(int tipo) {
|
|
//0: scontrino
|
|
//1: fattura
|
|
StringBuilder sb = new StringBuilder();
|
|
ArrayList<String> programData = fileToStringArray(PATH_PROGRAM_DATA);
|
|
programData.set(tipo,
|
|
programData.get(tipo).split(":")[0].trim() + ":" + Integer.toString(getCodice(tipo) + 1));
|
|
codiceScontrino++;
|
|
try (BufferedWriter bw = new BufferedWriter(
|
|
new FileWriter(PATH_PROGRAM_DATA))) {
|
|
for (String riga : programData) {
|
|
sb.append(riga);
|
|
sb.append("\n");
|
|
}
|
|
bw.write(sb.toString());
|
|
} catch (IOException e) {
|
|
System.out.println(ERRORE_FILE + e.getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
static int getCodice(int tipo) {
|
|
//0: scontrino
|
|
//1: fattura
|
|
return Integer.parseInt(fileToStringArray(PATH_PROGRAM_DATA).get(tipo).split(":")[1].trim());
|
|
}
|
|
|
|
static void adminMode() {
|
|
int scelta = -1;
|
|
|
|
do {
|
|
System.out.println("Scegliere un'opzione:");
|
|
System.out.println("1. Visualizza menu");
|
|
System.out.println("2. Aggiunta di un nuovo piatto al menu");
|
|
System.out.println("3. Rimozione di un nuovo piatto dal menu");
|
|
System.out.println("4. Fatturazione elettronica della giornata");
|
|
System.out.println("0. Esci dalla modalità amministratore");
|
|
System.out.print("Opzione: ");
|
|
|
|
try {
|
|
scelta = sc.nextInt();
|
|
sc.nextLine();
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
if (getFileSize(PATH_MENU) < 1) {
|
|
System.out.println(ERRORE_MENU_VUOTO);
|
|
} else {
|
|
stampaMenu();
|
|
}
|
|
pausa();
|
|
break;
|
|
case 2:
|
|
aggiungiVoceMenu();
|
|
System.out.println("Piatto aggiunto con successo.");
|
|
pausa();
|
|
break;
|
|
case 3:
|
|
if (getFileSize(PATH_MENU) < 1) {
|
|
System.out.println(ERRORE_MENU_VUOTO);
|
|
} else {
|
|
rimuoviVoceMenu();
|
|
System.out.println("Piatto rimosso con successo.");
|
|
}
|
|
pausa();
|
|
break;
|
|
case 4:
|
|
fattura();
|
|
pausa();
|
|
break;
|
|
default:
|
|
System.out.println("Opzione non valida.");
|
|
pausa();
|
|
break;
|
|
}
|
|
} catch (InputMismatchException _) {
|
|
System.out.println("Errore: scelta non valida.");
|
|
pausa();
|
|
}
|
|
} while (scelta != 0);
|
|
}
|
|
|
|
static void autenticazione() {
|
|
String username;
|
|
String password;
|
|
boolean error;
|
|
int failedLoginCount = 0;
|
|
final int MAX_FAILED_LOGIN_COUNT = 3;
|
|
String savedUsername = "";
|
|
String savedPassword = "";
|
|
|
|
try (BufferedReader br = new BufferedReader(new FileReader(PATH_PROGRAM_DATA))) {
|
|
String riga;
|
|
|
|
do {
|
|
riga = br.readLine();
|
|
} while (!riga.contains("username: "));
|
|
|
|
savedUsername = riga.split(":")[1].trim();
|
|
savedPassword = br.readLine().split(":")[1].trim();
|
|
} catch (IOException e) {
|
|
System.out.println(ERRORE_FILE + e.getMessage());
|
|
}
|
|
|
|
do {
|
|
error = false;
|
|
if (failedLoginCount == MAX_FAILED_LOGIN_COUNT) {
|
|
failedLoginCount = failedLogin(failedLoginCount);
|
|
error = true;
|
|
} else {
|
|
System.out.print("Username: ");
|
|
username = sc.nextLine().trim();
|
|
password = new String(System.console().readPassword("Password: ")).trim();
|
|
|
|
if (!username.equals(savedUsername)) {
|
|
System.out.println("Errore: il nome utente non è valido.");
|
|
pausa();
|
|
error = true;
|
|
failedLoginCount++;
|
|
} else {
|
|
|
|
if (!password.equals(savedPassword)) {
|
|
System.out.println("Errore: la password non è valida.");
|
|
pausa();
|
|
error = true;
|
|
failedLoginCount++;
|
|
}
|
|
}
|
|
}
|
|
|
|
} while (error);
|
|
}
|
|
|
|
static int failedLogin(int failedLoginCount) {
|
|
|
|
long durataTimer = 7500;
|
|
final String FORMATTAZIONE_ERRORE_TIMER = "\rRiprovare tra %d secondi";
|
|
|
|
long istanteInizioTimer = System.currentTimeMillis();
|
|
durataTimer *= 2;
|
|
System.out.println("Errore: troppi tentativi di login falliti.");
|
|
|
|
while (true) {
|
|
long differenzaDiTempo = System.currentTimeMillis() - istanteInizioTimer;
|
|
// mostra il tempo rimanente ogni secondo
|
|
if (System.currentTimeMillis() % 1000 == 0) {
|
|
System.out.printf(FORMATTAZIONE_ERRORE_TIMER, (int) ((durataTimer - differenzaDiTempo) / 1000));
|
|
}
|
|
if (differenzaDiTempo >= durataTimer) {
|
|
failedLoginCount = 0;
|
|
System.out.println();
|
|
break;
|
|
}
|
|
}
|
|
return failedLoginCount;
|
|
}
|
|
|
|
static void aggiungiVoceMenu() {
|
|
String nome;
|
|
String prezzo;
|
|
StringBuilder sb = new StringBuilder();
|
|
System.out.print("Inserire il nome della nuova voce: ");
|
|
nome = sc.nextLine().trim();
|
|
System.out.print("Inserire il prezzo della nuova voce: ");
|
|
prezzo = Double.toString(prezzo());
|
|
|
|
if (prezzo.contains(",")) {
|
|
prezzo = prezzo.replace(",", ".");
|
|
}
|
|
|
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(PATH_MENU, true))) {
|
|
sb.append(nome);
|
|
sb.append(": ");
|
|
sb.append(prezzo);
|
|
sb.append(" €\n");
|
|
|
|
bw.write(sb.toString());
|
|
} catch (IOException e) {
|
|
System.out.println(ERRORE_FILE + e.getMessage());
|
|
}
|
|
}
|
|
|
|
static double prezzo() {
|
|
boolean error;
|
|
double prezzo = -1;
|
|
do {
|
|
error = false;
|
|
|
|
System.out.print("Inserire il prezzo da acquistare: ");
|
|
|
|
try {
|
|
prezzo = sc.nextDouble();
|
|
sc.nextLine();
|
|
|
|
if (prezzo <= 0) {
|
|
System.out.println("Errore: non è possibile impostare un prezzo minore di zero.");
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} catch (InputMismatchException _) {
|
|
System.out.println(ERRORE_DEFAULT);
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} while (error);
|
|
|
|
return prezzo;
|
|
}
|
|
|
|
static void rimuoviVoceMenu() {
|
|
int voceDaRimuovere = selezioneMenu();
|
|
ArrayList<String> menu = fileToStringArray(PATH_MENU);
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
menu.remove(voceDaRimuovere);
|
|
|
|
for (String voce : menu) {
|
|
sb.append(voce);
|
|
sb.append("\n");
|
|
}
|
|
|
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(PATH_MENU))) {
|
|
bw.write(sb.toString());
|
|
} catch (IOException e) {
|
|
System.out.println(ERRORE_FILE + e.getMessage());
|
|
}
|
|
}
|
|
|
|
static ArrayList<String> fileToStringArray(String path) {
|
|
ArrayList<String> fileAsStringArray = new ArrayList<>();
|
|
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
|
|
String riga = br.readLine();
|
|
|
|
while (riga != null) {
|
|
fileAsStringArray.add(riga);
|
|
riga = br.readLine();
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
System.out.println(ERRORE_FILE + e.getMessage());
|
|
}
|
|
return fileAsStringArray;
|
|
}
|
|
|
|
static void fattura() {
|
|
boolean error;
|
|
String formatoDataInserimento = "dd/MM/yyyy";
|
|
String formatoDataScontrini = "yyyy_MM_dd";
|
|
SimpleDateFormat sdf = new SimpleDateFormat(formatoDataInserimento);
|
|
sdf.setLenient(false);
|
|
Date dataRicerca = null;
|
|
ArrayList<File> listaScontrini;
|
|
|
|
do {
|
|
error = false;
|
|
System.out.print("Inserire la data per cui fatturare nel formato "
|
|
+ formatoDataInserimento.replace("d", "g").replace("M", "m").replace("y", "a") + ": ");
|
|
try {
|
|
dataRicerca = sdf.parse(sc.nextLine());
|
|
} catch (ParseException _) {
|
|
System.out.println("Errore: data non valida.");
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} while (error);
|
|
|
|
sdf = new SimpleDateFormat(formatoDataScontrini);
|
|
|
|
listaScontrini = trovaScontriniPerData(sdf.format(dataRicerca));
|
|
|
|
if (listaScontrini.isEmpty()) {
|
|
System.out.println("Errore: non sono stati trovati scontrini per la data inserita.");
|
|
} else {
|
|
stampaFattura(new Fattura(listaScontrini.size(), calcolaTotaleScontrini(listaScontrini)),
|
|
sdf.format(dataRicerca));
|
|
}
|
|
}
|
|
|
|
static ArrayList<File> trovaScontriniPerData(String data) {
|
|
ArrayList<File> scontrini = trovaScontrini();
|
|
ArrayList<File> listaScontriniPerData = new ArrayList<>();
|
|
|
|
for (File scontrino : scontrini) {
|
|
if (scontrino.getName().contains(data)) {
|
|
listaScontriniPerData.add(scontrino);
|
|
}
|
|
}
|
|
|
|
return listaScontriniPerData;
|
|
}
|
|
|
|
static ArrayList<File> trovaScontrini() {
|
|
ArrayList<File> listaScontrini = new ArrayList<>();
|
|
File cartellaScontrini = new File(PATH_SCONTRINI);
|
|
File[] listaFile = cartellaScontrini.listFiles();
|
|
if (listaFile != null) {
|
|
for (int i = 0; i < listaFile.length; i++) {
|
|
if (listaFile[i].isFile() &&
|
|
listaFile[i].getName().split("_")[0].equals("scontrino")) {
|
|
listaScontrini.add(listaFile[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return listaScontrini;
|
|
}
|
|
|
|
static double calcolaTotaleScontrini(ArrayList<File> scontrini) {
|
|
double totaleScontrini = 0;
|
|
for (File scontrino : scontrini) {
|
|
try (BufferedReader br = new BufferedReader(new FileReader(scontrino))) {
|
|
String riga = br.readLine();
|
|
|
|
do {
|
|
riga = br.readLine();
|
|
} while (!riga.contains("Totale: "));
|
|
|
|
totaleScontrini += Double.parseDouble(riga.split(":")[1]);
|
|
} catch (IOException e) {
|
|
System.out.println(ERRORE_FILE + e.getMessage());
|
|
}
|
|
}
|
|
return totaleScontrini;
|
|
}
|
|
|
|
static void stampaFattura(Fattura fattura, String data) {
|
|
File f = new File(PATH_FATTURA);
|
|
|
|
if (!f.exists()) {
|
|
f.mkdir();
|
|
}
|
|
|
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(PATH_FATTURA + "fattura_" + data + ".txt"))) {
|
|
System.out.println(fattura.toString());
|
|
bw.write(fattura.toString());
|
|
} catch (IOException e) {
|
|
System.out.println(ERRORE_FILE + e.getMessage());
|
|
}
|
|
}
|
|
}
|