Creazione Fattura + fix
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
fatture/
|
||||||
|
scontrini/
|
||||||
@@ -31,6 +31,7 @@ public class FatturaScontrini {
|
|||||||
static Scanner sc = new Scanner(System.in);
|
static Scanner sc = new Scanner(System.in);
|
||||||
static final String PATH_MENU = "./src/fatturascontrini/menu.txt";
|
static final String PATH_MENU = "./src/fatturascontrini/menu.txt";
|
||||||
static final String PATH_SCONTRINI = "./src/fatturascontrini/scontrini/";
|
static final String PATH_SCONTRINI = "./src/fatturascontrini/scontrini/";
|
||||||
|
static final String PATH_FATTURA = "./src/fatturascontrini/fatture/";
|
||||||
static final String ERRORE_DEFAULT = "Errore: opzione non valida.";
|
static final String ERRORE_DEFAULT = "Errore: opzione non valida.";
|
||||||
static final String ERRORE_FILE = "Errore nella lettura del file: ";
|
static final String ERRORE_FILE = "Errore nella lettura del file: ";
|
||||||
static int codiceScontrino = 0;
|
static int codiceScontrino = 0;
|
||||||
@@ -38,8 +39,6 @@ public class FatturaScontrini {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int scelta = -1;
|
int scelta = -1;
|
||||||
ArrayList<int[]> ordinazione = null;
|
ArrayList<int[]> ordinazione = null;
|
||||||
boolean pagamentoEffettuato = false;
|
|
||||||
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
System.out.println("Scegliere un'opzione:");
|
System.out.println("Scegliere un'opzione:");
|
||||||
@@ -63,7 +62,6 @@ public class FatturaScontrini {
|
|||||||
case 2:
|
case 2:
|
||||||
if (ordinazione != null) {
|
if (ordinazione != null) {
|
||||||
pagare(ordinazione);
|
pagare(ordinazione);
|
||||||
pagamentoEffettuato = true;
|
|
||||||
} else {
|
} else {
|
||||||
System.out.println(
|
System.out.println(
|
||||||
"Errore: è necessario effettuare un'ordinazione prima di procedere al pagamento.");
|
"Errore: è necessario effettuare un'ordinazione prima di procedere al pagamento.");
|
||||||
@@ -71,8 +69,8 @@ public class FatturaScontrini {
|
|||||||
pausa();
|
pausa();
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
if (pagamentoEffettuato) {
|
if (!trovaScontrini().isEmpty()) {
|
||||||
trovaScontriniPerData();
|
fattura();
|
||||||
} else {
|
} else {
|
||||||
System.out.println(
|
System.out.println(
|
||||||
"Errore: è necessario pagare prima di procedere alla creazione della fattura.");
|
"Errore: è necessario pagare prima di procedere alla creazione della fattura.");
|
||||||
@@ -182,7 +180,7 @@ public class FatturaScontrini {
|
|||||||
ritorno = sc.nextInt();
|
ritorno = sc.nextInt();
|
||||||
sc.nextLine();
|
sc.nextLine();
|
||||||
|
|
||||||
if (ritorno < 0 || ritorno > getMenuSize()) {
|
if (ritorno < 0 || ritorno >= getMenuSize()) {
|
||||||
System.out.println(ERRORE_DEFAULT);
|
System.out.println(ERRORE_DEFAULT);
|
||||||
pausa();
|
pausa();
|
||||||
error = true;
|
error = true;
|
||||||
@@ -240,7 +238,7 @@ public class FatturaScontrini {
|
|||||||
ritorno = sc.nextInt();
|
ritorno = sc.nextInt();
|
||||||
sc.nextLine();
|
sc.nextLine();
|
||||||
|
|
||||||
if (ritorno < 0) {
|
if (ritorno <= 0) {
|
||||||
System.out.println("Errore: non è possibile selezionare una quantità minore di zero.");
|
System.out.println("Errore: non è possibile selezionare una quantità minore di zero.");
|
||||||
pausa();
|
pausa();
|
||||||
error = true;
|
error = true;
|
||||||
@@ -363,7 +361,7 @@ public class FatturaScontrini {
|
|||||||
return ritorno;
|
return ritorno;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void trovaScontriniPerData() {
|
static void fattura() {
|
||||||
boolean error;
|
boolean error;
|
||||||
String formatoDataInserimento = "dd/MM/yyyy";
|
String formatoDataInserimento = "dd/MM/yyyy";
|
||||||
String formatoDataScontrini = "yyyy_MM_dd";
|
String formatoDataScontrini = "yyyy_MM_dd";
|
||||||
@@ -375,7 +373,7 @@ public class FatturaScontrini {
|
|||||||
do {
|
do {
|
||||||
error = false;
|
error = false;
|
||||||
System.out.print("Inserire la data per cui fatturare nel formato "
|
System.out.print("Inserire la data per cui fatturare nel formato "
|
||||||
+ formatoDataInserimento.replace("d", "g").replace("y", "a") + ": ");
|
+ formatoDataInserimento.replace("d", "g").replace("M", "m").replace("y", "a") + ": ");
|
||||||
try {
|
try {
|
||||||
dataRicerca = sdf.parse(sc.nextLine());
|
dataRicerca = sdf.parse(sc.nextLine());
|
||||||
} catch (ParseException _) {
|
} catch (ParseException _) {
|
||||||
@@ -388,6 +386,13 @@ public class FatturaScontrini {
|
|||||||
sdf = new SimpleDateFormat(formatoDataScontrini);
|
sdf = new SimpleDateFormat(formatoDataScontrini);
|
||||||
|
|
||||||
listaScontrini = trovaScontriniPerData(trovaScontrini(), sdf.format(dataRicerca));
|
listaScontrini = trovaScontriniPerData(trovaScontrini(), 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> trovaScontrini() {
|
static ArrayList<File> trovaScontrini() {
|
||||||
@@ -397,8 +402,7 @@ public class FatturaScontrini {
|
|||||||
if (listaFile != null) {
|
if (listaFile != null) {
|
||||||
for (int i = 0; i < listaFile.length; i++) {
|
for (int i = 0; i < listaFile.length; i++) {
|
||||||
if (listaFile[i].isFile() &&
|
if (listaFile[i].isFile() &&
|
||||||
listaFile[i].getName().split("_")[0].equals("scontrino") &&
|
listaFile[i].getName().split("_")[0].equals("scontrino")) {
|
||||||
listaFile[i].getName().split(".")[1].equals("txt")) {
|
|
||||||
ritorno.add(listaFile[i]);
|
ritorno.add(listaFile[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -409,13 +413,46 @@ public class FatturaScontrini {
|
|||||||
|
|
||||||
static ArrayList<File> trovaScontriniPerData(ArrayList<File> scontrini, String data) {
|
static ArrayList<File> trovaScontriniPerData(ArrayList<File> scontrini, String data) {
|
||||||
ArrayList<File> ritorno = new ArrayList<>();
|
ArrayList<File> ritorno = new ArrayList<>();
|
||||||
|
|
||||||
for (File scontrino : scontrini) {
|
for (File scontrino : scontrini) {
|
||||||
if(scontrino.getName().contains(data)){
|
if (scontrino.getName().contains(data)) {
|
||||||
ritorno.add(scontrino);
|
ritorno.add(scontrino);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ritorno;
|
return ritorno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static double calcolaTotaleScontrini(ArrayList<File> scontrini) {
|
||||||
|
double ritorno = 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: "));
|
||||||
|
|
||||||
|
ritorno += Double.parseDouble(riga.split(":")[1]);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println(ERRORE_FILE + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ritorno;
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user